0

我知道这已经在此处和网络上的一千个其他站点上得到了回答,但是我尝试过的每个站点都无法按我的意愿工作。我只想导出一个数据表,而不是从中运行代码的页面,并且我想这样做而无需下载第 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 中?

4

3 回答 3

2

我通常这样做并且它对我有用(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();
            }
        }
于 2013-09-09T19:36:14.003 回答
1

这应该适用于 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
于 2013-09-09T19:01:57.527 回答
0

没有某种第 3 方 dll 的唯一方法是编写一个 csv 文件。

用分号分隔的列,用 /n 分隔的行

于 2013-09-09T19:55:01.530 回答