它如何设置“在一页上适合所有列"的文件?环境 [英] How can it set the file for "Fit all columns on one page" setting

查看:30
本文介绍了它如何设置“在一页上适合所有列"的文件?环境的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个要求,其中 Apache POI 生成的文件需要使用 适合一页设置中的所有列 设置.我已经尝试了一系列 API 变体,但到目前为止我还没有做到.我也真的不知道是否可以做到.

I have a requirement where files generated by Apache POI need to produce a fie with the fit all columns on one page setting set. I've tried a bunch of variations with the API but so far I haven't been able to do it. Nor can I really find if it can be done.

看起来 setFitToPage(true) 函数会调整高度和宽度的大小,而不仅仅是像我想要的那样调整宽度.像我在其他各种堆栈溢出问题中发现的那样,使用 setFitWidthsetFitHeight 似乎没有任何影响.

It seems like the setFitToPage(true) function resizes both the height and width not just the width like I want. Using setFitWidth and setFitHeight like I find in various other stack overflow questions doesn't seem to affect anything.

这是我目前所拥有的:

public void setPrintSettings(Sheet sheet) {

   sheet.setFitToPage(true); //this will resize both height and width to fit
   sheet.getPrintSetup().setLandscape(true);

   sheet.getPrintSetup().setFitWidth((short) 1);
   sheet.getPrintSetup().setFitHeight((short) 1);

}

推荐答案

不是调用 setFitToPage(true) 使 Excel 调整高度和宽度以适合一页.此调用是必要的,但原因不同.在 Excel 的页面设置屏幕中,此方法调用控制对话框中哪个单选按钮处于活动状态.值 true 设置适合:"的单选按钮,允许您控制页面宽度和页面高度框.值 false 将单选按钮设置为调整为:",正常大小的百分比.

It's not the call to setFitToPage(true) that makes Excel resize both the height and width to fit one page. This call is necessary, but for a different reason. In Excel's Page Setup screen, this method call controls which radio button is active in the dialog box. The value true sets the radio button for "Fit to:", allowing you to control the page(s) wide by page(s) tall boxes. The value false sets the radio button for "Adjust to: ", percentage normal size.

在这种情况下,您希望将其调整为 1 页宽不关心"页高.用于页面高度(setFitHeight 方法)的值在此处为 0.

In this case, you want to fit it to 1 page wide by "don't care" pages tall. The value to use for pages tall (setFitHeight method) is 0 here.

sheet.setFitToPage(true);
PrintSetup ps = sheet.getPrintSetup();
ps.setFitWidth( (short) 1);
ps.setFitHeight( (short) 0);

当我写出包含具有这些设置的 SheetWorkbook 并在 Excel 中打开它时,页面设置对话框具有适合:"单选框按钮被选中,1 页宽(空白)高".在打印预览"屏幕中,打印设置选项将在一页上显示所有列"列为选中状态.

When I write out a Workbook containing a Sheet with these settings, and I open it in Excel, the Page Setup dialog box has the "Fit to:" radio button selected, and "1 page(s) wide by (blank) tall". In the "Print Preview" screen, the print settings options lists "Fit All Columns on One Page" as selected.

这篇关于它如何设置“在一页上适合所有列"的文件?环境的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