当前位置:网站首页>The print area becomes smaller after epplus copies the template
The print area becomes smaller after epplus copies the template
2022-06-25 04:50:00 【firechun】
stay ASP.NET MVC Core in , When the user needs to print , From what was done in advance Excel Put... In the template Sheet copied , Then fill in the data , Finally, the file stream is returned , use epplus Realization , The key codes are as follows :
using var template = new ExcelPackage(new FileInfo(templateFile));
using var stream = new MemoryStream();
using var package = new ExcelPackage(stream);
var sheet = package.Workbook.Worksheets.Add(" The report ", template.Workbook.Worksheets[0]);
// Fill in the data , A little
package.Save();
return File(stream.GetBuffer(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{fileName}.xlsx");
There's no problem with the code itself , There is no problem with the data and format in the generated file . But when printing, I found , Set the print area in the template file , In the new file “ Bigger ”, That is, a page in the template file A4 What the paper just finished printing , In the new file , a sheet A4 The paper won't print out .
Compare the template file with the new file , The print settings are exactly the same . Because of Excel Not familiar with ,epplus There is no complete documentation , So it's not clear to copy one Sheet To another file , To keep everything the same , Are there any other relevant settings to be handled .
Just copy one Sheet There will be problems. , There should be no problem copying the entire file ? So follow this idea to modify the code :
using var template = new ExcelPackage(new FileInfo(templateFile));
using var stream = new MemoryStream();
// Copy the entire file to the memory stream
template.SaveAs(stream);
using var package = new ExcelPackage(stream);
// Fill in the data , A little
package.Save();
return File(stream.GetBuffer(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{fileName}.xlsx");
There is no problem printing , Another problem arises : The filled data and other modifications have not been saved ! In short , Namely package.Save() This line of code doesn't seem to call .
The final solution is as follows :
using var template = new ExcelPackage(new FileInfo(templateFile));
using var stream = new MemoryStream();
// Copy the entire file to the memory stream
template.SaveAs(stream);
using var package = new ExcelPackage(stream);
// Fill in the data , A little
// You must create a new memory stream , And use SaveAs Save the contents to this memory stream , It's strange .
using var outputStream = new MemoryStream();
package.SaveAs(outputStream);
return File(outputStream.GetBuffer(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{fileName}.xlsx");
Due to lack of documentation , The problem was solved , But the reason is still not clear .
边栏推荐
猜你喜欢
随机推荐
Paper notes: multi label learning ESMC (I don't understand it, but I haven't written it yet, so I'll put it here for a place temporarily)
我的IC之旅——资深芯片设计验证工程师成长——“胡”说IC工程师完美进阶
《QDebug 2022年6月》
Code scanning payment flow chart of Alipay payment function developed by PHP
Wechat likes to pay attention to the solution of invalid automatic reply
「 每日一练,快乐水题 」1108. IP 地址无效化
Php7.2 add JPEG extension
Immutable学习之路----告别传统拷贝
JS, BOM, DOM (VI)
buuctf(re)
Startup mode of SoC verification environment
Deep learning - several types of learning
Xiaobai learns MySQL - Statistical 'opportunism'
jsz中的join()
Part I Verilog quick start
dotnet-exec 0.4.0 released
【Keil】ADuCM4050官方库的GPIO输出宏定义
Response (XI)
Join() in JSZ
Gbase 8s memory management








