簡體   English   中英

在asp.net中下載時調整excel文件的大小?

[英]Resize excel file while downloading in asp.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

如果您不使用xslx格式,則可以考慮在發送到響應流之前打包文件。 由於excel文檔的大小與您存儲在其中的數據量有關,因此您可能無法在文檔中減少很多內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM