我在我的C#应用程序中使用NPOIVersion2.5.3,并试图设置缩放选项(适合1页上的所有列)。从这里和这里的这些问题上看,这似乎很容易做到。
问题:
因此,我的问题是在使用下面的代码时发生的。所做的就是配置;宽度和高度都适合于一个页面。我认为这是因为sheet.FitToPage = true。
private void SetPrintSettings(XSSFSheet sheet)
{
sheet.SetMargin(MarginType.BottomMargin, 0.5);
sheet.SetMargin(MarginType.TopMargin, 0.5);
sheet.SetMargin(MarginType.LeftMargin, 0.45);
sheet.SetMargin(MarginType.RightMargin, 0.45);
sheet.SetMargin(MarginType.HeaderMargin, 0.3);
sheet.SetMargin(MarginType.FooterMargin, 0.3);
sheet.Autobreaks = true; //auto breaks
sheet.FitToPage = true; //THIS SETS IT TO ALL FIT ON ONE PAGE
var PrintSetup = sheet.PrintSetup;
PrintSetup.FitWidth = 1; //fit width onto 1 page
PrintSetup.FitHeight = 0; //don't care about height
PrintSetup.Landscape = true;
PrintSetup.PaperSize = 3; //paper size 11x17
}
在执行上面的代码时,我在Excel中得到以下输出。
因此,在此之后,我尝试将其设置为false,如下所示。
private void SetPrintSettings(XSSFSheet sheet)
{
sheet.SetMargin(MarginType.BottomMargin, 0.5);
sheet.SetMargin(MarginType.TopMargin, 0.5);
sheet.SetMargin(MarginType.LeftMargin, 0.45);
sheet.SetMargin(MarginType.RightMargin, 0.45);
sheet.SetMargin(MarginType.HeaderMargin, 0.3);
sheet.SetMargin(MarginType.FooterMargin, 0.3);
sheet.Autobreaks = true; //auto breaks
sheet.FitToPage = false;
var PrintSetup = sheet.PrintSetup;
PrintSetup.FitWidth = 1; //fit width onto 1 page
PrintSetup.FitHeight = 0; //don't care about height
PrintSetup.Landscape = true;
PrintSetup.PaperSize = 3; //paper size 11x17
}
当更改配置时,它呈现如下所示的“无缩放”。
不管我怎么努力,我似乎都不能让它起作用。我尝试过各种不同的设置,但似乎没有任何效果。我开始认为这是一个错误的版本,我正在使用。我发现的几乎所有示例都是针对Java的,这一点也没有帮助,所以我不确定这是否只是一个方法问题。
期望输出:
下面是我想做的事。只需将缩放选项设置为适合1页的列即可。如果有人能帮我,或者指出正确的方向,那就太棒了。
发布于 2021-06-22 16:10:05
看起来这是NPOI中的一个bug。
为了实现设置,您需要在sheet中的PageSetup
元素将fitToHeight
属性设置为0之后。例如:
<pageSetup orientation="landscape" fitToHeight="0"/>
不幸的是,如果我正确地阅读了NPOI代码,那么NPOI似乎没有输出该属性,因为它认为它是一个空白值。
调用PrintSetup.FitHeight = 0;
时,它在Sheet.cs
中设置fitToHeight
属性。在编写文件时,在Sheet.cs中有以下内容:
XmlHelper.WriteAttribute(sw, "fitToHeight", this.fitToHeight, 1);
WriteAttribute
代码如下所示:
public static void WriteAttribute(StreamWriter sw, string attributeName, uint value, uint defaultValue, bool writeIfBlank = false)
{
if(value != defaultValue)
WriteAttribute(sw, attributeName, (int)value, writeIfBlank);
else if(writeIfBlank)
WriteAttribute(sw, attributeName, (int)value, writeIfBlank);
}
在我们的情况下,value
(0)不等于defaultValue
(1),所以我们输入第一个if
。这就调用了另一个重载的WriteAttribute
,如下所示:
public static void WriteAttribute(StreamWriter sw, string attributeName, int value, bool writeIfBlank)
{
if (value == 0 && !writeIfBlank)
return;
WriteAttribute(sw, attributeName, value.ToString(CultureInfo.InvariantCulture));
}
value
等于0
,而writeIfBlank
是false,因此,第一个if
是true,因此return;
被击中,没有写出值。
发布于 2021-06-21 19:23:39
1),如果我对你的理解正确的话,你可能会想念
autoSizeColumn
。请将autosize
配置/安装为适合您需要使用youExcelSheet.AutoSizeColumn()
的cols,在2)与autofit to page/sheetFitToPage
的组合中,应该会使其适合您。
来自阿帕奇·吉顿的源代码,因为计算col & page的大小,所以在autoSizeCol
和autofit
页面之间调用的顺序可能很重要,所以您可以尝试这两个组合。
来自阿帕奇裁判。可以从Apache 这里自动调整您的科尔的大小,
// create some cells
for (int r=0; r < 10; r++) {
Row row = sheet.createRow(r);
for (int c; c < 10; c++) {
Cell cell = row.createCell(c);
cell.setCellValue("Cell " + c.getAddress().formatAsString());
}
}
// 1 ** AUTO-Size the columns individually like below.
// 2 Or get/fit ALL, then loop through the cols, divide the sheet by number of cols
sheet.autoSizeColumn(0);
sheet.autoSizeColumn(1);
在SO 这里中有一个较旧的样本
HSSFWorkbook spreadsheet = new HSSFWorkbook();
DataSet results = GetSalesDataFromDatabase();
//here, we must insert at least one sheet to the workbook. otherwise, Excel will say 'data lost in file'
HSSFSheet sheet1 = spreadsheet.CreateSheet("Sheet1");
foreach (DataColumn column in results.Tables[0].Columns)
{
int rowIndex = 0;
foreach (DataRow row in results.Tables[0].Rows)
{
HSSFRow dataRow = sheet1.CreateRow(rowIndex);
dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
rowIndex++;
}
sheet1.AutoSizeColumn(column.Ordinal);
}
//Write the stream data of workbook to the file 'test.xls' in the temporary directory
FileStream file = new FileStream(Path.Combine(Path.GetTempPath(), "test.xls") , FileMode.Create);
spreadsheet.Write(file);
file.Close();
https://stackoverflow.com/questions/67926647
复制相似问题