首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >NPOI C#将列设置为适合于一页

NPOI C#将列设置为适合于一页
EN

Stack Overflow用户
提问于 2021-06-10 18:25:45
回答 2查看 827关注 0票数 5

我在我的C#应用程序中使用NPOIVersion2.5.3,并试图设置缩放选项(适合1页上的所有列)。从这里这里的这些问题上看,这似乎很容易做到。

问题:

因此,我的问题是在使用下面的代码时发生的。所做的就是配置;宽度和高度都适合于一个页面。我认为这是因为sheet.FitToPage = true

代码语言:javascript
运行
复制
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,如下所示。

代码语言:javascript
运行
复制
 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页的列即可。如果有人能帮我,或者指出正确的方向,那就太棒了。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-06-22 16:10:05

看起来这是NPOI中的一个bug。

为了实现设置,您需要在sheet中的PageSetup元素将fitToHeight属性设置为0之后。例如:

代码语言:javascript
运行
复制
<pageSetup orientation="landscape" fitToHeight="0"/>

不幸的是,如果我正确地阅读了NPOI代码,那么NPOI似乎没有输出该属性,因为它认为它是一个空白值。

调用PrintSetup.FitHeight = 0;时,它在Sheet.cs中设置fitToHeight属性。在编写文件时,在Sheet.cs中有以下内容:

代码语言:javascript
运行
复制
XmlHelper.WriteAttribute(sw, "fitToHeight", this.fitToHeight, 1);

WriteAttribute代码如下所示:

代码语言:javascript
运行
复制
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,如下所示:

代码语言:javascript
运行
复制
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;被击中,没有写出值。

票数 2
EN

Stack Overflow用户

发布于 2021-06-21 19:23:39

1),如果我对你的理解正确的话,你可能会想念autoSizeColumn。请将autosize配置/安装为适合您需要使用youExcelSheet.AutoSizeColumn()的cols,在2)与autofit to page/sheet FitToPage的组合中,应该会使其适合您。

来自阿帕奇·吉顿的源代码,因为计算col & page的大小,所以在autoSizeColautofit页面之间调用的顺序可能很重要,所以您可以尝试这两个组合。

来自阿帕奇裁判。可以从Apache 这里自动调整您的科尔的大小,

代码语言:javascript
运行
复制
// 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 这里中有一个较旧的样本

代码语言:javascript
运行
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67926647

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档