<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    JXL(JExcleApi)Excle詳細(xì)使用心得

    功能:寫(xiě)入/讀取Excle,設(shè)置打印樣式排版,鎖定列與行,設(shè)置打印標(biāo)題,給Excle加密。

    java報(bào)表JXL和POI打印設(shè)置
     =======================================   引用來(lái)自互聯(lián)網(wǎng)  start ===============================================

    JXL的打印設(shè)置在jxl.SheetSettings這個(gè)類(lèi)中,我們可以通過(guò)方法Sheet(或者WritableSheet#getSettings()獲取。

    1.頁(yè)面

    1.1方向

    SheetSetting#setOrientation(PageOrientation po)

    參數(shù):  PageOrientation#LANDSCAPE       橫向打印

    PageOrientation# PORTRAIT       縱向打印

    1.2縮放

    1.2.1縮放比例(A) SheetSetting #setScaleFactor (int);百分比形式

    1.2.2調(diào)整(F)

    頁(yè)寬 SheetSetting #setFitWidth(int);

    頁(yè)高 SheetSetting #setFitHeight(int);

    1.3紙張大小(Z) SheetSetting #setPaperSize (PaperSize);

    紙張大小的定義說(shuō)明參見(jiàn)PaperSize類(lèi)中的常量。

    1.4起始頁(yè)碼(R) SheetSetting #setPageStrart(int);[默認(rèn)狀態(tài)]

    2頁(yè)面距

    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頁(yè)眉(A) SheetSetting #setHeaderMargin(double);

    2.6頁(yè)腳(F) SheetSetting #setFooterMargin(double);

    2.7居中方式

    2.7.1水平(Z) SheetSetting # setHorizontalCentre (boolean);

    2.7.2垂直(V) SheetSetting #setVerticallyCenter(boolean);

    3頁(yè)眉/頁(yè)腳

    3.1頁(yè)眉SheetSetting # setHeaderHeaderFooter;

    說(shuō)明:

    對(duì)于HeaderFooter的設(shè)置,首先確定頁(yè)眉的位置(左、中、右),通過(guò)HeaderFooter#getCentre()(或者getLeft()getRight())方法獲取HeaderFooter.Contents類(lèi),之后對(duì)這個(gè)類(lèi)的屬性進(jìn)行操作。

    下面簡(jiǎn)單介紹幾個(gè)常用方法:

    設(shè)置字號(hào):Contents#setFontSize(int)

    設(shè)置字體:Contents#setFontName(String)

    設(shè)置內(nèi)容:Contents# appendString),還有appendDate()當(dāng)前日期等等,具體參考Contents類(lèi)說(shuō)明

    3.2頁(yè)腳SheetSetting # setFooterHeaderFooter);

    說(shuō)明同上

    4工作表

    4.1打印區(qū)域

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

    4.2打印標(biāo)題

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

    SheetSettings#setPrintTitlesCol(firstCol,lastCol)

    SheetSettings#setPrintTitlesRow(firstRow,lastRow)
     =======================================   引用來(lái)自互聯(lián)網(wǎng)  end ===============================================

    =======================================  自己寫(xiě)的Demo ========================================
    DEMO

     

    package finance.barcode;

    import static jxl.format.PaperSize.getPaperSize;

    import include.nseer_db.nseer_db;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.InputStream;

    import jxl.CellView;
    import jxl.HeaderFooter;
    import jxl.Workbook;
    import jxl.format.Alignment;
    import jxl.format.Border;
    import jxl.format.BorderLineStyle;
    import jxl.format.Colour;
    import jxl.format.PageOrientation;
    import jxl.format.PaperSize;
    import jxl.format.VerticalAlignment;
    import jxl.write.Label;
    import jxl.write.NumberFormats;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;

    /**
     * 
     * 
    @author Solitary
     * 
    @author xing5156@gmail.com
     *
     
    */

    public class BarToExcle extends nseer_db{

        
    /** *********創(chuàng)建字體**************** */
        
    public WritableFont BlodFont ;             // 粗體
        public WritableFont NormalFont ;        // 普通字體
        public WritableFont BarCodeFont ;         // 條形碼字體
        /** ************以下設(shè)置幾種格式的單元格屬性************ */
        
    public WritableCellFormat wcf_header ;    // 標(biāo)題樣式
        public WritableCellFormat wcf_left ;    // 用于正文左
        public WritableCellFormat wcf_barcode ; // 用于條形碼格式
        
        
        
    public BarToExcle(String db) {        
            
    super(db) ;
            
            
    /** ********* 初始化字體 **************** */
            BlodFont 
    = new WritableFont(WritableFont.ARIAL, 20, WritableFont.BOLD ) ;
            NormalFont 
    = new WritableFont(WritableFont.ARIAL, 15);
            BarCodeFont 
    = new WritableFont(WritableFont.ARIAL, 15, WritableFont.BOLD);
            
            
    try {
                
    /** ********* 初始化單元格屬性 **************** */
                
    // 初始化標(biāo)題樣式
                wcf_header = new WritableCellFormat(BlodFont, NumberFormats.TEXT) ; // 實(shí)例化單元格格式對(duì)象(標(biāo)題、居中)
                wcf_header.setBorder(Border.ALL, BorderLineStyle.THIN) ;    // 線(xiàn)條
                wcf_header.setVerticalAlignment(VerticalAlignment.CENTRE);     // 垂直對(duì)齊
                wcf_header.setAlignment(Alignment.CENTRE);                     // 水平對(duì)齊
                wcf_header.setBackground(Colour.GRAY_25) ;                    // 背景顏色
                wcf_header.setWrap(true);                                     // 是否換行
                
                
    // 用于正文左
                wcf_left = new WritableCellFormat(NormalFont);                // 實(shí)例化單元格格式對(duì)象(正文、左對(duì)齊)
                wcf_left.setBorder(Border.ALL, BorderLineStyle.THIN);         // 線(xiàn)條
                wcf_left.setVerticalAlignment(VerticalAlignment.CENTRE);     // 垂直對(duì)齊
                wcf_left.setAlignment(Alignment.LEFT);
                wcf_left.setLocked(
    false) ;                                    // 設(shè)置鎖定,還得設(shè)置SheetSettings啟用保護(hù)和設(shè)置密碼
                wcf_left.setWrap(true);                                     // 是否換行
                
                
    // 用于條形碼格式
                wcf_barcode = new WritableCellFormat(BarCodeFont);            // 實(shí)例化單元格格式對(duì)象(第一列、條形碼)
                wcf_barcode.setBorder(Border.ALL, BorderLineStyle.THIN);     // 線(xiàn)條
                wcf_barcode.setVerticalAlignment(VerticalAlignment.CENTRE); // 垂直對(duì)齊
                wcf_barcode.setAlignment(Alignment.CENTRE);
                wcf_barcode.setLocked(
    true) ;                                // 設(shè)置鎖定,還得設(shè)置SheetSettings啟用保護(hù)和設(shè)置密碼
                wcf_barcode.setWrap(true);                                     // 是否換行
            }
     catch (WriteException e) {
                e.printStackTrace();
            }

        }

        
        
        
    // 創(chuàng)建Excle及工作表
        public int createExcle(StringBuffer buf) {
            String barCodes[] 
    = null ;
            
    try {
                
    /** ********** 創(chuàng)建工作薄  ********** **/
                WritableWorkbook workbook 
    = Workbook.createWorkbook(new File("d:" + File.separator + "barcode.xls")) ;
                
    /** ********** 創(chuàng)建工作表  ********** **/
                WritableSheet sheet 
    = workbook.createSheet("條形碼"0) ;
                
                
    /** ********* 打印屬性 **************** */
                sheet.getSettings().setOrientation(PageOrientation.LANDSCAPE) ;    
    // 設(shè)置為橫向打印
                sheet.getSettings().setPaperSize(PaperSize.A4) ;            // 設(shè)置紙張
                sheet.getSettings().setFitHeight(297) ;                        // 打印區(qū)高度
                sheet.getSettings().setFitWidth(210) ;                        // 打印區(qū)寬度    
                
    // 設(shè)置列寬
                sheet.setColumnView(010); // 第1列
                sheet.setColumnView(130); // 第2列
                sheet.setColumnView(215);
                sheet.setColumnView(
    315);
                sheet.setColumnView(
    430);
                sheet.setColumnView(
    540);
                
    // 設(shè)置邊距            
                sheet.getSettings().setTopMargin(0.5) ;
                sheet.getSettings().setBottomMargin(
    0.3) ;
                sheet.getSettings().setLeftMargin(
    0.1) ;
                sheet.getSettings().setRightMargin(
    0.1) ;
                
    // 設(shè)置頁(yè)腳
                sheet.getSettings().getFooter().getCentre().appendPageNumber() ;    // 為頁(yè)腳添加頁(yè)數(shù)
                sheet.getSettings().setFooterMargin(0.07) ;                            // 設(shè)置頁(yè)腳邊距(下)
                
    // 設(shè)置保護(hù),并加密碼 鎖定的Cell才會(huì)起作用
                sheet.getSettings().setProtected(true) ;                            // 啟用保護(hù)
                sheet.getSettings().setPassword("xing5156") ;                        // 設(shè)置保護(hù)密碼
                
    // 設(shè)置打印標(biāo)題行
                sheet.getSettings().setPrintHeaders(true) ;                            // 啟用打印頭信息
                sheet.getSettings().setPrintTitlesRow(00) ;                        // 設(shè)置標(biāo)題行
                
                
    /** *********插入標(biāo)題內(nèi)容**************** */
                sheet.addCell(
    new Label(00"條碼"this.wcf_header));
                sheet.addCell(
    new Label(10"公司"this.wcf_header));
                sheet.addCell(
    new Label(20"部門(mén)"this.wcf_header));
                sheet.addCell(
    new Label(30"姓名"this.wcf_header));
                sheet.addCell(
    new Label(40"類(lèi)型"this.wcf_header));
                sheet.addCell(
    new Label(50"描述"this.wcf_header));

                barCodes 
    = buf.toString().split("") ;
                
    for(int i = 0, y = 1; i < barCodes.length; i++, y++{
                    sheet.setRowView(y, 
    400) ;
                    sheet.addCell(
    new Label(0, y, barCodes[i], this.wcf_barcode)) ;
                    sheet.addCell(
    new Label(1, y, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(2, y, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(3, y, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(4, y, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(5, y, ""this.wcf_left)) ;
                }

                
                
    /** **********以上所寫(xiě)的內(nèi)容都是寫(xiě)在緩存中的,下一句將緩存的內(nèi)容寫(xiě)到文件中******** */
                workbook.write();
                
    /** *********關(guān)閉文件************* */
                workbook.close();
                System.out.println(
    "導(dǎo)出成功");
                
            }
     catch (Exception e) {
                System.out.println(
    "在輸出到EXCEL的過(guò)程中出現(xiàn)錯(cuò)誤,錯(cuò)誤原因:" + e.toString());
            }

            
    return barCodes.length ;
        }

        
    /**
         * 構(gòu)建Workbook對(duì)象, 將內(nèi)容讀取到內(nèi)存中再更新到原來(lái)文件中 
         * 最后關(guān)閉寫(xiě)入操作來(lái)完成文件更新
         * 
    @param buf 該對(duì)象中包含(11AA◎55BC◎523A)類(lèi)似的數(shù)據(jù),以◎拆分
         * 
    @return    返回本次更新條數(shù)
         
    */

        
    public int updateExcle(StringBuffer buf) throws Exception {            // 更新Excle
            
            File file 
    = new File("d:" + File.separator + "barcode.xls") ;
            Workbook wb 
    = null;
            
    if(file.exists()) {
                wb 
    = Workbook.getWorkbook(file);
                WritableWorkbook wwb 
    = Workbook.createWorkbook(file, wb);
                
    // 讀取第一張工作表
                WritableSheet sheet = wwb.getSheet(0) ;
                
                
    int count = sheet.getRows() ;
                String barCodes[] 
    = buf.toString().split("") ;
                
    for(int i = count , y = 0; y < barCodes.length; y++ , i++{
                    sheet.setRowView(i, 
    400) ;
                    sheet.addCell(
    new Label(0, i, barCodes[y], this.wcf_barcode)) ;
                    sheet.addCell(
    new Label(1, i, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(2, i, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(3, i, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(4, i, ""this.wcf_left)) ;
                    sheet.addCell(
    new Label(5, i, ""this.wcf_left)) ;
                }

                wwb.write() ;
                wwb.close() ;
                wb.close() ;
                System.out.println(
    "更新完成!") ;
                
    return barCodes.length ;
            }
     else {
                
    return createExcle(buf) ;
            }

        }

        
        
    // 測(cè)試
    //    public static void main(String args[]) {
    //        StringBuffer buf = new StringBuffer() ;        // 測(cè)試數(shù)據(jù)
    //        buf.append("56KF").append("◎").append("12AA").append("◎").append("23GH").append("◎").append("55JJ") ;
    //        try {
    //            BarToExcle bte = new BarToExcle() ;
    //            bte.updateExcle(buf) ;
    //        } catch (Exception e) {
    //            e.printStackTrace();
    //        }
    //    }
    }


    posted on 2011-11-18 14:04 Solitary 閱讀(28023) 評(píng)論(1)  編輯  收藏 所屬分類(lèi): Tool

    評(píng)論

    # re: JXL(JExcleApi)Excle詳細(xì)使用心得 2015-11-13 10:02 周周周周

    include是自己洗的么、  回復(fù)  更多評(píng)論   


    只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     
    <2015年11月>
    25262728293031
    1234567
    891011121314
    15161718192021
    22232425262728
    293012345

    導(dǎo)航

    統(tǒng)計(jì)

    常用鏈接

    留言簿(1)

    隨筆分類(lèi)

    隨筆檔案

    搜索

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 国产亚洲精品欧洲在线观看| 69视频在线是免费观看| 免费无码肉片在线观看| 亚洲欧洲∨国产一区二区三区| 亚洲欧美日韩久久精品| 2019中文字幕在线电影免费| 国产亚洲?V无码?V男人的天堂| 亚洲欧洲国产综合AV无码久久| 日韩在线永久免费播放| 2048亚洲精品国产| 亚洲第一se情网站| 好男人www免费高清视频在线| 亚洲影院在线观看| 99久久成人国产精品免费 | 亚洲综合无码一区二区三区| 一级毛片高清免费播放| 日本a级片免费看| 亚洲中文字幕无码爆乳app| 18女人水真多免费高清毛片| 日韩亚洲人成在线综合日本| 一级一黄在线观看视频免费| 国产高清免费在线| 亚洲色大成网站www| 91精品免费国产高清在线| 中文字幕亚洲第一在线| 三上悠亚在线观看免费| 亚洲成av人片一区二区三区| 亚洲成a人无码亚洲成www牛牛| 97人妻无码一区二区精品免费| 亚洲一区二区影院| 美女视频黄的免费视频网页 | 中国好声音第二季免费播放| 亚洲国产婷婷香蕉久久久久久| 亚洲AV电影天堂男人的天堂| 成人黄软件网18免费下载成人黄18免费视频| 久久久久无码精品亚洲日韩| 本免费AV无码专区一区| 久久青青草原亚洲av无码| 人人公开免费超级碰碰碰视频 | 免费黄色大片网站| 久久亚洲精品国产精品婷婷|