<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
    前些日子寫了一些關(guān)于EXCEL解決的方法,今天用JXL寫文件的方法,來處理。把封裝好的東西發(fā)上來,大家一起看看。

    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工作簿,默認(rèn)為1

    ?WritableWorkbook wwb = null; // 構(gòu)建Workbook對(duì)象,只讀Workbook對(duì)象

    ?Vector vSheet = null;

    ?/**
    ? * 無(wú)參構(gòu)造函數(shù),生成默認(rèn)名字的excel文件
    ? */
    ?public Excel() {
    ??this("noName.excel");
    ?}

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

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

    ?/**
    ? * 讀取一個(gè)EXCEL文件的所有行和列,在同一行上的數(shù)據(jù)以一個(gè)String的形式保存在Vector中,各列數(shù)據(jù)以","號(hào)分割
    ? *
    ? * @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表示前面一個(gè)參數(shù)的值指的是行數(shù),行COL表示前面一個(gè)參數(shù)的值指的是列數(shù)
    ? * @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;
    ?}

    ?/**
    ? * 讀取第幾行第幾列的一個(gè)數(shù)據(jù)
    ? *
    ? * @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 {

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

    ?/**
    ? * 為工作表添加內(nèi)容,指定添加到第幾列
    ? *
    ? * @param v
    ? *????????Vector 要添加到工作表的內(nèi)容 格式
    ? *????????String,String,String,String,...,...,...,..., 每列內(nèi)容以逗號(hào)隔開
    ? * @param col
    ? *????????int 指定列數(shù)
    ? * @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;
    ?}

    ?/**
    ? * 為工作表添加內(nèi)容,不指定添加幾列
    ? *
    ? * @param v
    ? *????????Vector 要添加到工作表的內(nèi)容 格式
    ? *????????String,String,String,String,...,...,...,..., 每列內(nèi)容以逗號(hào)隔開
    ? * @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;
    ?}

    ?/**
    ? * 為工作表添加內(nèi)容,不指定添加幾列
    ? *
    ? * @param rs
    ? *????????ResultSet 從數(shù)據(jù)庫(kù)中得到的結(jié)果集
    ? * @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();
    ??}

    ?}

    }

    轉(zhuǎn)自:http://www.tkk7.com/wujiaqian/archive/2006/12/21/89308.html?Pending=true#Post

    posted on 2007-01-08 15:19 小石頭 閱讀(1187) 評(píng)論(0)  編輯  收藏 所屬分類: 轉(zhuǎn)載區(qū)我的java學(xué)習(xí)
    主站蜘蛛池模板: wwwxxx亚洲| 五月婷婷综合免费| 免费国产a国产片高清| 99视频在线精品免费| 国产精品亚洲片在线观看不卡| 又硬又粗又长又爽免费看 | 亚洲国产综合专区电影在线| 久久久久久久久久免免费精品| 亚洲AV无码成H人在线观看| 国产精品亚洲专区无码牛牛| 全部免费毛片免费播放| 成人福利在线观看免费视频| 亚洲乱码中文字幕综合234| 男女拍拍拍免费视频网站| 亚洲AV永久无码精品成人| 91福利视频免费| 亚洲美女高清一区二区三区 | 亚洲人成网7777777国产| 国产无遮挡裸体免费视频在线观看| 久久精品亚洲一区二区| 亚洲人成免费电影| 亚洲AV无码XXX麻豆艾秋| 亚洲一级特黄大片无码毛片 | 亚洲精品亚洲人成在线| 亚洲av无码成人精品区在线播放 | 中文字幕在线免费观看| 亚洲人成77777在线播放网站不卡| 成年午夜视频免费观看视频 | 亚洲综合久久一本伊伊区| 好爽…又高潮了免费毛片| 国产福利免费视频 | 一级特黄录像免费播放肥| 亚洲福利在线观看| 大学生高清一级毛片免费| 国产成人无码精品久久久免费| 久久久久亚洲AV无码专区首| 毛片免费观看的视频在线| 永久免费观看黄网站| 亚洲视频在线不卡| 亚洲精品无码日韩国产不卡?V | 美女在线视频观看影院免费天天看 |