如何使用EPPlus为A4纸创建Excel文件

问题描述:

我目前的项目使用EPPlus来创建Excel文件。这些文件是由用户打印的,我试图强制Excel文件只打印一个A4页面,而不管内容的宽度。如何使用EPPlus为A4纸创建Excel文件

实际上,当打印文件时,它需要两页,第二页只包含一列。

我的代码:

ws.PrinterSettings.Orientation = eOrientation.Landscape; 
ws.PrinterSettings.PrintArea = ws.Cells[ws_dimension_adress]; 
ws.PrinterSettings.TopMargin= 0; 
ws.PrinterSettings.RightMargin = 0; 
ws.PrinterSettings.BottomMargin = 0; 
ws.PrinterSettings.LeftMargin = 0; 
ws.Cells[ws_dimension_adress].AutoFitColumns(); 
ws.Cells[ws_dimension_adress].Style.Font.Size = 9; 

结果: 由我提供的代码,结果

enter image description here

我需要什么:

enter image description here

我搜索之类的东西“AUT到A4页面“,但还没有解决方案。

备注:所有列都需要。在创建文件之前,我不能简单地删除一个。

感谢您的帮助!

我找到了解决方案。

EPPlus有一个printerSettings为此。 使用该生产线是

ws.PrinterSettings.FitToPage = true; 

使用,缺省文件的属性强制Excel打印仅在一个页面中的数据。

希望这可以帮助其他一些开发者。

您也可以使用该行代码为特定的纸张尺寸:

ws.PrinterSettings.PaperSize = ePaperSize.A4; 

Hopte这可以帮助其他一些人。