当前位置:网站首页>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 .

原网站

版权声明
本文为[firechun]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206250401264088.html