生成导出EXCEL太慢了,一万条数据要10分钟左右

菜鸟111号 菜鸟111号 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using ICSharpCode;
using NPOI.XSSF.UserModel;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using System.Data;
using System.Reflection;
using System.Text.RegularExpressions;

public class WordHelp
{

#region Excel列类型获取列的值
/// <summary>
/// 根据Excel列类型获取列的值
/// </summary>
/// <param name="cell">Excel列</param>
/// <returns></returns>
private static string GetCellValue(ICell cell)
{
if (cell == null)
return string.Empty;
switch (cell.CellType)
{
case CellType.Blank:
return string.Empty;
case CellType.Boolean:
return cell.BooleanCellValue.ToString();
case CellType.Error:
return cell.ErrorCellValue.ToString();
case CellType.Numeric:
case CellType.Unknown:
default:
return cell.ToString();//This is a trick to get the correct value of the cell. NumericCellValue will return a numeric value no matter the cell value is a date or a number
case CellType.String:
return cell.StringCellValue;
case CellType.Formula:
try
{
XSSFFormulaEvaluator e = new XSSFFormulaEvaluator(cell.Sheet.Workbook);
e.EvaluateInCell(cell);
return cell.ToString();
}
catch
{
return cell.NumericCellValue.ToString();
}
}
}
#endregion

//数据量过大会造成响应时间过长
/// <summary>
/// 自动设置Excel列宽
/// </summary>
/// <param name="sheet">Excel表</param>
//private static void AutoSizeColumns(ISheet sheet)
//{
// if (sheet.PhysicalNumberOfRows > 0)
// {
// IRow headerRow = sheet.GetRow(0);

// for (int i = 0, l = headerRow.LastCellNum; i < l; i++)
// {
// sheet.AutoSizeColumn(i);
// }
// }
//}

/// <summary>
/// 保存Excel文档流到文件
/// </summary>
/// <param name="ms">Excel文档流</param>
/// <param name="fileName">文件名</param>
private static void SaveToFile(MemoryStream ms, string fileName)
{
using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
{
byte[] data = ms.ToArray();

fs.Write(data, 0, data.Length);
fs.Flush();

data = null;
}
}

///// <summary>
///// 输出文件到浏览器
///// </summary>
///// <param name="ms">Excel文档流</param>
///// <param name="context">HTTP上下文</param>
///// <param name="fileName">文件名</param>
//private static void RenderToBrowser(MemoryStream ms, HttpContext context, string fileName)
//{
// if (context.Request.Browser.Browser == "IE")
// fileName = HttpUtility.UrlEncode(fileName);
// context.Response.AddHeader("Content-Disposition", "attachment;fileName=" + fileName);
// context.Response.BinaryWrite(ms.ToArray());
//}

/// <summary>
/// DataReader转换成Excel文档流
/// </summary>
/// <param name="reader"></param>
/// <returns></returns>
public static MemoryStream RenderToExcel(IDataReader reader)
{
MemoryStream ms = new MemoryStream();

using (reader)
{
IWorkbook workbook = new XSSFWorkbook();

ISheet sheet = workbook.CreateSheet();

IRow headerRow = sheet.CreateRow(0);
int cellCount = reader.FieldCount;

// handling header.
for (int i = 0; i < cellCount; i++)
{
headerRow.CreateCell(i).SetCellValue(reader.GetName(i));
}

// handling value.
int rowIndex = 1;
while (reader.Read())
{
IRow dataRow = sheet.CreateRow(rowIndex);

for (int i = 0; i < cellCount; i++)
{
dataRow.CreateCell(i).SetCellValue(reader[i].ToString());
}

rowIndex++;
}

//AutoSizeColumns(sheet);

workbook.Write(ms);
ms.Flush();
ms.Position = 0;
}

return ms;
}

/// <summary>
/// DataReader转换成Excel文档流,并保存到文件
/// </summary>
/// <param name="reader"></param>
/// <param name="fileName">保存的路径</param>
public static void RenderToExcel(IDataReader reader, string fileName)
{
using (MemoryStream ms = RenderToExcel(reader))
{
SaveToFile(ms, fileName);
}
}

///// <summary>
///// DataReader转换成Excel文档流,并输出到客户端
///// </summary>
///// <param name="reader"></param>
///// <param name="context">HTTP上下文</param>
///// <param name="fileName">输出的文件名</param>
//public static void RenderToExcel(IDataReader reader, HttpContext context, string fileName)
//{
// using (MemoryStream ms = RenderToExcel(reader))
// {
// RenderToBrowser(ms, context, fileName);
// }
//}

/// <summary>
/// DataTable转换成Excel文档流
/// </summary>
/// <param name="table"></param>
/// <returns></returns>
public static MemoryStream RenderToExcel(DataTable table)
{
MemoryStream ms = new MemoryStream();

using (table)
{
IWorkbook workbook = new XSSFWorkbook();

ISheet sheet = workbook.CreateSheet();

IRow headerRow = sheet.CreateRow(0);

// handling header.
foreach (DataColumn column in table.Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value
// handling value.
int rowIndex = 1;

foreach (DataRow row in table.Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);

foreach (DataColumn column in table.Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}

rowIndex++;
}
//AutoSizeColumns(sheet);

workbook.Write(ms);
ms.Flush();
//ms.Position = 0;
}

return ms;
}

/// <summary>
/// DatasSet转换成Excel文档流
/// </summary>
/// <param name="sourceDs">DataSet</param>
/// <param name="sheetName">工作表命,按逗号分隔</param>
/// <returns></returns>
private static MemoryStream RenderToExcel(DataSet sourceDs, string sheetName)
{
IWorkbook workbook = new XSSFWorkbook();
MemoryStream ms = new MemoryStream();
string[] sheetNames = sheetName.Split(',');
for (int i = 0; i < sheetNames.Length; i++)
{
ISheet sheet = workbook.CreateSheet(sheetNames[i]);
IRow headerRow = sheet.CreateRow(0);
// handling header.
foreach (DataColumn column in sourceDs.Tables[i].Columns)
headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName);
// handling value.
int rowIndex = 1;
foreach (DataRow row in sourceDs.Tables[i].Rows)
{
IRow dataRow = sheet.CreateRow(rowIndex);

foreach (DataColumn column in sourceDs.Tables[i].Columns)
{
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
}
rowIndex++;
}
}
workbook.Write(ms);
ms.Flush();
//ms.Position = 0;
//workbook = null;
return ms;
}

