当前位置:网站首页>大规模后台导出Excel无法并发
大规模后台导出Excel无法并发
2022-07-23 05:37:00 【王者之路001】
近日,完成一个功能。需要后台在数据库导出数据,然后,生成Excel发送给前端。
原来的程序,是C#写的,采取简易原则,继续在原有工程上实现。
碰到的第一个问题是,原有的ApiController不支持Response语法。于是,改为Controller实现。
第二个问题,是Excel的导出库。
最开始,试图用CSV作为Excel导出,发现部分数据会存在格式问题,于是,最终采用了Excel导出库。最后,综合以后,选用NPOI库。样例代码如下:
public class FileDownController : Controller
{
private void createExport(List<AccountInfoAndFee> list, out string strRealPath)
{
HSSFWorkbook workbook = new HSSFWorkbook();
FileStream fileNum = null;
try
{
// 1.检测是否存在根文件夹,若不存在就建立个文件夹
if (!Directory.Exists("需要保存的文件路径"))
{
Directory.CreateDirectory(CommonFiles.FILE_ROOT_PATH + id);
}
strRealPath = 实际文件路径; //需要更改
// 工作簿
ISheet sheet = workbook.CreateSheet("数据");
// 工作表
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++)
{
//数据处理过程
IRow rowTmp = sheet.CreateRow(i + 1);
rowTmp.CreateCell(0).SetCellValue(""); //后续数据同样处理
}
// 4.生成文件
fileNum = new FileStream(strRealPath + fileName + ".xls", FileMode.Create);
workbook.Write(fileNum);
fileNum.Flush();
fileNum.Close();
workbook.Clear();
workbook.Close();
Log.Error("文件" + strRealPath + fileName + " 总行数: " + 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);
}
}在使用NPOI时,发现一个问题为:HSSFWorkbook 只支持65535行。首先考虑的是,换为XSSFWorkbook。但发现,即使是XSSFWorkbook,依然也存在文件最多数量限制。于是解决方案为:还用HSSFWorkbook,但到65535行时,就生成一个文件。
但这样的问题就又来了,就HTTP传输为1个文件。方法为:压缩Excel文件为1个压缩文件。
但依然存在很重要的问题:那就是,NPOI在写大文件时,只支持一个写过程,否则会出错。
突然想到《Hadoop原理》上的话:人们发现,即使是最大的大象,它的拉车能力也是有限的;但人们不会培养更大的大象,而是让多个大象来拉车。
于是,方法就提供出来了:提供多个服务器,分别为多个请求提供服务。
于是,问题解决。
边栏推荐
- Cadence(九)17.4规则与间距设置
- [ROS advanced chapter] Lesson 8 syntax explanation of URDF file
- Wechat applet package wx.request
- Detailed explanation of structure
- Redis source code and design analysis -- 14. Database implementation
- C language n battle -- structure (VII)
- [swift bug] Xcode prompt error running playground: failed to prepare for communication with playground
- C language explanation series - understanding of functions (1) library functions, user-defined functions
- Concepts et différences de bits, bits, octets et mots
- The 12th Blue Bridge Cup embedded design and development project
猜你喜欢

12 open source background management systems suitable for outsourcing projects

动态内存管理

PMP practice once a day | don't get lost in the exam -7.22

Li Nan, CTO of Yunqian Technology: technology and technical people, coevolution with digitalization

Redis源码与设计剖析 -- 9.字符串对象

IO should know and should know
TS type gymnastics intermediate type gymnastics challenge closing battle

阿里云对象存储服务OSS前后联调

Error reporting when installing opencv in Anaconda virtual environment

Analyse du code source et de la conception de redis - - 7. Liste rapide
随机推荐
Meyer Burger梅耶博格西门子工控机维修及机床养护
Gerrit 使用操作手册
Two strategies for building AI products / businesses (by Andrew ng)
Mysql数据库基础
Analyse du code source et de la conception de redis - - 7. Liste rapide
Niuke brush questions - Sword finger offer (phase II)
PMP practice once a day | don't get lost in the exam -7.22
Single sign on - how to unify the expiration time of session between authentication server and client
Redis source code and design analysis -- 7. Quick list
C1 -- vivado configuration vs code text editor environment 2022-07-21
Why does MySQL index use b+ tree?
Three implementation methods of C # client program calling external program
《天幕红尘》笔记与思考(三)只要条件具足了,结果自然来
Deploy metersphere
华为高层谈 35 岁危机,程序员如何破年龄之忧?
Updated again, idea 2022.2 officially released
Kubernetes technology and Architecture (VI)
An accident caused by MySQL misoperation, and "high availability" is not working well
An analysis of the CPU explosion of an intelligent transportation background service in.Net
十年架构五年生活-02第一份工作