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

本文关键字:文件 excel 调整 asp net 下载 | 更新日期: 2023-09-27 18:29:03

我正在使用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

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

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