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

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

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

    乖,別哭的薄殼
    ~一份耕耘,一份收獲~
    posts - 23,comments - 260,trackbacks - 0

    有時候我們會需要做excel的報表,下面這個例子詳細的給出了讀寫及定義樣式的方法。
    (jexcel下載)
    package com.test;

    import java.io.File;
    import java.io.IOException;

    import jxl.Cell;
    import jxl.Sheet;
    import jxl.Workbook;
    import jxl.format.Border;
    import jxl.format.BorderLineStyle;
    import jxl.format.Colour;
    import jxl.read.biff.BiffException;
    import jxl.write.Label;
    import jxl.write.WritableCellFormat;
    import jxl.write.WritableFont;
    import jxl.write.WritableSheet;
    import jxl.write.WritableWorkbook;
    import jxl.write.WriteException;
    import jxl.write.biff.RowsExceededException;

    /**
    ?*
    ?* CopyRight (C) www.tkk7.com/ilovezmh? All rights reserved.<p>
    ?*
    ?* WuHan Inpoint Information Technology Development,Inc.<p>
    ?*
    ?* Author zhu<p>
    ?*
    ?* @version 1.0??? 2007-2-6
    ?*
    ?* <p>Base on : JDK1.5<p>
    ?*
    ?*/

    public class JexcelSample {
    ?
    ?/**
    ? * 寫excel文件
    ? *
    ? */
    ?public void writeExc(File filename){
    ??WritableWorkbook wwb = null;
    ??try
    ??{
    ???wwb = Workbook.createWorkbook(filename); ??
    ??}
    ??catch (Exception e){
    ???e.printStackTrace();
    ??}
    ??
    ??//創建Excel工作表
    ??WritableSheet ws = wwb.createSheet("通訊錄", 0);//創建sheet
    ??try {
    ???ws.mergeCells(0, 0, 2, 1);//合并單元格(左列,左行,右列,右行)從第1行第1列到第2行第3列
    ???Label header = new Label(0, 0, "通訊錄(191026班)", getHeader());
    ???ws.addCell(header);//寫入頭
    ???Label l = new Label(0, 2, "姓名", getTitle());//第3行
    ???ws.addCell(l);
    ???l = new Label(1, 2, "電話", getTitle());
    ???ws.addCell(l);
    ???l = new Label(2, 2, "地址", getTitle());
    ???ws.addCell(l);
    ???l = new Label(0, 3, "小祝", getNormolCell());//第4行
    ???ws.addCell(l);
    ???l = new Label(1, 3, "1314***0974", getNormolCell());
    ???ws.addCell(l);
    ???l = new Label(2, 3, "武漢武昌", getNormolCell());
    ???ws.addCell(l);
    ???l = new Label(0, 4, "小施", getNormolCell());//第5行
    ???ws.addCell(l);
    ???l = new Label(1, 4, "1347***5057", getNormolCell());
    ???ws.addCell(l);
    ???l = new Label(2, 4, "武漢武昌", getNormolCell());
    ???ws.addCell(l);
    ???ws.setColumnView(0,20);//設置列寬
    ???ws.setColumnView(1,20);
    ???ws.setColumnView(2,40);
    ???ws.setRowView(0,400);//設置行高
    ???ws.setRowView(1,400);
    ???ws.setRowView(2,500);
    ???ws.setRowView(3,500);
    ???ws.setRowView(4,500);
    ??} catch (RowsExceededException e1) {
    ???e1.printStackTrace();
    ??} catch (WriteException e1) {
    ???e1.printStackTrace();
    ??}
    ??
    ??//輸出流
    ??try {
    ???wwb.write();
    ??} catch (IOException ex) {
    ???// TODO 自動生成 catch 塊
    ???ex.printStackTrace();
    ??}
    ??//關閉流
    ??try {
    ???wwb.close();
    ??} catch (WriteException ex) {
    ???// TODO 自動生成 catch 塊
    ???ex.printStackTrace();
    ??} catch (IOException ex) {
    ???// TODO 自動生成 catch 塊
    ???ex.printStackTrace();
    ??}
    ??//outStream.close();
    ??System.out.println("寫入成功!\n");
    ?}
    ?
    ?public void readExc(File filename) throws BiffException, IOException{

    ??? Workbook wb = Workbook.getWorkbook(filename);
    ??? Sheet s = wb.getSheet(0);//第1個sheet
    ??? Cell c = null;
    ??? int row = s.getRows();//總行數
    ??? int col = s.getColumns();//總列數
    ??? for(int i=0;i<row;i++){
    ???? for(int j=0;j<col;j++){
    ????? c = s.getCell(j,i);
    ????? System.out.print(c.getContents()+"? ");
    ???? }
    ???? System.out.println();
    ??? }??
    ?}
    ?
    ?/**
    ? * 設置頭的樣式
    ? * @return
    ? */
    ?public static WritableCellFormat getHeader(){
    ??WritableFont font = new? WritableFont(WritableFont.TIMES, 24 ,WritableFont.BOLD);//定義字體
    ??try {
    ???font.setColour(Colour.BLUE);//藍色字體
    ??} catch (WriteException e1) {
    ???// TODO 自動生成 catch 塊
    ???e1.printStackTrace();
    ??}
    ??WritableCellFormat format = new? WritableCellFormat(font);
    ??try {
    ???format.setAlignment(jxl.format.Alignment.CENTRE);//左右居中
    ???format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);//上下居中
    ???format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);//黑色邊框
    ???format.setBackground(Colour.YELLOW);//黃色背景
    ??} catch (WriteException e) {
    ???// TODO 自動生成 catch 塊
    ???e.printStackTrace();
    ??}
    ??return format;
    ?}
    ?
    ?/**
    ? * 設置標題樣式
    ? * @return
    ? */
    ?public static WritableCellFormat getTitle(){
    ??WritableFont font = new? WritableFont(WritableFont.TIMES, 14);
    ??try {
    ???font.setColour(Colour.BLUE);//藍色字體
    ??} catch (WriteException e1) {
    ???// TODO 自動生成 catch 塊
    ???e1.printStackTrace();
    ??}
    ??WritableCellFormat format = new? WritableCellFormat(font);
    ??
    ??try {
    ???format.setAlignment(jxl.format.Alignment.CENTRE);
    ???format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
    ???format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
    ??} catch (WriteException e) {
    ???// TODO 自動生成 catch 塊
    ???e.printStackTrace();
    ??}
    ??return format;
    ?}
    ?
    ?/**
    ? * 設置其他單元格樣式
    ? * @return
    ? */
    ?public static WritableCellFormat getNormolCell(){//12號字體,上下左右居中,帶黑色邊框
    ??WritableFont font = new? WritableFont(WritableFont.TIMES, 12);
    ??WritableCellFormat format = new? WritableCellFormat(font);
    ??try {
    ???format.setAlignment(jxl.format.Alignment.CENTRE);
    ???format.setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
    ???format.setBorder(Border.ALL,BorderLineStyle.THIN,Colour.BLACK);
    ??} catch (WriteException e) {
    ???// TODO 自動生成 catch 塊
    ???e.printStackTrace();
    ??}
    ??return format;
    ?}
    ?
    ?public static void main(String[] args) throws IOException, BiffException{
    ??JexcelSample js = new JexcelSample();
    ??File f = new File("D:\\address.xls");
    ??f.createNewFile();
    ??js.writeExc(f);
    ??js.readExc(f);
    ?}

    }

    生成的excel表格如下:

    posted on 2007-02-06 13:21 小祝 閱讀(9208) 評論(1)  編輯  收藏 所屬分類: java技術

    FeedBack:
    # re: 用jexcel讀寫excel的.xls文件的例子[未登錄]
    2007-04-17 11:18 |
    挺好  回復  更多評論
      
    主站蜘蛛池模板: 又大又黄又粗又爽的免费视频| 精品亚洲永久免费精品| 亚洲视频在线免费看| 不卡一卡二卡三亚洲| 一级做a爰全过程免费视频毛片| 日韩免费视频播播| 亚洲精华国产精华精华液网站| 四虎www免费人成| 亚洲乱码国产乱码精华| 日本xxwwxxww在线视频免费| 国产精品日本亚洲777| 四虎影永久在线高清免费| 一级毛片人与动免费观看| 国产亚洲自拍一区| 无码人妻一区二区三区免费看| 亚洲国产成人私人影院| 精品福利一区二区三区免费视频| jlzzjlzz亚洲jzjzjz| 天天摸天天操免费播放小视频 | 亚洲XX00视频| 国产人成网在线播放VA免费| 亚洲国产国产综合一区首页| 久久久久久国产a免费观看黄色大片| 亚洲国产精品无码久久98| 亚洲国产一区二区视频网站| 国产真人无码作爱免费视频| 亚洲韩国在线一卡二卡| 国内大片在线免费看| 国产成人精品免费视频大全| 亚洲久本草在线中文字幕| 好爽…又高潮了免费毛片| 一区二区在线视频免费观看| 精品亚洲国产成AV人片传媒| 最近最好的中文字幕2019免费 | 中文字幕免费在线看电影大全| 久久精品国产亚洲av高清漫画| 日韩视频免费一区二区三区| a级毛片毛片免费观看久潮喷 | 三级片免费观看久久| 亚洲精品在线观看视频| 女性自慰aⅴ片高清免费|