在asp.net下载时调整excel文件的大小?

时间:2012-03-26 04:08:09

标签: c# asp.net vb.net

我正在使用NOPI库创建Excel文件下载。我有很多数据显示在Excel文件中,需要很长时间和大文件大小。

无论如何我们可以在下载时减少Excel文件大小吗?现在文件大小为32 MB想要这个大小。

当前代码:

If sqlDs.Tables(0).Rows.Count > 0 Then
    Dim dtExcel As DataTable = sqlDs.Tables(0)
    For Each column In dtExcel.Columns
        row.CreateCell(j).SetCellValue(column.ColumnName)
        'sheet1.AutoSizeColumn(j)
        sheet1.SetColumnWidth(j, 500)
        j = (j + 1)
     Next
     Dim iRow As Integer = 2
     'Double Cell Style
     Dim cellDoubleStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
     cellDoubleStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#,##0.00")

     'Integer Cell Style
     Dim cellIntStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
     cellIntStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("0")

     'Date Cell Style
     Dim cellDateStyle As HSSFCellStyle = hssfworkbook.CreateCellStyle
     cellDateStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat(Format("m/d/yy"))

     For Each dr As DataRow In dtExcel.Rows
         j = 0
         Dim value As Double
         Dim colInt As Integer
         row = sheet1.CreateRow(iRow)
         For Each col As DataColumn In dtExcel.Columns
             If Double.TryParse(dr(col).ToString, value) Then
                Dim cell As HSSFCell = row.CreateCell(j)
                If dr(col).ToString.Contains(".") Then
                   cell.SetCellValue(value)
                   cell.CellStyle = cellDoubleStyle
                Else
                   cell.SetCellValue(value)
                   cell.CellStyle = cellIntStyle
                End If                        '
             ElseIf IsDate(dr(col).ToString) Then
                   Dim cell As HSSFCell = row.CreateCell(j)
                   Dim dt As New DateTime
                   DateTime.TryParse(dr(col).ToString, dt)
                   cell.SetCellValue(dt)

                   cell.CellStyle = cellDateStyle
             ElseIf Integer.TryParse(dr(col).ToString, colInt) Then
                   Dim cell As HSSFCell = row.CreateCell(j)
                   cell.SetCellValue(colInt)
             Else
                   row.CreateCell(j).SetCellValue(dr(col).ToString)
                  'sheet1.AutoSizeColumn(j)
             End If
             j = j + 1
          Next
          iRow = iRow + 1
       Next
    End If

1 个答案:

答案 0 :(得分:0)

如果您不使用xslx格式,可以考虑在发送到响应流之前打包文件。由于excel文档的大小与您存储在其中的数据量有关,因此您可能无法在文档中对其进行大量减少。