如何使用Java设置电子表格的打印区域。(How to set the print area of a spreadsheet using Java.)

优质
小牛编辑
124浏览
2023-12-01

问题描述 (Problem Description)

如何使用Java设置电子表格的打印区域。

解决方案 (Solution)

以下是使用Java设置电子表格打印区域的程序。

import java.io.File;
import java.io.FileOutputStream;
import org.apache.poi.xssf.usermodel.XSSFPrintSetup;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class SettingPrintAreaToSpreadSheet {
   public static void main(String[] args)throws Exception {
      //Create a Work Book
      XSSFWorkbook workbook = new XSSFWorkbook();
      XSSFSheet spreadsheet = workbook.createSheet("Print Area");
      //set print area with indexes
      workbook.setPrintArea(
         0, //sheet index
         0, //start column
         5, //end column
         0, //start row
         5  //end row
      );
      //set paper size
      spreadsheet.getPrintSetup().setPaperSize(XSSFPrintSetup.A4_PAPERSIZE);
      //set display grid lines or not
      spreadsheet.setDisplayGridlines(true);
      //set print grid lines or not
      spreadsheet.setPrintGridlines(true);
      FileOutputStream out = new FileOutputStream(
         new File("C:/poiexcel/printarea.xlsx"));
      workbook.write(out);
      out.close();
      System.out.println("printarea.xlsx written successfully");
   }
}

输出 (Output)

打印区域