当我从网站下载Excel文件时,我无法看到它的内容

时间:2017-07-26 18:49:20

标签: asp.net excel vb.net gridview

我在Asp.net有一个网站,其中我实现了一个以Excel格式存储GridView的按钮,但是当我打开文件时,我无法看到GridView的实际内容,我只能看到列标题。通过NuGet Package Manager安装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"/>

2 个答案:

答案 0 :(得分:0)

我不知道products的数据类型是什么,但为什么不将它直接绑定到工作表?

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

你没有在Button3_Click上看到GridView的内容,因为你正在向客户端发送一个Excel文件(通过设置Response.ContentType),而不是一个更新的html页面,其中GridView已被填写products

快速演示从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

答案 1 :(得分:0)

编辑以下代码解决了这个问题,通过Nuget Package Manager,我们可以下载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