EPPlus将DataTable加载到ExcelWorksheet时出错

时间:2016-12-30 20:06:51

标签: vb.net epplus

我有以下代码,我使用 SqlDataAdapter 来填充 DataTable 。我使用 EPPlus 创建Excel包和工作表,但是当我调用 LoadFromDataTable 时,工作表中没有填充数据表中的数据。我不确定问题在哪里,但我很难过。

Protected Sub btnExportToExcel_Click(sender As Object, e As EventArgs)

    Dim dt As DataTable = New DataTable()
    Dim da As SqlDataAdapter = New SqlDataAdapter("usp_StoredProcedure", sQLConnString)
    da.SelectCommand.CommandType = CommandType.StoredProcedure
    da.SelectCommand.Parameters.AddWithValue("param1", "text")
    da.SelectCommand.Parameters.AddWithValue("param2", "text")
    da.SelectCommand.Parameters.AddWithValue("param3", "text")
    da.SelectCommand.Parameters.AddWithValue("param4", "text")
    da.SelectCommand.Parameters.AddWithValue("param5", "text")
    da.SelectCommand.Parameters.AddWithValue("param6", "text")
    da.SelectCommand.Parameters.AddWithValue("param7", "text")
    da.SelectCommand.Parameters.AddWithValue("param8", "text")

    Try
        da.Fill(dt)
    Catch ex As Exception
        ' Do Stuff
        Return
    End Try

    If dt.Rows.Count > 0 Then
        Using pck As ExcelPackage = New ExcelPackage()
            Dim ws As ExcelWorksheet = pck.Workbook.Worksheets.Add("SheetName")
            ws.Cells("A1").LoadFromDataTable(dt, True)
            Response.ClearContent()
            Response.BufferOutput = True
            Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
            Response.AddHeader("Content-Disposition", "attachment; filename=" + String.Format("FileName-{0:yyyy-MM-dd-HH-mm-ss}.xlsx", DateTime.UtcNow))
            Response.BinaryWrite(pck.GetAsByteArray())
            Response.Flush()
            Response.End()
        End Using
    End If
End Sub

0 个答案:

没有答案