java报表JXL和POI打印设置

JXL的打印设置在jxl.SheetSettings这个类中,我们可以通过方法Sheet(或者WritableSheet#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.1页眉SheetSetting # setHeaderHeaderFooter;

说明:

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

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

设置字号:Contents#setFontSize(int)

设置字体:Contents#setFontName(String)

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

3.2页脚SheetSetting # 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
3SheetSettings sheetSetting=sheet.getSettings(); 
4

5//将原sheet页的打印设置设置到当前Sheet页中 

6
7sheet=new MjJxlExcelCopyPrintSetting().copySheetSettingToSheet(sheet, sheetSetting);
8

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

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);
按草稿方式(Q):HSSFPrintSetup#setDraft(false)

行号列标(L):(很抱歉,还没有找到)
批注(M):(很抱歉,还没有找到)
错误单元格打印为(E):(很抱歉,还没有找到)

4.4
打印顺序

HSSFPrintSetup#setLeftToRight(false) 

 

 

 

 

你可能感兴趣的:(java,Excel,F#)