• 11、借助POI实现Java生成并打印excel报表(2)


    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

     

  • 相关阅读:
    天才难过四级关
    肉鸟一只
    继续折腾mac。os(没进展)
    Intel(R) ICH9M LPC Interface Controller 2919驱动安装方法
    nusoap的使用
    STL HASH_MAP简单应用
    jQuery select操作控制方法小结
    unordered_map详解
    STL map与Boost unordered_map
    [C/C++]关于C++11中的std::move和std::forward
  • 原文地址:https://www.cnblogs.com/hthuang/p/3477454.html
Copyright © 2020-2023  润新知