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

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

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

    小石頭
    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.tkk7.com/wujiaqian/archive/2006/12/21/89308.html?Pending=true#Post

    posted on 2007-01-08 15:19 小石頭 閱讀(1182) 評論(0)  編輯  收藏 所屬分類: 轉載區我的java學習
    主站蜘蛛池模板: 免费大片黄手机在线观看| 日韩精品视频免费网址| 国产亚洲?V无码?V男人的天堂| 亚洲综合一区无码精品| 九九精品免费视频| 久久久久精品国产亚洲AV无码| 国产免费一区二区三区| 亚洲一级毛片中文字幕| 好先生在线观看免费播放| 亚洲日本乱码卡2卡3卡新区| 成年女人男人免费视频播放 | 在线成人爽a毛片免费软件| 久久精品国产96精品亚洲| 无码国产精品一区二区免费式芒果| 亚洲AV日韩精品久久久久久 | 久久久久亚洲AV成人无码网站| 黄色免费在线网站| 亚洲欧洲精品国产区| 最近中文字幕mv手机免费高清| 亚洲成a∨人片在无码2023| 免费a级毛片大学生免费观看| xxxx日本在线播放免费不卡| 国产AV无码专区亚洲A∨毛片| 亚洲网站在线免费观看| 亚洲日韩看片无码电影| 亚洲国产精品成人AV无码久久综合影院 | 亚洲国模精品一区| 免费福利在线视频| 亚洲国产理论片在线播放| 日韩免费一区二区三区| 久久精品无码免费不卡| 亚洲精品美女久久久久| 免费人妻无码不卡中文字幕18禁| 99精品视频在线观看免费| 亚洲一区二区三区在线| 亚洲国产成人久久综合碰| 2019中文字幕在线电影免费| 久久久亚洲精华液精华液精华液 | 成年女人男人免费视频播放| 丁香六月婷婷精品免费观看| 亚洲一区中文字幕久久|