隨筆-204  評論-90  文章-8  trackbacks-0
              通過jxl.jar讀寫Excel:
          package com.pub.util;

          import java.io.File;
          import java.io.IOException;
          import java.util.Vector;

          import jxl.Sheet;
          import jxl.Workbook;
          import jxl.read.biff.BiffException;
          import jxl.write.Label;
          import jxl.write.WritableSheet;
          import jxl.write.WritableWorkbook;

          public class ExlUtil {

              
          public static Vector readExl(String fileName) {
                  Vector vec1 
          = null;
                  Vector vec2 
          = new Vector();
                  File f 
          = new File(fileName);

                  
          try {
                      
          // 構建Workbook對象, 只讀Workbook對象
                      Workbook workbook = Workbook.getWorkbook(f);
                      
          // 獲取第一張Sheet表
                      Sheet sheet = workbook.getSheet(0);
                      
          // 獲取行數
                      int rows = sheet.getRows();
                      
          // 獲取列數
                      int columns = sheet.getColumns();
                      
          for (int i = 0; i < rows; i++) {
                          vec1 
          = new Vector();
                          
          for (int j = 0; j < columns; j++) {
                              
          // 獲取j列i行的值
                              String cbxmdm = sheet.getCell(j, i).getContents();
                              
          if (cbxmdm == null)
                                  cbxmdm 
          = "";
                              vec1.add(cbxmdm);
                          }
                          
          if (vec1 != null)
                              vec2.add(vec1);
                      }
                  } 
          catch (BiffException e) {
                      e.printStackTrace();
                  } 
          catch (IOException e) {
                      e.printStackTrace();
                  }
                  
          return vec2;

              }

              
          public static void writeExl(String fileName, Vector vec) {
                  WritableWorkbook book 
          = null;
                  WritableSheet sheet 
          = null;
                  
          try {
                      book 
          = Workbook.createWorkbook(new File(fileName));

                      
          // 生成名為“第一頁”的工作表,參數0表示這是第一頁
                      sheet = book.createSheet("第一頁"0);

                      
          // 打開文件
                      for (int i = 0; i < vec.size(); i++) {
                          
          // 在Label對象的構造子中指名單元格位置是第一列第一行(0,0)
                          Vector vec2 = (Vector) vec.get(i);
                          
          for (int j = 0; j < vec2.size(); j++) {
                              
          // 第i行第j列
                              Label label = new Label(j, i, (String) vec2.get(j));
                              
          // 將定義好的單元格添加到工作表中
                              sheet.addCell(label);
                          }
                      }
                      
          // 寫入數據并關閉文件
                      book.write();
                      book.close();

                  } 
          catch (Exception e) {
                      e.printStackTrace();
                  }
              }
              
              
          public static void main(String[] args) {
                  ExlUtil.writeExl(
          "f:/new.xls", ExlUtil.readExl("f:/xx.xls"));
              }
          }


          posted on 2008-01-21 16:37 一凡 閱讀(397) 評論(0)  編輯  收藏 所屬分類: JAVA 基礎
          主站蜘蛛池模板: 白城市| 彩票| 云南省| 修水县| 宁城县| 榆林市| 宁明县| 大名县| 革吉县| 汝州市| 郁南县| 无极县| 瑞昌市| 甘南县| 乌拉特前旗| 丹凤县| 英超| 叶城县| 四子王旗| 义马市| 乳山市| 明星| 天全县| 武宁县| 屏边| 礼泉县| 上虞市| 遵义县| 嵊泗县| 成都市| 马公市| 新密市| 成安县| 阿拉善盟| 驻马店市| 华宁县| 吉木萨尔县| 安阳县| 长宁县| 临洮县| 沁水县|