2017-07-26 41 views
0

我在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"/> 

回答

0

編輯通過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 
0

給出我不知道的數據類型是什麼products,但爲什麼不綁定它直接表單?

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

因爲你要發送一個Excel文件到客戶端(通過設置Response.ContentType),不要在那裏的GridView已經充滿了products更新的HTML頁面,你是不是在Button3_Click看到GridView控件的內容。

快速演示從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 
+0

當我替換'workSheet.Cells(1,i).Value = headerRow.Cells((i - 1))。Text' with 'worksheet.Cells [「A1」]。LoadFromCollection(products)'這是給我屬性訪問必須分配給屬性或使用它的值。 – Snb93

+0

你還在使用循環嗎?因爲我的例子中的那一行應該是足夠的。請參閱https://stackoverflow.com/documentation/epplus/8223/filling-the-document-with-data/26425/fill-from-collection#t=201707261918225613912 – VDWWD

+0

我刪除了仍然拍攝錯誤屬性訪問必須分配給的循環該物業或使用其價值 – Snb93

相關問題