如何在vb.net中的gridview导出后减lessExcel工作簿的权重

我有以下代码导出我的GridView在Excel中:

Protected Sub ExportToExcel(sender As Object, e As EventArgs) Handles ExportExcel.Click Try Response.Clear() Response.Buffer = True Response.AddHeader("content-disposition", "attachment;filename=ExportEthias.xls") Response.Charset = "" Response.ContentType = "application/vnd.ms-excel" Using sw As New StringWriter() Dim hw As New HtmlTextWriter(sw) GvActifs.BackColor = Color.White For Each cell As TableCell In GvActifs.HeaderRow.Cells cell.BackColor = Color.DarkBlue cell.BorderStyle = BorderStyle.Solid cell.ForeColor = Color.White Next For Each row As GridViewRow In GvActifs.Rows row.BackColor = Color.White For Each cell As TableCell In row.Cells If row.RowIndex Mod 2 = 0 Then cell.BackColor = GvActifs.AlternatingRowStyle.BackColor Else cell.BackColor = GvActifs.RowStyle.BackColor End If cell.CssClass = "textmode" cell.BorderStyle = BorderStyle.Solid Next Next GvActifs.RenderControl(hw) 'Le format de base est le texte pour éviter les problèmes d'arrondis des nombres Dim style As String = "<style> .textmode { } </style>" Response.Write(style) Response.Output.Write(sw.ToString()) Response.Flush() Response.End() End Using Catch ex As Exception lblMessage.Text = "Erreur export Excel : " & ex.Message End Try End Sub Public Overrides Sub VerifyRenderingInServerForm(control As Control) ' Verifies that the control is rendered End Sub 

它工作正常,但Excel为5K行做14.7 MB。 这是很多:)如何减less在Excel导出的重量?

提前致谢。

我发现,当你做了很多格式化的Excel文件膨胀。 即使稍后删除格式(颜色,字体),也会使文件大小变大。 我看到你正在格式化单个单元格。 如果您设法以可以同时格式化一系列单元格的方式编写代码,则文件大小将显着减less。 这是因为这个(伪代码):

Cell A1: Bold Cell A2: Bold Cell A3: Bold ... Cell Z3: Bold

需要更多的空间来存储在一个文件比:

Range A1-Z5: Bold

在这部分代码中:

 If row.RowIndex Mod 2 = 0 Then cell.BackColor = GvActifs.AlternatingRowStyle.BackColor Else 

即使您所做的是交替排列颜色,您仍在为每个单独的单元格着色。 那么为什么不用颜色来代替呢?

HTH!