【问题标题】:When I download the Excel File from the website I am unable to see it's contents当我从网站下载 Excel 文件时,我看不到它的内容
【发布时间】:2023-03-08 04:12:02
【问题描述】:

我在 Asp.net 中有一个网站,其中我实现了一个以 Excel 格式存储 GridView 的按钮,但是当我打开文件时,我无法看到 GridView 的实际内容,我只能看到列标题。通过 NuGet 包管理器安装 EPPlus 后,我使用了以下三个导入。

using OfficeOpenXml;
using System.IO;
using WebFormsTest.Models;

我后面的代码如下

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    If Not IsPostBack Then
        Dim GetProducts As Object = Nothing
        GridView6.DataSource = GetProducts
        GridView6.DataBind()
    End If

End Sub

Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click

    Response.Clear()
    Dim products = GetProducts()
    GridView6.DataSource = products
    GridView6.DataBind()
    Dim excel As ExcelPackage = New ExcelPackage
    Dim workSheet = excel.Workbook.Worksheets.Add("Products")
    Dim totalCols = GridView6.Rows(0).Cells.Count
    Dim totalRows = GridView6.Rows.Count
    Dim headerRow = GridView6.HeaderRow
    worksheet.Cells["A1"].LoadFromCollection(GetProducts())
    Dim memoryStream = New MemoryStream
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
    Response.AddHeader("content-disposition", "attachment;  filename=products.xlsx")
    excel.SaveAs(memoryStream)
    memoryStream.WriteTo(Response.OutputStream)
    Response.Flush()
    Response.End()
End Sub



Public Function GetProducts() As List(Of Product)

End Function

我的 Aspx 按钮如下所示

<asp:Button ID="Button3" runat="server" Text="EXPORT RTD TO EXCEL" onclick="Button3_Click" BackColor="#FF9966"  CssClass="btn btn-large" Font-Bold="True"/>

【问题讨论】:

    标签: asp.net excel vb.net gridview


    【解决方案1】:

    编辑后面的代码解决了这个问题,我们可以通过 Nuget 包管理器下载 ClosedXML 以便它包含以下导入

    Imports ClosedXML.Excel
    
    
    Protected Sub Button3_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button3.Click
        Dim dt As New DataTable("GridView_Data")
        For Each cell As TableCell In GridView5.HeaderRow.Cells
            dt.Columns.Add(cell.Text)
        Next
        For Each row As GridViewRow In GridView5.Rows
            dt.Rows.Add()
            For i As Integer = 0 To row.Cells.Count - 1
                dt.Rows(dt.Rows.Count - 1)(i) = row.Cells(i).Text
            Next
        Next
        Dim wb As New XLWorkbook
        wb.Worksheets.Add(dt)
        Response.Clear()
        Response.Buffer = True
        Response.Charset = ""
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("content-disposition", "attachment;filename=GridViewPOLQA.xlsx")
        Using MyMemoryStream As New MemoryStream()
            wb.SaveAs(MyMemoryStream)
            MyMemoryStream.WriteTo(Response.OutputStream)
            Response.Flush()
            Response.[End]()
        End Using
    End Sub
    

    【讨论】:

      【解决方案2】:

      不知道products的数据类型是什么,为什么不直接绑定到sheet呢?

      worksheet.Cells["A1"].LoadFromCollection(GetProducts());
      

      并且您在Button3_Click 上看不到 GridView 的内容,因为您正在向客户端发送 Excel 文件(通过设置 Response.ContentType),而不是在 GridView 中填充了 @987654326 的更新的 html 页面@。

      从 C# 翻译成 vb 的快速演示

      Dim excelPackage As ExcelPackage = New ExcelPackage
      Dim worksheet As ExcelWorksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1")
      
      worksheet.Cells("A1").LoadFromCollection(GetProducts())
      
      Dim bin() As Byte = excelPackage.GetAsByteArray
      
      Response.ClearHeaders
      Response.Clear
      Response.Buffer = true
      Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
      Response.AddHeader("content-length", bin.Length.ToString)
      Response.AddHeader("content-disposition", "attachment; filename=""mySheet.xlsx"""")", Response.OutputStream.Write(bin, 0, bin.Length))
      Response.Flush
      HttpContext.Current.ApplicationInstance.CompleteRequest
      

      【讨论】:

      • 当我用 worksheet.Cells["A1"].LoadFromCollection(products) 替换 workSheet.Cells(1, i).Value = headerRow.Cells((i - 1)).Text 时,它给了我消息属性访问必须分配给属性或使用它的值。
      • 你还在使用循环吗?因为在我的示例中,这一行就足够了。见stackoverflow.com/documentation/epplus/8223/…
      • 我删除了循环仍然拍摄错误属性访问必须分配给属性或使用它的值
      • 删除所有代码并像我评论中的链接一样重新开始。如果您获得带有books 的excel 表,请尝试。如果是这样,请逐步扩展,直到获得所需的结果。
      • 好的,谢谢您的建议,我会遵循并更新您。
      猜你喜欢
      • 2015-11-12
      • 2020-09-13
      • 1970-01-01
      • 2012-06-02
      • 1970-01-01
      • 2014-06-24
      • 2021-07-10
      • 2020-11-23
      • 2013-05-08
      相关资源
      最近更新 更多