小石頭
          Excellence in any department can be attained only by the labor of a lifetime; it is not to be purchased at a lesser price.
          posts - 91,comments - 22,trackbacks - 0
          前些日子寫了一些關于EXCEL解決的方法,今天用JXL寫文件的方法,來處理。把封裝好的東西發上來,大家一起看看。

          import java.util.*;
          import java.io.*;
          import java.sql.*;
          import jxl.*;
          import jxl.write.*;

          /**
          ?* 提供了常用的Excel讀取和寫入的方法
          ?* <p>?* Title:?* </p>
          ?* <p>?* Description:?* </p>
          ?* <p>?* Copyright: Copyright (c) 2006?* </p>
          ?* <p>?* Company:?* </p>
          ?*??* @author?wujiaqian?* @version 1.0
          ?*/

          public class Excel {
          ?int sheetCount = 1; // excel工作簿,默認為1

          ?WritableWorkbook wwb = null; // 構建Workbook對象,只讀Workbook對象

          ?Vector vSheet = null;

          ?/**
          ? * 無參構造函數,生成默認名字的excel文件
          ? */
          ?public Excel() {
          ??this("noName.excel");
          ?}

          ?/**
          ? * 帶有一個String類型參數的構造函數
          ? *
          ? * @param fileName
          ? *?String 將要生成的excel文件名
          ? */
          ?public Excel(String fileName) {
          ??try {
          ???wwb = Workbook.createWorkbook(new File(fileName));
          ???vSheet = new Vector(5);
          ??} catch (Exception e) {
          ??}
          ?}

          ?/**
          ? * 帶有一個File類型參數的構造函數
          ? *
          ? * @param fileName
          ? *??String 將要生成的excel文件名
          ? */
          ?public Excel(File file) {
          ??try {
          ???wwb = Workbook.createWorkbook(file);
          ???vSheet = new Vector(5);
          ??} catch (Exception e) {
          ??}
          ?}

          ?/**
          ? * 讀取一個EXCEL文件的所有行和列,在同一行上的數據以一個String的形式保存在Vector中,各列數據以","號分割
          ? *
          ? * @param fileName
          ? *?????? ?String 文件名
          ? * @throws Exception
          ? * @return Vector
          ? */
          ?public static Vector readFromExcel(String fileName) throws Exception {
          ??Vector v = new Vector();
          ??File file = new File(fileName);
          ??if (!file.isFile()) {
          ???return null;
          ??}
          ??v = readExl(file, -1, -1);
          ??return v;
          ?}

          ?public static Vector readFromExcel(File file) throws Exception {
          ??Vector v = new Vector();
          ??if (!file.isFile()) {
          ???return null;
          ??}
          ??v = readExl(file, -1, -1);
          ??return v;
          ?}

          ?/**
          ? * 讀取一行多列或者一列多行
          ? *
          ? * @param fileName
          ? *????????String 文件名
          ? * @param rowORcol
          ? *????????int 第幾行或者第幾列
          ? * @param flag
          ? *????????String ROW表示前面一個參數的值指的是行數,行COL表示前面一個參數的值指的是列數
          ? * @throws Exception
          ? * @return Vector
          ? */
          ?public static Vector readFromExcel(String fileName, int rowORcol,
          ???String flag) throws Exception {
          ??Vector v = new Vector();
          ??File file = new File(fileName);
          ??if (!file.isFile()) {
          ???return null;
          ??}
          ??if (flag != null && flag.equals("ROW")) {
          ???v = readExl(file, rowORcol, -1);
          ??} else if (flag != null && flag.equals("COL")) {
          ???v = readExl(file, -1, rowORcol);
          ??} else {
          ???return null;
          ??}
          ??return v;
          ?}

          ?public static Vector readFromExcel(File file, int rowORcol, String flag)
          ???throws Exception {
          ??Vector v = new Vector();
          ??if (!file.isFile()) {
          ???return null;
          ??}
          ??if (flag != null && flag.equals("ROW")) {
          ???v = readExl(file, rowORcol, -1);
          ??} else if (flag != null && flag.equals("COL")) {
          ???v = readExl(file, -1, rowORcol);
          ??} else {
          ???return null;
          ??}

          ??return v;
          ?}

          ?/**
          ? * 讀取多行或者多列,可以任意挑選幾行或幾列
          ? *
          ? * @param fileName
          ? *????????String 文件名
          ? * @param rowORcol
          ? *????????int 任意的行或列
          ? * @param flag
          ? *????????String ROW表示行COL表示列
          ? * @throws Exception
          ? * @return Vector
          ? */
          ?public static Vector readFromExcel(String fileName, int[] rowORcol,
          ???String flag) throws Exception {
          ??Vector v = new Vector();
          ??return v;
          ?}

          ?public static Vector readFromExcel(File file, int[] rowORcol, String flag)
          ???throws Exception {
          ??Vector v = new Vector();
          ??return v;
          ?}

          ?/**
          ? * 讀取第幾行第幾列的一個數據
          ? *
          ? * @param fileName
          ? *??????????? String
          ? * @param row
          ? *??????????? int
          ? * @param col
          ? *??????????? int
          ? * @throws Exception
          ? * @return String
          ? */
          ?public static String readFromExcel(String fileName, int row, int col)
          ???throws Exception {
          ??String res = null;
          ??File file = new File(fileName);
          ??if (!file.isFile()) {
          ???return null;
          ??}
          ??return res;
          ?}

          ?public static String readFromExcel(File file, int row, int col)
          ???throws Exception {
          ??String res = null;
          ??if (!file.isFile()) {
          ???return null;
          ??}

          ??return res;
          ?}

          ?/**
          ? * 讀取xls文件
          ? *
          ? * @param f
          ? *????????File 文件
          ? * @param row
          ? *????????int 讀取從0到row行
          ? * @param col
          ? *????????int 讀取從0到col列
          ? * @throws Exception
          ? * @return Vector
          ? */

          ?private static Vector readExl(File f, int row, int col) throws Exception {
          ??Vector v = new Vector();
          ??Workbook wb = null;
          ??Sheet st = null;
          ??wb = Workbook.getWorkbook(f);
          ??st = wb.getSheet(0);
          ??int allRow = st.getRows();
          ??if (row == -1) {
          ???row = allRow;
          ??}
          ??int allCol = st.getColumns();
          ??if (col == -1) {
          ???col = allCol;
          ??}
          ??for (int i = 0; i < row; i++) {
          ???String sRow = null;
          ???for (int j = 0; j < col; j++) {
          ????Cell c1 = st.getCell(j, i);
          ????String sCol = c1.getContents();
          ????if (j == 0) {
          ?????sRow = sCol;
          ????} else {
          ?????if (sCol != null) {
          ??????sRow = sRow + "," + sCol;
          ?????} else {
          ??????sRow = sRow + "," + "";
          ?????}
          ????}
          ????c1 = null;
          ????sCol = null;
          ???}
          ???v.addElement(sRow);
          ???sRow = null;
          ??}

          ??st = null;
          ??wb.close();
          ??wb = null;
          ??return v;
          ?}

          ?public void addSheet() throws Exception {
          ??addSheet(String.valueOf(sheetCount));
          ?}

          ?public void addSheet(String sheetName) throws Exception {

          ??// 創建Excel工作表
          ??WritableSheet ws = wwb.createSheet(sheetName, sheetCount);
          ??vSheet.addElement(ws);
          ??sheetCount++;
          ?}

          ?/**
          ? * 為工作表添加內容,指定添加到第幾列
          ? *
          ? * @param v
          ? *????????Vector 要添加到工作表的內容 格式
          ? *????????String,String,String,String,...,...,...,..., 每列內容以逗號隔開
          ? * @param col
          ? *????????int 指定列數
          ? * @throws Exception
          ? */
          ?public void addContent(Vector v, int col) throws Exception {
          ??WritableSheet ws = (WritableSheet) vSheet.get(sheetCount - 2);

          ??int size = v.size();
          ??try {
          ???for (int i = 0; i < size; i++) {
          ????String s = (String) v.get(i);
          ????String[] s1 = s.split(",");
          ????for (int j = 0; j < col; j++) {
          ?????Label label = new Label(j, i, s1[j]);

          ?????ws.addCell(label);
          ?????label = null;
          ????}
          ???}
          ??} catch (ArrayIndexOutOfBoundsException e) {
          ???throw new ArrayIndexOutOfBoundsException("check column!");
          ??}
          ??ws = null;
          ?}

          ?/**
          ? * 為工作表添加內容,不指定添加幾列
          ? *
          ? * @param v
          ? *????????Vector 要添加到工作表的內容 格式
          ? *????????String,String,String,String,...,...,...,..., 每列內容以逗號隔開
          ? * @throws Exception
          ? */
          ?public void addContent(Vector v) throws Exception {
          ??WritableSheet ws = (WritableSheet) vSheet.get(sheetCount - 2);

          ??int size = v.size();
          ??try {
          ???for (int i = 0; i < size; i++) {
          ????String s = (String) v.get(i);
          ????String[] s1 = s.split(",");
          ????int col_size = s1.length;
          ????for (int j = 0; j < col_size; j++) {
          ?????Label label = new Label(j, i, s1[j]);

          ?????ws.addCell(label);
          ?????label = null;
          ????}
          ???}
          ??} catch (Exception e) {
          ???throw new Exception();
          ??}
          ??ws = null;
          ?}

          ?/**
          ? * 為工作表添加內容,不指定添加幾列
          ? *
          ? * @param rs
          ? *????????ResultSet 從數據庫中得到的結果集
          ? * @throws Exception
          ? */
          ?public void addContent(ResultSet rs) throws Exception {
          ??if (rs == null) {
          ???return;
          ??}
          ??WritableSheet ws = (WritableSheet) vSheet.get(sheetCount - 2);
          ??ResultSetMetaData rsMetaD = rs.getMetaData();
          ??int col = rsMetaD.getColumnCount();
          ??int i = 0;
          ??while (rs.next()) {
          ???for (int j = 0; j < col; j++) {
          ????Label label = new Label(j, i, rs.getString(j));// Chinese.fromDatabase(rs.getString(j))
          ????ws.addCell(label);
          ????label = null;
          ???}
          ???i++;
          ??}
          ?}

          ?/**
          ? * 最終生成excel文件
          ? *
          ? * @throws Exception
          ? */
          ?public void createExcel() throws Exception {
          ??wwb.write();
          ??wwb.close();
          ?}

          ?public static void main(String[] args) {
          ??Excel t = new Excel("d:\\test.xls");
          ??Vector v = new Vector();
          ??try {
          ???v.addElement("ding,wen,yuan");
          ???v.addElement("ding,wen,yuan");
          ???t.addSheet("first");
          ???t.addContent(v, 3);

          ???v.clear();
          ???v.addElement("xuhy,hai,yong");
          ???v.addElement("xuhy,hai,yong");
          ???t.addSheet("second");
          ???t.addContent(v, 3);

          ???v.clear();
          ???v.addElement("wu,jia,qian");
          ???v.addElement("wu,jia,qian");
          ???t.addSheet("third");
          ???t.addContent(v, 3);

          ???t.createExcel();
          ??} catch (Exception e) {
          ???e.printStackTrace();
          ??}

          ?}

          }

          轉自:http://www.aygfsteel.com/wujiaqian/archive/2006/12/21/89308.html?Pending=true#Post

          posted on 2007-01-08 15:19 小石頭 閱讀(1183) 評論(0)  編輯  收藏 所屬分類: 轉載區我的java學習
          主站蜘蛛池模板: 洮南市| 铅山县| 喀什市| 金乡县| 浮山县| 闽清县| 永昌县| 彩票| 呼和浩特市| 南丰县| 崇州市| 盐池县| 手游| 崇信县| 广汉市| 渝北区| 虞城县| 五原县| 清苑县| 清新县| 新沂市| 南投市| 泸溪县| 军事| 封开县| 潜山县| 乐山市| 海伦市| 隆安县| 罗城| 东阿县| 青铜峡市| 阿拉善右旗| 亚东县| 桦川县| 连江县| 云和县| 湖州市| 哈尔滨市| 安仁县| 原阳县|