当前位置:网站首页>Large scale background export excel cannot be concurrent
Large scale background export excel cannot be concurrent
2022-07-23 11:06:00 【Road of Kings 001】
In recent days, , Complete a function . You need to export data in the database in the background , then , Generate Excel Send to front end .
The original program , yes C# Written , Adopt the principle of simplicity , Continue to realize on the original project .
The first problem is , The original ApiController I won't support it Response grammar . therefore , Change it to Controller Realization .
The second question is , yes Excel Export library for .
In the beginning , Try to use CSV As Excel export , Some data will have format problems , therefore , Finally adopted Excel Export Library . Last , After synthesis , choose NPOI library . The sample code is as follows :
public class FileDownController : Controller
{
private void createExport(List<AccountInfoAndFee> list, out string strRealPath)
{
HSSFWorkbook workbook = new HSSFWorkbook();
FileStream fileNum = null;
try
{
// 1. Check whether the root folder exists , If it doesn't exist, create a folder
if (!Directory.Exists(" File path to save "))
{
Directory.CreateDirectory(CommonFiles.FILE_ROOT_PATH + id);
}
strRealPath = The actual file path ; // You need to change
// workbook
ISheet sheet = workbook.CreateSheet(" data ");
// Worksheet
IRow row = sheet.CreateRow(0);
createExcleHeader(ref row, ref sheet);
int j = 0;
int k = 0;
//while(k++ < 10)
//{
for (int i = 0; i < list.Count(); i++)
{
// Data processing
IRow rowTmp = sheet.CreateRow(i + 1);
rowTmp.CreateCell(0).SetCellValue(""); // Subsequent data are also processed
}
// 4. Generate the file
fileNum = new FileStream(strRealPath + fileName + ".xls", FileMode.Create);
workbook.Write(fileNum);
fileNum.Flush();
fileNum.Close();
workbook.Clear();
workbook.Close();
Log.Error(" file " + strRealPath + fileName + " Total number of lines : " + list.Count().ToString());
}
catch (Exception ex)
{
Log.Error(ex, ex.StackTrace);
Log.Error(ex, ex.Message);
throw ex;
}
finally
{
workbook.Close();
if (fileNum != null)
{
fileNum.Close();
}
}
}
private void getFiles(string fileSrcName)
{
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
Response.ContentType = "application/ms-excel";
Response.TransmitFile(fileSrcName);
Response.Flush();
Response.End();
removeFiles(strFilePath);
}
}In the use of NPOI when , One problem found is :HSSFWorkbook Only support 65535 That's ok . The first thing to consider is , Replace with XSSFWorkbook. But found , Even if it's XSSFWorkbook, There is still a maximum number of files . So the solution is : Also use HSSFWorkbook, But to 65535 Line time , Just generate a file .
But this problem comes again , Just HTTP Transmitted as 1 File . Method is : Compress Excel File for 1 Compressed files .
But there are still important problems : That's it ,NPOI When writing large files , Only one write process is supported , Otherwise it will go wrong .
Suddenly thought of 《Hadoop principle 》 What I said : It was found that , Even the largest elephant , Its pulling ability is also limited ; But people will not cultivate bigger elephants , Instead, let several elephants pull the cart .
therefore , Methods are provided : Provide multiple servers , Serve multiple requests separately .
therefore , Problem solving .
边栏推荐
- 【Anaconda 环境管理与包管理】
- Filter in MATLAB
- Notifier Nordic fire engine power supply maintenance and daily maintenance
- 【信息系统项目管理师】第六章 复盘进度管理知识架构
- 视、音频分开的网站内容如何合并?批量下载代码又该如何编写?
- 结构体详解
- MySql语句查询某一级节点的所有子节点
- 2.启动函数返回值的剖析
- Deploy storageclass trample record
- Heidelberg CP2000 circuit board maintenance printer host controller operation and maintenance precautions
猜你喜欢
随机推荐
Redis source code and design analysis -- 10. List object
对比redis的RDB、AOF模式的优缺点
Visual studio 2022 interesting and powerful intelligent auxiliary coding
R language uses DALEX package to explain and analyze the machine learning model built by H2O package: summary and Practice
Redis源码与设计剖析 -- 6.压缩列表
Data Lake: introduction to delta Lake
Error reporting when installing opencv in Anaconda virtual environment
C1--Vivado配置VS Code文本编辑器环境2022-07-21
Notifier Nordic fire engine power supply maintenance and daily maintenance
Redis源碼與設計剖析 -- 7.快速列錶
疫情时期加中年危机——游荡在十字街口的三个月
手风琴效果
Activiti工作流使用之项目实例
Data Lake: introduction to Apache iceberg
安装企业版pycharm以及anaconda
防止神经网络过拟合的五种方法
Notes and Thoughts on the red dust of the sky (III) as long as the conditions are sufficient, the results will come naturally
An accident caused by MySQL misoperation, and "high availability" is not working well
Cadence(九)17.4规则与间距设置
CountDownLatch的用法









