分批汇出EXCEL

EXCEL 汇出的时候,如果一时间汇出大量的数据的话,会影响服务器的性能及效率,故分批汇出,每一批50000条数据(每页EXCEL支持65536行),具体实现如下

Public Shared Sub ExportBatch(dtSource As DataTable, strFileName As String)
Dim workbook As New HSSFWorkbook()
CreateFolder(strFileName.Substring(0, strFileName.LastIndexOf("\")))
Dim iCount As Integer = 0

‘每批汇出行数
Dim sheetRows As Integer = 50000
For c As Integer = 0 To dtSource.Rows.Count - 1 Step sheetRows
‘For c As Integer = 0 To 10
If c = iCount * sheetRows Then
iCount = iCount + 1
Dim sheet As HSSFSheet = workbook.CreateSheet()

‘填充表头
Dim dataRow As HSSFRow = sheet.CreateRow(0)
For Each column As DataColumn In dtSource.Columns
dataRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName)
Next

‘填充内容
‘判断是否是最后一页,如果不是的话,取50000的倍数,如果是,则取datatable的最大条数
Dim rowCounts As Integer = IIf(dtSource.Rows.Count > iCount * sheetRows, iCount * sheetRows, dtSource.Rows.Count)
‘每一批(第一页)都是由第一行开始
Dim rowIndex As Integer = 1
For i As Integer = c To rowCounts - 1
dataRow = sheet.CreateRow(rowIndex)
For j As Integer = 0 To dtSource.Columns.Count - 1
dataRow.CreateCell(j).SetCellValue(dtSource.Rows(i)(j).ToString())
Next
rowIndex += 1
Next

‘保存
Using ms As New MemoryStream()
Using fs As New FileStream(strFileName, FileMode.OpenOrCreate, FileAccess.Write)
workbook.Write(ms)
ms.Flush()
ms.Position = 0
Dim data As Byte() = ms.ToArray()
fs.Write(data, 0, data.Length)
fs.Flush()
End Using
End Using
End If

System.Threading.Thread.Sleep(10000)

Next
End Sub

时间: 2024-11-13 06:34:55

分批汇出EXCEL的相关文章

gridview汇出EXCEL (ExportGridViewToExcel(dt, HttpContext.Current.Response);)

调用 ExportGridViewToExcel(dt, HttpContext.Current.Response); private void ExportGridViewToExcel(DataTable tb, HttpResponse response) { response.Clear(); response.Charset = ""; response.AppendHeader("Content-Disposition", "attachmen

c# npoi分批往excel追加数据

直接贴代码: using DongYang.Core.Model.Domain; using DongYang.Core.Utils; using NLog; using NPOI.XSSF.UserModel; using System; using System.Collections.Generic; using System.Data; using System.Diagnostics; using System.IO; using System.Threading; namespace

ERP问题解决

1.Program stopped at 'cin_aint340.4gl', line number 1121.FORMS statement error number -1338.The function 'cl_exe_rep' has not been defined in any module in the program. 解决办法:1.做报表打印格式时,在azzi988里面添加报表元件参数时,必须要重新下载单据程序并且上传,          2.上传后第一次运行单据作业会出现全英

Excel 导入 分批导入

tm.begin(); sm.begin(); try { result = excel.readE(headKeyV,path); for (int index = 1; index < result.size(); index++) { tempMap = result.get(index); if(StringUtils.isEmpty(tempMap.get("XREDID_DUE_DATE"))&& !StringUtils.isEmpty(tempMa

有gridview汇出word和excel

private void Export(GridView _gv, string filetype, string FileName)    {        if (filetype == "1")        {            string style = @"<style> .text { mso-number-format:\@; } </script> ";            Response.Clear();    

asp.net读取Excel数据

先通过控件FileUpload获取excel文件路径 protected void btnReadExcelFromFileUpload_Click(object sender, EventArgs e) { if (fupExcel.PostedFile.ContentLength > 0) { //获取全路径 string fullFileName = fupExcel.PostedFile.FileName.ToString(); //获取文件名 string fileName = fup

将excel导入sql-连接odbc

数据库连接字符串 Excel2000-2003: string connStr = "Microsoft.Jet.Oledb.4.0;Data Source='c:\test.xls';Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";"; Excel2007: string connStr = "Microsoft.Ace.OleDb.12.0;Data Source='c:\test.xlsx';Exte

利用poi3.9做的excel导出工具

一.先看看所生成的文件效果图 二.准备 本文需要六个jar包: dom4j-1.6.1.jar ojdbc14.jar poi-3.9-20121203.jar poi-ooxml-3.9-20121203.jar poi-ooxml-schemas-3.9-20121203.jar xmlbeans-2.3.0.jar 除了ojdbc14.jar是用来访问数据库的,其它的都是导出excel所需要的poi相关jar包. 注:本文是以poi3.9版本写的,利用了SXSSFWorkbook这个Wor

JAVA关于POI导出Excel内存溢出的解决方案

JAVA关于POI导出Excel内存溢出的解决方案 在我们使用JAVA开发过程中,经常要导出查询获得的数据,这些数据一般情况下都是以Excel存储的,因此我们在导出数据的时候要使用JAVA的POI库,其主要是对各种windows平台的数据格式进行操作,在这里,我们是对Excel操作. 生成Excel的过程原理是这样的,首先,我们对数据库进行查询,获取相应的结果集,一般是list集合,然后生成Workbook对象,根据生成的Workbook对象获取sheet对象,根据此sheet对象获取Row对象