/// <summary>
/// DataTable转换成Excel文档流,并保存到文件
/// </summary>
/// <param name="table"></param>
/// <param name="fileName">保存的路径</param>
public static void RenderToExcel(DataTable table, string fileName)
{
using (MemoryStream ms = RenderToExcel(table))
{
SaveToFile(ms, fileName);
}
}

///// <summary>
///// DataTable转换成Excel文档流,并输出到客户端
///// </summary>
///// <param name="table"></param>
///// <param name="response"></param>
///// <param name="fileName">输出的文件名</param>
//public static void RenderToExcel(DataTable table, HttpContext context, string fileName)
//{
// using (MemoryStream ms = RenderToExcel(table))
// {
// RenderToBrowser(ms, context, fileName);
// }
//}

#region Excel验证是否有数据
/// <summary>
/// Excel文档流是否有数据
/// </summary>
/// <param name="excelFileStream">Excel文档流</param>
/// <returns></returns>
public static bool HasData(Stream excelFileStream)
{
return HasData(excelFileStream, 0);
}

/// <summary>
/// Excel文档流是否有数据
/// </summary>
/// <param name="
...全文

相关推荐

java导出30万数据量的excel(采用生成多个excel,最后打包zip)

30万的数据量,生成多个excel最后打包成zip下载,是工程,直接导入就行

咖啡加糖_

数据库大量数据导出Excel

应客户要求,导出数据库中的数据为Excel,只在一个Sheet中,Excel中的内容要与数据库里的内容一致(主要针对日期型数据)。 由于之前接触过POI,对POI的一些特性还是有一定的了解的,因此顺其自然的用POI去解决这个问题。POI3.8版本之前的版本处理大量数据的导出Excel效果不是很理想,主要在与Excel2003版本单个Sheet的行限制为65536,大量数据的导出得分多个Sheet,针对这一点,客户就不会满意。其次,在实验过程中,大数据量的导出很容易引发内存溢出,调整JVM的内存大小治标不治本。很多人建议保存为.CSV格式的文件。不过,.CSV方式导出也存在问题:首先,如果用excel来打开csv,超过65536行的数据都会看不见,这是Excel程序的问题。其次,如果要导出一个身份证号码,手机号码等纯数字构成的字符串,在excel中打开csv时,这些字段很容易被识别成数字,被误处理。 POI3.8以后的版本支持Excel2007高版本,单个Sheet的行数可达到百万,针对内存溢出问题,可通过设置内存数据保留数,每当计数到指定的数值时,刷新数据到硬盘,清理内存。

shanhuijava

C#导出Excel(DataTable生成Excel)

使用C#利用Aspose.Cells.dll导出Excel. 优化了之前上传的导出Excel方法,希望能够对大家有帮助 大家也可以根据自己需求修改导出Excel方法

行侠者

jfreechart生成图片及导出pdf 、excel

jfreechat生成柱状图,饼状图,折线图,导出饼状图到pdf,excel.

勤能补拙笨鸟先飞

plsql导出数据到excel的三种方法

plsql导出数据到excel常用的三种方法,(1)直接导出;(2)转换导出方式;(3)导出后转换格式;

a385528896
评论 16
收藏 已收藏
分享
返回顶部
菜鸟111号
.NET社区 62,275 成员 668,967 内容