为 Apache POI 中生成的 Excel 文件设置默认打印缩放 [英] Set default print scalling for the generated Excel file in Apache POI
问题描述
我通过 Apache POI 成功生成了一个 Excel 文件.问题是,当我尝试打印它时,最后 3 列不在可打印区域内.例如,工作表有 A 到 L 列,但在打印时它只适合 A 列.用户总是在打印前将打印缩放从无缩放"更改为在一页上容纳所有列".
I generate an Excel file via Apache POI successfully. The problem is that, when I try to print it, the last 3 columns is not within the printable area. For example, the worksheet has columns A to L but it only fits column A to when printing. The users always change the print scaling from "No Scaling" to "Fit All Columns on One Page" before printing.
如何修改 Excel 文件,使所有列都适合可打印区域,并且用户每次打印时都不需要更改缩放比例.
How can I modify the Excel file in such a way that all columns would fit on the printable area, and the users won't need to change the scaling every time they print.
推荐答案
这可以通过 Sheet.setFitToPage 和 PrintSetup.setFitWidth 和 PrintSetup.setFitHeight
.
This can be done with a combination of Sheet.setFitToPage and PrintSetup.setFitWidth and PrintSetup.setFitHeight
.
示例:
import java.io.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.*;
public class PrintSetupFitWidth {
public static void main(String[] args) throws Exception {
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet();
//create at least one cell so print preview is possible
sheet.createRow(0).createCell(0).setCellValue("Cell A1");
//set page setup to fit to one page width but multiple pages height
sheet.setFitToPage(true);
sheet.getPrintSetup().setFitWidth((short)1);
sheet.getPrintSetup().setFitHeight((short)0);
FileOutputStream fileOut = new FileOutputStream("PrintSetupFitWidth.xlsx");
workbook.write(fileOut);
fileOut.close();
workbook.close();
}
}
这篇关于为 Apache POI 中生成的 Excel 文件设置默认打印缩放的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!