Export Database Table to XLSX without Interop

Asked

Viewed 387 times

1

In the code below was assigned a Query for the variable sQuery.

I need to somehow export this Query for Excel using some resource available to C# Webform other than Interop.

 Dim da = New OleDbDataAdapter(sQuery, ConnectionString)
    Dim dt = New DataTable()
    da.Fill(dt)
    Dim GridView1 As New GridView()
    Dim iTipoExport As Byte


        Dim sstr As String

        Session("Arquivo") = sNomeArquivo

        CaminhoDiretorio = Server.MapPath("~/Download/" & sNomeArquivo & ".txt")

        FileOpen(1, CaminhoDiretorio, OpenMode.Binary)
        sstr = ""
        For k As Integer = 0 To dt.Columns.Count - 1
            sstr = sstr & dt.Columns(k).ColumnName + "^"
        Next
        sstr = Mid(sstr, 1, Len(sstr) - 1)
        sstr = sstr & vbCrLf
        FilePut(1, sstr)

        For i As Integer = 0 To dt.Rows.Count - 1
            sstr = ""
            For k As Integer = 0 To dt.Columns.Count - 1
                sstr = sstr & dt.Rows(i)(k).ToString() + "^"
            Next
            sstr = Mid(sstr, 1, Len(sstr) - 1)
            sstr = sstr & vbCrLf
            FilePut(1, sstr)
        Next

        FileClose(1)
        AppExcelExport = CreateObject("Excel.Application")            
        AppExcelExport.Workbooks.OpenText(CaminhoDiretorio, StartRow:=1, DataType:=Excel.XlTextParsingType.xlDelimited, TextQualifier:=Excel.XlTextQualifier.xlTextQualifierNone, Other:=True, OtherChar:="^")
        WboExcelExport = AppExcelExport.Workbooks(1)
        WshExcelExport = WboExcelExport.Sheets(1)
        WshExcelExport.Select()
        RngExcelExport = WshExcelExport.Rows(1)
  • 1

    See if it helps https://answall.com/q/15590/101

  • Got it, I can use Epplus or Linq to Excel to convert a csv to xlsx?

  • Maybe you need to see if you can get what you need, but I think you can.

  • I believe that the options of this post are to generate an Excel, in case I need to export it from my data table.

  • The question clearly asks to convert an Excel file.

2 answers

0

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.