11、POI打印功能
11.1、常用模塊形式:
1 HSSFPrintSetup printSetup = sheet.getPrintSetup(); 2 printSetup.setVResolution((short) 600); //打印質量600點 3 printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); //A4紙張打印 4 printSetup.setLandscape(true); //橫向打印
11.2、常用參數設置方法
1、頁面設置
1.1、方向:
縱向(T):HSSFPrintSetup#setLandscape(false); [默認狀態]
橫向(L):HSSFPrintSetup#setLandscape(true);
1.2、縮放:
縮放比例(A):HSSFPrintSetup#setScale((short) 100); [默認狀態]
調整(F): 頁寬 HSSFPrintSetup#setFitWidth((short) 1);
頁高 HSSFPrintSetup#setFitHeight((short) 0);
1.3、紙張大小(Z):HSSFPrintSetup#setPageSize(HSSFPrintSetup.LETTER_PAPERSIZE);
紙張大小定義說明:
public static final short LETTER_PAPERSIZE = 1;
public static final short LEGAL_PAPERSIZE = 5;
public static final short EXECUTIVE_PAPERSIZE = 7;
public static final short A4_PAPERSIZE = 9;
public static final short A5_PAPERSIZE = 11;
public static final short ENVELOPE_10_PAPERSIZE = 20;
public static final short ENVELOPE_DL_PAPERSIZE = 27;
public static final short ENVELOPE_CS_PAPERSIZE = 28;
public static final short ENVELOPE_MONARCH_PAPERSIZE = 37;
1.4、打印質量(Q): HSSFPrintSetup#setVResolution((short) 300);
1.5、起始頁碼(R): HSSFPrintSetup#setPageStrart((short) 0); [默認狀態]
2、頁面距
2.1、上(T): HSSFSheet#setMargin(HSSFSheet.TopMargin,(short) 0.6);
2.2、下(B): HSSFSheet#setMargin(HSSFSheet.BottomMargin,(short) 0.6);
2.3、左(L): HSSFSheet#setMargin(HSSFSheet.LeftMargin,(short) 0.6);
2.4、右(R): HSSFSheet#setMargin(HSSFSheet.RightMargin,(short) 0.2);
2.5、頁眉(A): HSSFPrintSetup#setHeaderMargin((double) 0.2);
2.6、頁腳(F): HSSFPrintSetup#setFooterMargin((double) 0.6);
2.7、居中對齊方式: 水平(Z) HSSFSheet#setHorizontallyCenter(false);
垂直(V) HSSFSheet#setVerticallyCenter(false);
3、頁眉/頁腳
3.1、頁眉:HSSFHeader#setLeft(HSSFHeader.date();
說明:首先獲得HSSFHeader對象,確定頁眉的顯示位置(如:左邊顯示頁眉HSSFHeader#setLeft(顯示內容))
可用:HSSFHeader#setLeft,setCenter,setRight
3.2、頁腳: HSSFFotter#setLeft(HSSFFotter.page()+”/”+HSSFFotter.numPages())
說明同頁眉
4、工作表
4.1、打印區域
HSSFWorkbook#setPrintArea((int) sheetIndex, (int) startColumn, (int) endColumn, (int) startRow, (int) endRow);
參數說明:
sheetIndex–從0開始的sheet的索引編號
startColumn-打印區域的開始列號
endColumn- 打印區域的結束列號
startRow-打印區域的開始行號
endRow- 打印區域的結束行號
4.2、打印標題
HSSFWorkbook#setRepeatingRowsAndColumns((int) sheetIndex, (int) startColumn, (int) endColumn, (int) startRow, (int) endRow);
參數說明同上。
使用說明:僅僅設置左端標題列:workbook.setRepeatingRowsAndColumns(0,0,1,-1,-1);
僅僅設置頂端標題行:workbook.setRepeatingRowsAndColumns(0,-1,-1,0,4);
同時設置左端和頂端標題:workbook.setRepeatingRowsAndColumns(0,-1,-1,-1,-1);
4.3、打印
網格線 (G):HSSFSheet#setPrintGridlines(false);
單色打印(B)HSSFPrintSetup#setNoColor(false);
按草稿方式(Q):HSSFPrintSetup#setDraft(false);
行號列標(L):
批注(M):
錯誤單元格打印為(E):
4.4、打印順序
HSSFPrintSetup#setLeftToRight(false);
11.3、程序示例:
1 1package test; 2 2 3 3import java.io.FileOutputStream; 4 4import java.io.IOException; 5 5 6 6import org.apache.poi.hssf.usermodel.HSSFCell; 7 7import org.apache.poi.hssf.usermodel.HSSFCellStyle; 8 8import org.apache.poi.hssf.usermodel.HSSFFont; 9 9import org.apache.poi.hssf.usermodel.HSSFPrintSetup; 10 10import org.apache.poi.hssf.usermodel.HSSFRichTextString; 11 11import org.apache.poi.hssf.usermodel.HSSFRow; 12 12import org.apache.poi.hssf.usermodel.HSSFSheet; 13 13import org.apache.poi.hssf.usermodel.HSSFWorkbook; 14 14 15 15public class ExcelTest { 16 16 17 17 public static void main(String[] args) throws IOException { 18 18 19 19 // create a new file 20 20 FileOutputStream out = new FileOutputStream("D:/workbook.xls"); 21 21 // create a new workbook 22 22 HSSFWorkbook wb = new HSSFWorkbook(); 23 23 // create a new sheet 24 24 HSSFSheet sheet = wb.createSheet(); 25 25 26 26 //2.model 27 27 HSSFRow row = sheet.createRow(2); 28 28 row.setHeightInPoints(20); 29 29 HSSFCell cell = row.createCell(2); 30 30 HSSFFont cnFont = wb.createFont(); 31 31 cnFont.setFontHeightInPoints((short) 10); 32 32 //font.setFontName("漢儀報宋簡"); 33 33 cnFont.setFontName("隸書"); 34 34 HSSFCellStyle cnStyle = wb.createCellStyle(); 35 35 cnStyle.setFont(cnFont); 36 36 cell.setCellStyle(cnStyle); 37 37 HSSFRichTextString richText = new HSSFRichTextString("中文字體測試"); 38 38 cell.setCellValue(richText); 39 39 HSSFCell enCell = row.createCell(3); 40 40 HSSFFont enFont = wb.createFont(); 41 41 enFont.setFontHeightInPoints((short) 10); 42 42 enFont.setFontName("Arial Black"); 43 43 HSSFCellStyle enStyle = wb.createCellStyle(); 44 44 enStyle.setFont(enFont); 45 45 enCell.setCellStyle(enStyle); 46 46 enCell.setCellValue(new HSSFRichTextString("English font test")); 47 47 sheet.setColumnWidth(2, 4000); 48 48 sheet.setColumnWidth(3, 4000); 49 49 50 50 //3.output 51 51 sheet.setDisplayGridlines(false); 52 52 sheet.setPrintGridlines(false); 53 53 HSSFPrintSetup printSetup = sheet.getPrintSetup(); 54 54 //A4紙 55 55 printSetup.setPaperSize(HSSFPrintSetup.A4_PAPERSIZE); 56 56 wb.write(out); 57 57 out.close(); 58 58 } 59 59} 60 60