-
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 # setHeader(HeaderFooter);
说明:
对于HeaderFooter的设置,首先确定页眉的位置(左、中、右),通过HeaderFooter#getCentre()(或者getLeft()、getRight())方法获取HeaderFooter.Contents类,之后对这个类的属性进行操作。
下面简单介绍几个常用方法:
设置字号:Contents#setFontSize(int)
设置字体:Contents#setFontName(String)
设置内容:Contents# append(String),还有appendDate()当前日期等等,具体参考Contents类说明
3.2页脚SheetSetting # setFooter(HeaderFooter);
说明同上
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页写数据之前:
- //获取原Sheet页的设置
- SheetSettings sheetSetting=sheet.getSettings();
- //将原sheet页的打印设置设置到当前Sheet页中
- sheet=new MjJxlExcelCopyPrintSetting().copySheetSettingToSheet(sheet, sheetSetting);
- 下面是MjJxlExcelCopyPrintSetting的代码:
- import jxl.HeaderFooter;
- import jxl.Range;
- import jxl.SheetSettings;
- import jxl.format.PageOrientation;
- import jxl.format.PaperSize;
- import jxl.write.WritableSheet;
- /**
- * 读取Jxl方法并设置
- * @author 邱大为
- * @version 1.0
- */
- public class MjJxlExcelCopyPrintSetting {
- /**
- * 该方法将setting设置到sheet中
- * @param sheet 需要设置的sheet
- * @param setting 被设置的属性
- * @return
- */
- public WritableSheet copySheetSettingToSheet(WritableSheet sheet,SheetSettings setting){
- // 设置原Sheet打印属性到新Sheet页
- SheetSettings sheetSettings= sheet.getSettings();
- sheetSettings.setAutomaticFormulaCalculation(setting.getAutomaticFormulaCalculation());
- sheetSettings.setBottomMargin(setting.getBottomMargin());
- sheetSettings.setCopies(setting.getCopies());
- sheetSettings.setDefaultColumnWidth(setting.getDefaultColumnWidth());
- sheetSettings.setDefaultRowHeight(setting.getDefaultRowHeight());
- sheetSettings.setDisplayZeroValues(setting.getDisplayZeroValues());
- sheetSettings.setFitHeight(setting.getFitHeight());
- sheetSettings.setFitToPages(setting.getFitToPages());
- sheetSettings.setFitWidth(setting.getFitWidth());
- HeaderFooter footer=setting.getFooter();
- if(footer!=null){
- sheetSettings.setFooter(footer);
- }
- sheetSettings.setFooterMargin(setting.getFooterMargin());
- HeaderFooter header=setting.getHeader();
- if(header!=null){
- sheetSettings.setHeader(header);
- }
- sheetSettings.setHeaderMargin(setting.getHeaderMargin());
- sheetSettings.setHidden(setting.isHidden());
- sheetSettings.setHorizontalCentre(setting.isHorizontalCentre());
- sheetSettings.setHorizontalFreeze(setting.getHorizontalFreeze());
- sheetSettings.setHorizontalPrintResolution(setting.getHorizontalPrintResolution());
- sheetSettings.setLeftMargin(setting.getLeftMargin());
- sheetSettings.setNormalMagnification(setting.getNormalMagnification());
- PageOrientation pageOrientation=setting.getOrientation();
- if(pageOrientation!=null){
- sheetSettings.setOrientation(pageOrientation);
- }
- sheetSettings.setPageBreakPreviewMagnification(setting.getPageBreakPreviewMagnification());
- sheetSettings.setPageBreakPreviewMode(setting.getPageBreakPreviewMode());
- sheetSettings.setPageStart(setting.getPageStart());
- PaperSize paperSize=setting.getPaperSize();
- if(paperSize!=null){
- sheetSettings.setPaperSize(setting.getPaperSize());
- }
- sheetSettings.setPassword(setting.getPassword());
- sheetSettings.setPasswordHash(setting.getPasswordHash());
- Range printArea=setting.getPrintArea();
- if(printArea!=null){
- sheetSettings.setPrintArea(printArea.getTopLeft()==null?0:printArea.getTopLeft().getColumn(),
- printArea.getTopLeft()==null?0:printArea.getTopLeft().getRow(),
- printArea.getBottomRight()==null?0:printArea.getBottomRight().getColumn(),
- printArea.getBottomRight()==null?0:printArea.getBottomRight().getRow());
- }
- sheetSettings.setPrintGridLines(setting.getPrintGridLines());
- sheetSettings.setPrintHeaders(setting.getPrintHeaders());
- Range printTitlesCol=setting.getPrintTitlesCol();
- if(printTitlesCol!=null){
- sheetSettings.setPrintTitlesCol(printTitlesCol.getTopLeft()==null?0:printTitlesCol.getTopLeft().getColumn(),
- printTitlesCol.getBottomRight()==null?0:printTitlesCol.getBottomRight().getColumn());
- }
- Range printTitlesRow=setting.getPrintTitlesRow();
- if(printTitlesRow!=null){
- sheetSettings.setPrintTitlesRow(printTitlesRow.getTopLeft()==null?0:printTitlesRow.getTopLeft().getRow(),
- printTitlesRow.getBottomRight()==null?0:printTitlesRow.getBottomRight().getRow());
- }
- sheetSettings.setProtected(setting.isProtected());
- sheetSettings.setRecalculateFormulasBeforeSave(setting.getRecalculateFormulasBeforeSave());
- sheetSettings.setRightMargin(setting.getRightMargin());
- sheetSettings.setScaleFactor(setting.getScaleFactor());
- sheetSettings.setSelected(setting.isSelected());
- sheetSettings.setShowGridLines(setting.getShowGridLines());
- sheetSettings.setTopMargin(setting.getTopMargin());
- sheetSettings.setVerticalCentre(setting.isVerticalCentre());
- sheetSettings.setVerticalFreeze(setting.getVerticalFreeze());
- sheetSettings.setVerticalPrintResolution(setting.getVerticalPrintResolution());
- sheetSettings.setZoomFactor(setting.getZoomFactor());
- return sheet;
- }
- }
-
关于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#setLeft(HSSFHeader.date();
说明:
首先获得HSSFHeader对象
确定页眉的显示位置(如,左边显示页眉HSSFHeader#setLeft(显示内容))
可使用HSSFHeader#setLeft,setCenter,setRight
3.2页脚HSSFFotter#setLeft(HSSFFotter.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);