当前位置:网站首页>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 .
边栏推荐
- DMA double buffer mode of stm32
- Which programming language is the most cumbersome to implement Hello world?
- [esp32 learning path 6 - Flash encryption]
- Chapter IX app project test (2) test tools
- Join() in JSZ
- Méthode de récupération des données d'ouverture du disque dur à l'état solide
- Cookie & session & JSP (XII)
- Upgrade PHP to php7 The impact of X (2), the obsolescence of mcrypt decryption
- buuctf(pwn)
- 执行SQL响应比较慢,你有哪些排查思路?
猜你喜欢
随机推荐
DMA double buffer mode of stm32
华为鸿蒙开发第四课
leetcode1221. Split balance string
Machine learning deep learning -- Vectorization
2.0springmvc uses restful
基于Cortex-M3、M4的精准延时(系统定时器SysTick延时,可用于STM32、ADuCM4050等)
大话云原生数据库中的存算分离
绝了!自动点赞,我用 PyAutoGUI!
great! Auto like, I use pyautogui!
Upgrade PHP to php7 The impact of X (I). The problem of session retention. Keep login
[Flink] problems and solutions of the continuous growth of checkpoint size in rocksdb incremental mode
leetcode1221. 分割平衡字符串
Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影
Heavy broadcast | phase shift method + mathematical principle derivation of multi frequency heterodyne + implementation
为什么TCP握手刚刚好是3次呢?
机器学习深度学习——向量化
写shell脚本报错总结
执行SQL响应比较慢,你有哪些排查思路?
OOP stack class template (template +ds)
js的call()和apply()
![[untitled]](/img/68/5e711f7c473dcea54a56f7b7e48604.png)








