当前位置:网站首页>epplus复制模板后打印区域变小的问题
epplus复制模板后打印区域变小的问题
2022-06-25 04:02:00 【firechun】
在ASP.NET MVC Core中,用户需要打印时,从事先做的Excel模板中把Sheet复制过来,然后填充数据,最后返回文件流,用epplus实现,关键代码如下:
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("报告", template.Workbook.Worksheets[0]);
//填充数据,略
package.Save();
return File(stream.GetBuffer(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{fileName}.xlsx");
代码本身没有问题,生成的文件中的数据和格式也没有问题。但是打印时发现,在模板文件设置好的打印区域,在新文件中“变大了”,也就是模板文件中一张A4纸刚好打印完的内容,在新文件中,一张A4纸打印不下了。
对照模板文件和新文件,打印设置完全一样。因为对Excel并不熟悉,epplus也没有完整文档,所以不清楚复制一个Sheet到另一个文件中,要想保持所有内容一模一样,是不是还有其它相关设置要处理。
即然只复制一个Sheet会有问题,那么复制整个文件应该不会有问题吧?于是按照这个思路修改代码:
using var template = new ExcelPackage(new FileInfo(templateFile));
using var stream = new MemoryStream();
//复制整个文件到内存流中
template.SaveAs(stream);
using var package = new ExcelPackage(stream);
//填充数据,略
package.Save();
return File(stream.GetBuffer(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{fileName}.xlsx");
打印没有问题了,另一个问题又出现:填充的数据以及其它修改都没有被保存!简单地说,就是package.Save()这行代码仿佛没有调用一样。
最终解决方案如下:
using var template = new ExcelPackage(new FileInfo(templateFile));
using var stream = new MemoryStream();
//复制整个文件到内存流中
template.SaveAs(stream);
using var package = new ExcelPackage(stream);
//填充数据,略
//必须创建一个新的内存流,并且用SaveAs把内容保存到这个内存流中才可以,很奇怪。
using var outputStream = new MemoryStream();
package.SaveAs(outputStream);
return File(outputStream.GetBuffer(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", $"{fileName}.xlsx");
因为缺少文档,问题虽然解决了,但原因还是没有搞清楚。
边栏推荐
- Gbase 8s stored procedure execution and deletion
- CTF_ Web: Changan cup-2021 old but a little new & asuka
- Why PHP is not safe
- 成功解决:selenium.common.exceptions.TimeoutException: Message: timeout: Timed out receiving message from
- 【Flink】RocksDB增量模式checkpoint大小持续增长的问题及解决
- OOP stack class template (template +ds)
- Codeforces Round #802 (Div. 2) C D
- Gbase 8s memory management
- Why is the TCP handshake just 3 times?
- 【无标题】
猜你喜欢

以太网是什么要怎么连接电脑

固态硬盘开盘数据恢复的方法

unity Quad剔除背面并剔除透明部分的shader

ASEMI三相整流桥的工作原理

Php7.2 add JPEG extension

冰冰学习笔记:循环队列的实现

Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影

Record small knowledge points

Sleep more, you can lose weight. According to the latest research from the University of Chicago, sleeping more than 1 hour a day is equivalent to eating less than one fried chicken leg

Chapter IX app project test (2) test tools
随机推荐
Leader: who can use redis expired monitoring to close orders and get out of here!
js中的concat()
绝了!自动点赞,我用 PyAutoGUI!
华为鸿蒙开发第四课
Method of opening data recovery of solid state disk
[untitled]
dotnet-exec 0.4.0 released
记录小知识点
File upload vulnerability shooting range upload labs learning (pass1-pass5)
How do the defi protocols perform under this round of stress test?
At the age of 30, I began to learn programming by myself. Is it still time for me to have difficulties at home?
win11蓝牙无法连接怎么办?win11蓝牙无法连接的解决方法
GBASE 8s存储过程执行和删除
高效的NoSQL数据库服务Amozon DynamoDB体验分享
电脑的dwg文件怎么打开
Gbase 8s overall architecture
Kotlin Compose 完善toDo项目 Surface 渲染背景 与阴影
Upgrade PHP to php7 The impact of X (2), the obsolescence of mcrypt decryption
Record the problem of C # print size once
OOP栈类模板(模板+DS)