我在Asp.net中有一個網站,其中我已經實現了一個按鈕,以Excel格式存儲GridView,但是當我打開文件時我無法看到GridView的實際內容,我只能看到列標題。在通過NuGet Package Manager安裝EPPlus後,我已經使用了三個以下導入。當我從網站上下載Excel文件時,我無法看到它的內容
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"/>
當我替換'workSheet.Cells(1,i).Value = headerRow.Cells((i - 1))。Text' with 'worksheet.Cells [「A1」]。LoadFromCollection(products)'這是給我屬性訪問必須分配給屬性或使用它的值。 – Snb93
你還在使用循環嗎?因爲我的例子中的那一行應該是足夠的。請參閱https://stackoverflow.com/documentation/epplus/8223/filling-the-document-with-data/26425/fill-from-collection#t=201707261918225613912 – VDWWD
我刪除了仍然拍攝錯誤屬性訪問必須分配給的循環該物業或使用其價值 – Snb93