<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

    有時(shí)候我們會(huì)需要做excel的報(bào)表,下面這個(gè)例子詳細(xì)的給出了讀寫(xiě)及定義樣式的方法。
    (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 {
    ?
    ?/**
    ? * 寫(xiě)excel文件
    ? *
    ? */
    ?public void writeExc(File filename){
    ??WritableWorkbook wwb = null;
    ??try
    ??{
    ???wwb = Workbook.createWorkbook(filename); ??
    ??}
    ??catch (Exception e){
    ???e.printStackTrace();
    ??}
    ??
    ??//創(chuàng)建Excel工作表
    ??WritableSheet ws = wwb.createSheet("通訊錄", 0);//創(chuàng)建sheet
    ??try {
    ???ws.mergeCells(0, 0, 2, 1);//合并單元格(左列,左行,右列,右行)從第1行第1列到第2行第3列
    ???Label header = new Label(0, 0, "通訊錄(191026班)", getHeader());
    ???ws.addCell(header);//寫(xiě)入頭
    ???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);//設(shè)置列寬
    ???ws.setColumnView(1,20);
    ???ws.setColumnView(2,40);
    ???ws.setRowView(0,400);//設(shè)置行高
    ???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 自動(dòng)生成 catch 塊
    ???ex.printStackTrace();
    ??}
    ??//關(guān)閉流
    ??try {
    ???wwb.close();
    ??} catch (WriteException ex) {
    ???// TODO 自動(dòng)生成 catch 塊
    ???ex.printStackTrace();
    ??} catch (IOException ex) {
    ???// TODO 自動(dòng)生成 catch 塊
    ???ex.printStackTrace();
    ??}
    ??//outStream.close();
    ??System.out.println("寫(xiě)入成功!\n");
    ?}
    ?
    ?public void readExc(File filename) throws BiffException, IOException{

    ??? Workbook wb = Workbook.getWorkbook(filename);
    ??? Sheet s = wb.getSheet(0);//第1個(gè)sheet
    ??? Cell c = null;
    ??? int row = s.getRows();//總行數(shù)
    ??? int col = s.getColumns();//總列數(shù)
    ??? 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();
    ??? }??
    ?}
    ?
    ?/**
    ? * 設(shè)置頭的樣式
    ? * @return
    ? */
    ?public static WritableCellFormat getHeader(){
    ??WritableFont font = new? WritableFont(WritableFont.TIMES, 24 ,WritableFont.BOLD);//定義字體
    ??try {
    ???font.setColour(Colour.BLUE);//藍(lán)色字體
    ??} catch (WriteException e1) {
    ???// TODO 自動(dòng)生成 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 自動(dòng)生成 catch 塊
    ???e.printStackTrace();
    ??}
    ??return format;
    ?}
    ?
    ?/**
    ? * 設(shè)置標(biāo)題樣式
    ? * @return
    ? */
    ?public static WritableCellFormat getTitle(){
    ??WritableFont font = new? WritableFont(WritableFont.TIMES, 14);
    ??try {
    ???font.setColour(Colour.BLUE);//藍(lán)色字體
    ??} catch (WriteException e1) {
    ???// TODO 自動(dòng)生成 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 自動(dòng)生成 catch 塊
    ???e.printStackTrace();
    ??}
    ??return format;
    ?}
    ?
    ?/**
    ? * 設(shè)置其他單元格樣式
    ? * @return
    ? */
    ?public static WritableCellFormat getNormolCell(){//12號(hào)字體,上下左右居中,帶黑色邊框
    ??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 自動(dòng)生成 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 小祝 閱讀(9222) 評(píng)論(1)  編輯  收藏 所屬分類: java技術(shù)

    FeedBack:
    # re: 用jexcel讀寫(xiě)excel的.xls文件的例子[未登錄](méi)
    2007-04-17 11:18 |
    主站蜘蛛池模板: 中文亚洲AV片在线观看不卡| 啦啦啦完整版免费视频在线观看 | 国产精品免费一级在线观看| 亚洲欧洲日韩在线电影| 国产麻豆成人传媒免费观看| 国产AV无码专区亚洲AV手机麻豆| 一级午夜免费视频| 国产亚洲视频在线播放| 国产高清对白在线观看免费91| 亚洲人成无码www久久久| 视频一区在线免费观看| 亚洲精品97久久中文字幕无码| 在线观看亚洲免费| 狠狠色婷婷狠狠狠亚洲综合| 国产做国产爱免费视频| 久久久久亚洲av无码专区蜜芽| 久久精品私人影院免费看| 久久精品国产亚洲AV电影| 免费A级毛片无码无遮挡内射| 亚洲国产成人99精品激情在线| 大地资源二在线观看免费高清| 亚洲精品久久无码| 久久久久国产亚洲AV麻豆| 国产无遮挡无码视频免费软件| 亚洲一区二区在线视频| 日韩欧毛片免费视频| 精品在线视频免费| 亚洲国产精品无码久久SM| 97国产免费全部免费观看| 亚洲成av人在线观看网站| 亚洲精品高清一二区久久| 一级毛片免费不卡在线| 亚洲依依成人亚洲社区| 亚洲一级黄色视频| 1000部拍拍拍18勿入免费视频下载| 亚洲一卡2卡4卡5卡6卡在线99| 四虎精品亚洲一区二区三区| 国产一级淫片a免费播放口| 亚洲人精品亚洲人成在线| 国产成人亚洲综合| 国产1000部成人免费视频|