簡體   English   中英

如何僅使用標准.Net而不使用第三方dll將數據表僅導出到Excel?

[英]How do I export only a datatable to Excel using only standard .Net and not a 3rd party dll?

我知道這已經在這里和網絡上的其他數千個站點上得到了回答,但是我嘗試過的每個站點都無法按我希望的那樣工作。 我只想導出一個數據表,而不是我正在運行代碼的頁面,並且我想這樣做而不必下載第3方dll。 我嘗試過的每一段代碼最終都會導出我正在運行的頁面。 這是我正在使用的當前迭代...

Private Sub ExporttoExcel(table As DataTable)
  Dim attachment As String = "attachment; filename=file.xls"
  Response.ClearContent()
  Response.AddHeader("content-disposition", attachment)
  Response.ContentType = "application/vnd.ms-excel"
  Dim tab As String = ""

  For Each dc As DataColumn In table.Columns
    Response.Write(tab + dc.ColumnName)
    tab = vbTab
  Next

  Response.Write(vbLf)

  Dim i As Integer
  For Each dr As DataRow In table.Rows
    tab = ""
    For i = 0 To table.Columns.Count - 1
      Response.Write(tab & dr(i).ToString())
      tab = vbTab
    Next
    Response.Write(vbLf)
  Next
  Response.End()
End Sub

誰能解釋為什么這不起作用,或者我只能將數據表導出到Excel嗎?

我通常這樣做,並且對我有用(ds是一個DataSet,其中包含我想推送到excel的數據):

            HttpResponse response = HttpContext.Current.Response;

        // first let's clean up the response.object
        response.Clear();
        response.Charset = "";

        string filename = "TEMP/ex1.xls";

        // set the response mime type for excel
        response.ContentType = "application/vnd.ms-excel";
        response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");

        // create a string writer
        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                // instantiate a datagrid
                DataGrid dg = new DataGrid();
                dg.DataSource = ds.Tables[0];
                dg.DataBind();

                dg.RenderControl(htw);
                response.Write(sw.ToString());
                response.End();
            }
        }

這應該與EPPlus一起使用

Public Shared Function ExportToExcel(FileName As String, SheetName As String, data As DataTable) As Boolean
    Dim pck As ExcelPackage
    pck = New ExcelPackage()

    Try

        Dim ws = pck.Workbook.Worksheets.Add(SheetName)
        ws.Cells("A1").LoadFromDataTable(data, True)
        Dim excel = pck.GetAsByteArray()

        HttpContext.Current.Response.ClearHeaders()
        HttpContext.Current.Response.ClearContent()
        HttpContext.Current.Response.Clear() 'really clear it :-p
        HttpContext.Current.Response.BufferOutput = False
        HttpContext.Current.Response.ContentType = "application/octet-stream"
        HttpContext.Current.Response.AddHeader("cache-control", "max-age=0")
        HttpContext.Current.Response.AddHeader("Pragma", "public")
        HttpContext.Current.Response.AddHeader("Content-Length", excel.Length.ToString())
        HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;  filename=""" & FileName & ".xlsx""")

        HttpContext.Current.Response.BinaryWrite(excel)

        HttpContext.Current.Response.[End]()

        Return True
    Catch
        Return False
    Finally
        pck.Dispose()
    End Try
End Function

沒有某種第三方dll的唯一方法是編寫一個csv文件。

列用分號分隔,行用/ n分隔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM