java报表JXL和POI打印设置

  1. JXL的打印设置jxl.SheetSettings这个类中,我们可以通过方SheetWritableSheet#getSettings()获取

     

    1.

    1.1

    SheetSetting#setOrientation(PageOrientation po)

    参数  PageOrientation#LANDSCAPE       横向打

    PageOrientation# PORTRAIT       纵向打

    1.2

    1.2.1缩放比(A) SheetSetting #setScaleFactor (int);百分比形

    1.2.2(F)

     SheetSetting #setFitWidth(int);

     SheetSetting #setFitHeight(int);

    1.3纸张大(Z) SheetSetting #setPaperSize (PaperSize);

    纸张大小的定义说明参PaperSize类中的常量

    1.4起始页(R) SheetSetting #setPageStrart(int);[默认状]

     

    2页面

    2.1(T) SheetSetting # setTopMargin (double);

    2.2(B) SheetSetting # setBottomMargin (double);

    2.3(L) SheetSetting # setLeftMargin (double);

    2.4(R) SheetSetting # setRightMargin (double);

    2.5(A) SheetSetting #setHeaderMargin(double);

    2.6(F) SheetSetting #setFooterMargin(double);

    2.7居中方

    2.7.1(Z) SheetSetting # setHorizontalCentre (boolean);

    2.7.2(V) SheetSetting #setVerticallyCenter(boolean);

     

    3/

    3.1SheetSetting # setHeaderHeaderFooter;

    :

    HeaderFooter的设置,首先确定页眉的位置(左、中、右),通HeaderFooter#getCentre()(或getLeft()getRight())方法获HeaderFooter.Contents类,之后对这个类的属性进行操作

    下面简单介绍几个常用方法

    设置字号Contents#setFontSize(int)

    设置字体Contents#setFontName(String)

    设置内容Contents# appendString),还appendDate()当前日期等等,具体参Contents类说

    3.2SheetSetting # setFooterHeaderFooter

    说明同

     

    4工作

    4.1打印区

    SheetSettings #setPrintArea(firstCol, firstRow, lastCol, lastRow)

     

    4.2打印标

    SheetSettings#setPrintTitles (firstRow,lastRow,firstCol,lastCol);

    SheetSettings#setPrintTitlesCol(firstCol,lastCol)

    SheetSettings#setPrintTitlesRow(firstRow,lastRow)

     

    另有一些其他的设置可JXL API中找到,不多说了

     

    附件:通常Excel中设置好打印,再将其读出来设置到当前页中来,附上代码

    Sheet页写数据之:

     

 

  1. //获取原Sheet页的设置
  2. SheetSettings sheetSetting=sheet.getSettings();
  3. //将原sheet页的打印设置设置到当前Sheet页中
  4. sheet=new MjJxlExcelCopyPrintSetting().copySheetSettingToSheet(sheet, sheetSetting);
  1. 下面是MjJxlExcelCopyPrintSetting的代码:
  2. import jxl.HeaderFooter;
  3. import jxl.Range;
  4. import jxl.SheetSettings;
  5. import jxl.format.PageOrientation;
  6. import jxl.format.PaperSize;
  7. import jxl.write.WritableSheet;
  8. /**
  9.  * 读取Jxl方法并设置
  10.  * @author 邱大为
  11.  * @version 1.0
  12.  */
  13. public class MjJxlExcelCopyPrintSetting {
  14.     /**
  15.      * 该方法将setting设置到sheet中
  16.      * @param sheet 需要设置的sheet
  17.      * @param setting 被设置的属性
  18.      * @return
  19.      */
  20.     public WritableSheet copySheetSettingToSheet(WritableSheet sheet,SheetSettings setting){
  21. //      设置原Sheet打印属性到新Sheet页
  22.         SheetSettings sheetSettings= sheet.getSettings();
  23.         
  24.         sheetSettings.setAutomaticFormulaCalculation(setting.getAutomaticFormulaCalculation());
  25.         sheetSettings.setBottomMargin(setting.getBottomMargin());
  26.         sheetSettings.setCopies(setting.getCopies());
  27.         sheetSettings.setDefaultColumnWidth(setting.getDefaultColumnWidth());
  28.         sheetSettings.setDefaultRowHeight(setting.getDefaultRowHeight());
  29.         sheetSettings.setDisplayZeroValues(setting.getDisplayZeroValues());
  30.         sheetSettings.setFitHeight(setting.getFitHeight());
  31.         sheetSettings.setFitToPages(setting.getFitToPages());
  32.         sheetSettings.setFitWidth(setting.getFitWidth());
  33.         
  34.         HeaderFooter footer=setting.getFooter();
  35.         if(footer!=null){
  36.             sheetSettings.setFooter(footer);
  37.         }
  38.         sheetSettings.setFooterMargin(setting.getFooterMargin());
  39.         HeaderFooter header=setting.getHeader();
  40.         if(header!=null){
  41.             sheetSettings.setHeader(header);
  42.         }
  43.         sheetSettings.setHeaderMargin(setting.getHeaderMargin());
  44.         sheetSettings.setHidden(setting.isHidden());
  45.         sheetSettings.setHorizontalCentre(setting.isHorizontalCentre());
  46.         sheetSettings.setHorizontalFreeze(setting.getHorizontalFreeze());
  47.         sheetSettings.setHorizontalPrintResolution(setting.getHorizontalPrintResolution());
  48.         sheetSettings.setLeftMargin(setting.getLeftMargin());
  49.         sheetSettings.setNormalMagnification(setting.getNormalMagnification());
  50.         PageOrientation pageOrientation=setting.getOrientation();
  51.         if(pageOrientation!=null){
  52.             sheetSettings.setOrientation(pageOrientation);
  53.         }
  54.         sheetSettings.setPageBreakPreviewMagnification(setting.getPageBreakPreviewMagnification());
  55.         sheetSettings.setPageBreakPreviewMode(setting.getPageBreakPreviewMode());
  56.         sheetSettings.setPageStart(setting.getPageStart());
  57.         PaperSize paperSize=setting.getPaperSize();
  58.         if(paperSize!=null){
  59.             sheetSettings.setPaperSize(setting.getPaperSize());
  60.         }
  61.         
  62.         sheetSettings.setPassword(setting.getPassword());
  63.         sheetSettings.setPasswordHash(setting.getPasswordHash());
  64.         Range printArea=setting.getPrintArea();
  65.         if(printArea!=null){
  66.             sheetSettings.setPrintArea(printArea.getTopLeft()==null?0:printArea.getTopLeft().getColumn(), 
  67.                     printArea.getTopLeft()==null?0:printArea.getTopLeft().getRow(), 
  68.                             printArea.getBottomRight()==null?0:printArea.getBottomRight().getColumn(), 
  69.                                     printArea.getBottomRight()==null?0:printArea.getBottomRight().getRow());
  70.         }
  71.         
  72.         sheetSettings.setPrintGridLines(setting.getPrintGridLines());
  73.         sheetSettings.setPrintHeaders(setting.getPrintHeaders());
  74.         
  75.         Range printTitlesCol=setting.getPrintTitlesCol();
  76.         if(printTitlesCol!=null){
  77.         sheetSettings.setPrintTitlesCol(printTitlesCol.getTopLeft()==null?0:printTitlesCol.getTopLeft().getColumn(), 
  78.                 printTitlesCol.getBottomRight()==null?0:printTitlesCol.getBottomRight().getColumn());
  79.         }
  80.         Range printTitlesRow=setting.getPrintTitlesRow();
  81.         if(printTitlesRow!=null){
  82.             sheetSettings.setPrintTitlesRow(printTitlesRow.getTopLeft()==null?0:printTitlesRow.getTopLeft().getRow(), 
  83.                     printTitlesRow.getBottomRight()==null?0:printTitlesRow.getBottomRight().getRow());
  84.         }
  85.         
  86.         sheetSettings.setProtected(setting.isProtected());
  87.         sheetSettings.setRecalculateFormulasBeforeSave(setting.getRecalculateFormulasBeforeSave());
  88.         sheetSettings.setRightMargin(setting.getRightMargin());
  89.         sheetSettings.setScaleFactor(setting.getScaleFactor());
  90.         sheetSettings.setSelected(setting.isSelected());
  91.         sheetSettings.setShowGridLines(setting.getShowGridLines());
  92.         sheetSettings.setTopMargin(setting.getTopMargin());
  93.         sheetSettings.setVerticalCentre(setting.isVerticalCentre());
  94.         sheetSettings.setVerticalFreeze(setting.getVerticalFreeze());
  95.         sheetSettings.setVerticalPrintResolution(setting.getVerticalPrintResolution());
  96.         sheetSettings.setZoomFactor(setting.getZoomFactor());
  97.         return sheet;
  98.     }
  99. }
  100.  
  101. 关于POI的打印设置:

    转自http://bbs.club.sina.com.cn/tableforum/App/view.php?bbsid=343&subid=0&fid=5477&tbid=8182特此感

    1.
    1.
    1

    1.1.
    1
    (T)HSSFPrintSetup#setLandscape(false); [默认状]
    1.1.2
    (L)HSSFPrintSetup#setLandscape(true);

    1.2

    1.2.
    1
    缩放比(A)HSSFPrintSetup#setScale((short) 100);[默认状]
    1.2.2
    (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
    居中方
    2.7.
    1
    (Z)HSSFSheet#setHorizontallyCenter(false);
    2.7.2
    (V)HSSFSheet#setVerticallyCenter(false);

    3
    /
    3.
    1
    HSSFHeader#setLeftHSSFHeader.date();
    :
    首先获HSSFHeader
    确定页眉的显示位(,左边显示页HSSFHeader#setLeft(显示内))
    可使HSSFHeader#setLeft,setCenter,setRight

    3.2
    HSSFFotter#setLeftHSSFFotter.page()+”/”+HSSFFotter.numPages()
    说明3.1
    首先获HSSFFotter
    确定页眉的显示位(,左边显示页HSSFFotter#setLeft(显示内))
    可使HSSFFotter#setLeft,setCenter,setRight

    4
    工作
    4.
    1
    打印区
    HSSFWorkbook#setPrintArea(intsheetIndex,

    intstartColumn,
    intendColumn,
    intstartRow,
    intendRow);
    参数的说
    sheetIndex
    0开始sheet的索引编
    startColumn
    -
    打印区域的开始列
    endColumn
    -
    打印区域的结束列
    startRow
    -
    打印区域的开始行
    endRow
    -
    打印区域的结束行

    4.
    2
    打印标
    HSSFWorkbook#setRepeatingRowsAndColumns(intsheetIndex,

    intstartColumn,
    intendColumn,
    intstartRow,
    intendRow);
    参数说明4.1
    使用说明
    仅仅设置左端标题列
    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);
    按草稿方式QHSSFPrintSetup#setDraft(false)
    行号列(L):(很抱歉,还没有找到
    (M):(很抱歉,还没有找到
    错误单元格打印(E):(很抱歉,还没有找到

    4.
    4
    打印顺

    HSSFPrintSetup#setLeftToRight(false) 

     

你可能感兴趣的:(java报表)