posts - 5,  comments - 7,  trackbacks - 0
           

          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) 


          posted on 2008-11-25 12:59 Vincent-chen 閱讀(8359) 評論(1)  編輯  收藏 所屬分類: PrintJXL
          主站蜘蛛池模板: 金堂县| 昭通市| 永福县| 乐昌市| 金乡县| 彰化市| 长沙县| 璧山县| 镇原县| 交城县| 双鸭山市| 周口市| 长岛县| 济源市| 灵宝市| 义马市| 烟台市| 蚌埠市| 云和县| 新绛县| 商洛市| 黔西县| 灵璧县| 兴隆县| 沙坪坝区| 临泉县| 麦盖提县| 延长县| 梨树县| 区。| 鹤岗市| 从江县| 闻喜县| 潢川县| 永登县| 库尔勒市| 张北县| 英超| 新乐市| 固安县| 泉州市|