guanxf

          我的博客:http://blog.sina.com.cn/17learning

            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
            71 隨筆 :: 1 文章 :: 41 評論 :: 0 Trackbacks
          package com.exl.test;
          import java.awt.Color;
          import java.io.File;
          import jxl.CellView;
          import jxl.Workbook;
          import jxl.format.Alignment;
          import jxl.format.Colour;
          import jxl.format.UnderlineStyle;
          import jxl.write.Label;
          import jxl.write.WritableCellFormat;
          import jxl.write.WritableFont;
          import jxl.write.WritableSheet;
          import jxl.write.WritableWorkbook;
          import com.exl.utils.ColourUtil;
          public class Test {
             public static void main(String[] args) throws Exception {
            String title="報表測試";
            String[] navTitle= {"第一行","第二行","第三行","第四行","第五行","第六行","第七行","第八行"};  
            String[][] content={
            {"1","2","第naionfdapfn三行","第四niaodnfoanfdas行","第noandfoasnjdf五行","第六sdfadsafas行","第afdadfasdfs七a行","第adfasfdasf八行"},
            {"2","2","第三行","第四行","第五行","第六行","第七行","sssssssssss第八sss行"},
            {"3","2","第三行","第四行","第五行","第六行","第七行","第八行sssssssssssss"},
            {"4","2","第三行","第四行","第sssssssssssssss五行","第ssssssssssssssssssss六行","第七行","第八行sssssssss"},
            {"5","2","第三行","第ddddddddddddddddddddddddddddddddddddddddddddddddddddddddd四行","第五行","第六行","第七行","第八行"},
            {"6","2","第三行","第四行","第五行","第六行","第七行","第八行"},
            {"7","2","第三行","第四ddddddddddddddddddddddddddddddd行","第五行","第六行","第七行","第八行"},
            {"8","2","第三行","第四行","第五行","第六行","第七行","第八行"},
            {"9","2","第三行","第ddddddddddddddddddddddddddddddd四行","第五行","第六行","第七行","第八行"},
            {"10","2","第三行","第四行","第五行","第六行","第七行","第八行"},
            {"11","2","第三行","第四行","第五行","第六dddddddddddddd行","第七行","第八行"},
            {"12","2","第三行","第四行","第五行","第六行","第七行","第八行"},
            {"13","2","第三行","第四行","第五行","dddddddddddddddddddddd第六行","第七行","第八行"},
            {"14","2","第三行","第四行","第五行","第dddddddddddddddddddddd六行","第七行","第八行"},
            };  
            String filePath="D:\\DesignSource\\tempT";
            String fileName="NewProject.xls";
            File dir=new  File(filePath);
            if(!dir.isDirectory()){
            dir.mkdirs();
            }
            
                 File file = new File(filePath+"\\"+fileName);
                 WritableWorkbook workbook = Workbook.createWorkbook(file);  
                 WritableSheet sheet = workbook.createSheet("報表統計", 0);  //單元格
                 /**
                  * title
                  */
                 Label lab = null;  
                 WritableFont   wf2   =   new   WritableFont(WritableFont.ARIAL,14,WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK); // 定義格式 字體 下劃線 斜體 粗體 顏色
                 WritableCellFormat wcfTitle = new WritableCellFormat(wf2);
                 wcfTitle.setBackground(jxl.format.Colour.IVORY);  //象牙白
                 wcfTitle.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle邊框
                 //       wcfTitle.setVerticalAlignment(VerticalAlignment.CENTRE); //設置垂直對齊
                 wcfTitle.setAlignment(Alignment.CENTRE); //設置垂直對齊
                 
                 CellView navCellView = new CellView();  
                 navCellView.setAutosize(true); //設置自動大小
                 navCellView.setSize(18);
                 
                 lab = new Label(0,0,title,wcfTitle); //Label(col,row,str);   
                 sheet.mergeCells(0,0,navTitle.length-1,0);
                 sheet.setColumnView(0, navCellView); //設置col顯示樣式
                 sheet.setRowView(0, 1600, false); //設置行高
                 sheet.addCell(lab);  
                 /**
                  * status
                  */
                 
                 
                 /**
                  * nav
                  */
                 jxl.write.WritableFont wfcNav =new jxl.write.WritableFont(WritableFont.ARIAL,12, WritableFont.BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK);
                  WritableCellFormat wcfN=new WritableCellFormat(wfcNav);
                  
                  Color color = Color.decode("#0099cc"); // 自定義的顏色
          workbook.setColourRGB(Colour.ORANGE, color.getRed(),color.getGreen(), color.getBlue());
                 wcfN.setBackground(Colour.ORANGE);
                 wcfN.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle邊框
                 wcfN.setAlignment(Alignment.CENTRE); //設置水平對齊
                 wcfN.setWrap(false); //設置自動換行
                 for(int i=0;i<navTitle.length;i++){
                lab = new Label(i,1,navTitle[i],wcfN); //Label(col,row,str);   
                sheet.addCell(lab);  
                sheet.setColumnView(i, new String(navTitle[i]).length());  
                 }
                 
                 /**
                  * 內容
                  */
                 jxl.write.WritableFont wfcontent =new jxl.write.WritableFont(WritableFont.ARIAL,12, WritableFont.NO_BOLD,false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.GREEN);
                 WritableCellFormat wcfcontent = new WritableCellFormat(wfcontent);
                 wcfcontent.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN,jxl.format.Colour.BLACK); //BorderLineStyle邊框
                 wcfcontent.setAlignment(Alignment.CENTRE);
                 CellView cellView = new CellView();  
                 cellView.setAutosize(true); //設置自動大小
                 for(int i=0;i<content.length;i++){  
                     for(int j=0;j<content[i].length;j++){  
                    sheet.setColumnView(i, cellView);//根據內容自動設置列寬  
                    lab = new Label(j,i+2,content[i][j],wcfcontent); //Label(col,row,str);  
                         sheet.addCell(lab);  
          //               sheet.setColumnView(j, new String(content[i][j]).length());  
                     }  
                 }  
                 
                 workbook.write();  
                 workbook.close();  
          }
          }
          posted on 2013-10-17 01:18 管先飛 閱讀(40052) 評論(1)  編輯  收藏

          評論

          # re: Jxl導出Excel(跨行、設置列寬、設置行高、自動對齊、自定義背景等) 2015-06-09 09:09 wfsfsdf
          wefsfsfsfsfsfs  回復  更多評論
            


          只有注冊用戶登錄后才能發表評論。


          網站導航:
          博客園   IT新聞   Chat2DB   C++博客   博問  
           
          主站蜘蛛池模板: 兴文县| 仪征市| 阳泉市| 建昌县| 西宁市| 衡水市| 承德市| 正蓝旗| 彩票| 克拉玛依市| 漯河市| 珠海市| 定安县| 洛宁县| 孟连| 南川市| 满洲里市| 深水埗区| 东阿县| 伽师县| 台前县| 乌鲁木齐县| 溆浦县| 堆龙德庆县| 三门峡市| 玛曲县| 黑山县| 平舆县| 阿坝县| 深水埗区| 乌海市| 昌图县| 双城市| 伊春市| 扬中市| 新巴尔虎右旗| 横峰县| 广宗县| 榆中县| 隆回县| 临江市|