Jump to content

Recommended Posts

Posted (edited)

Boas pessoal, tenho este código que está funcional se usar o formato .xls, contudo se quiser alterar para .xlsx ele diz que o ficheiro não pode ser aberto por estar corrompido ou por a extensão não corresponder.

A minha pergunta é, Existe alguma maneira de fazer isto em .xlsx sem alterar muito este código base que tenho?

GV_VENDOR.Columns.RemoveAt(1)
    GV_VENDOR.Columns.RemoveAt(0)

    If GV_VENDOR.Rows.Count.ToString + 1 < 65536 Then
        GV_VENDOR.AllowPaging = "False"
        GV_VENDOR.DataBind()
        Dim tw As System.IO.StringWriter = New System.IO.StringWriter
        Dim hw As New System.Web.UI.HtmlTextWriter(tw)
        Dim frm As HtmlForm = New HtmlForm()
        Response.ContentType = "application/vnd.ms-excel"
        Response.AddHeader("content-disposition", "attachment;filename=VENDOR" & "_" & Format(Date.Now, "dd/MM/yyyy_HHmm") & ".xls")
        Response.Charset = ""
        EnableViewState = False
        Controls.Add(frm)
        frm.Controls.Add(GV_VENDOR)
        frm.RenderControl(hw)
        Response.Write(tw.ToString())
        Response.End()
        GV_VENDOR.AllowPaging = "True"
        GV_VENDOR.DataBind()
Edited by thoga31
GeSHi
Posted (edited)

Fiz a alteração das linhas :

	Response.ContentType = "Application / vnd.openxmlformats - officedocument.spreadsheetml.sheet"
	Response.AddHeader("content-disposition", "attachment;filename=VENDOR" & "_" & Format(Date.Now, "dd/MM/yyyy_HHmm") & ".xlsx")

Mas continua com este erro:

http://img593.imageshack.us/img593/4016/lu2d.jpg

Eu poderia fazer em XLS apenas mas o caso não me permite porque preciso de exportar muitas das vezes mais de 65 000rows algo que XLS não me permite fazer :/

Edited by startuga
Posted (edited)

bom dia,

já confirmaste se tens os drives corretos da versão mais recente do excel...

eu costumo no inicio do formulário ou do modulo... onde vou fazer exportação para dados Excel

colocar como referencia ao office a seguinte linha

Imports Microsoft.Office.Interop

comigo tem funcionado, depois é só escolher o tipo de ficheiro a gravar, alterando a extenção do ficheiro...

Edited by PMSF
Posted (edited)

Bom dia,

Este é o código que tenho,

Imports System.IO
Imports Microsoft.Office.Interop
Partial Public Class Selection_IMPVEN
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub export_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btn_IMPVEN_ExcelReport.Click

	GV_IMPVEN.Columns.RemoveAt(0)

	GV_IMPVEN.AllowPaging = "False"
	GV_IMPVEN.DataBind()
	Dim tw As System.IO.StringWriter = New System.IO.StringWriter
	Dim hw As New System.Web.UI.HtmlTextWriter(tw)
	Dim frm As HtmlForm = New HtmlForm()
	Response.ContentType = "Application / vnd.openxmlformats - officedocument.spreadsheetml.sheet"
	Response.AddHeader("content-disposition", "attachment;filename=IMPVEN" & "_" & Format(Date.Now, "dd/MM/yyyy_HHmm") & ".xlsx")
	Response.Charset = ""
	EnableViewState = False
	Controls.Add(frm)
	frm.Controls.Add(GV_IMPVEN)
	frm.RenderControl(hw)
	Response.Write(tw.ToString())
	Response.End()
	GV_IMPVEN.AllowPaging = "True"
	GV_IMPVEN.DataBind()


End Sub
End Class

Já com a referência adicionada e com a extensão alterada ele faz o mesmo :x

Edited by startuga
Posted

Bem, o objectivo era dar-te uma ideia de como poderias fazer. Nunca trabalhei com ASP, pelo que não te consigo ser grande ajuda nesse campo.

No entanto, para converteres, podes utilizar ferramentas online. Deves ter em atenção, que esse artigo tem outro relacionado (nomeadamente uma classe)..daí poderes não perceber algumas coisas.

O código, portanto, que deves ter em atenção é:

Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application();;
Microsoft.Office.Interop.Excel.Range excelRange;
// excelApp.Cells[line, column] = value
excelApp.Cells[2 + i, 1] = dS.Tables[0].Rows[i].ItemArray[0].ToString();
excelApp.Visible = true;

http://www.developerfusion.com/tools/convert/csharp-to-vb/

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.