隨筆-200  評論-148  文章-15  trackbacks-0

          [code]

          /**
          ?*
          ?*/
          package org;

          import java.io.FileOutputStream;

          import org.apache.poi.hssf.usermodel.HSSFCell;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;

          /**
          ?* @author parable
          ?*
          ?*/
          public class TestPoi {

          ??? /**
          ??? ?* @param args
          ??? ?*/
          ??? public static String outputFile = "D:/gongye.xls";

          ??? public static void main(String[] args) {
          ??? ??? // TODO Auto-generated method stub
          ??? ??? try {
          ??? ??? ??? // 創(chuàng)建新的Excel 工作簿
          ??? ??? ??? HSSFWorkbook workbook = new HSSFWorkbook();
          ??? ??? ??? // 在Excel工作簿中建一工作表,其名為缺省值
          ??? ??? ??? // 如要新建一名為"效益指標(biāo)"的工作表,其語句為:
          ??? ??? ??? // HSSFSheet sheet = workbook.createSheet("效益指標(biāo)");
          ??? ??? ??? HSSFSheet sheet = workbook.createSheet();
          ??? ??? ??? // 在索引0的位置創(chuàng)建行(最頂端的行)
          ??? ??? ??? HSSFRow row = sheet.createRow((short) 0);
          ??? ??? ??? // 在索引0的位置創(chuàng)建單元格(左上端)
          ??? ??? ??? HSSFCell cell = row.createCell((short) 0);
          ??? ??? ??? // 定義單元格為字符串類型???
          ??? ??? ??? cell.setEncoding(HSSFCell.ENCODING_UTF_16);
          ??? ??? ??? cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          ??? ??? ??? // 在單元格中輸入一些內(nèi)容
          ??? ??? ??? cell.setCellValue("青山不老");
          ??? ??? ??? // 新建一輸出文件流
          ??? ??? ??? FileOutputStream fOut = new FileOutputStream(outputFile);
          ??? ??? ??? // 把相應(yīng)的Excel 工作簿存盤
          ??? ??? ??? workbook.write(fOut);
          ??? ??? ??? fOut.flush();
          ??? ??? ??? // 操作結(jié)束,關(guān)閉文件
          ??? ??? ??? fOut.close();
          ??? ??? ??? System.out.println("文件生成...");

          ??? ??? } catch (Exception e) {
          ??? ??? ??? System.out.println("已運行 xlCreate() : " + e);
          ??? ??? }

          ??? }

          }
          /**
          ?*
          ?*/
          package org;

          import java.io.FileInputStream;

          import org.apache.poi.hssf.usermodel.HSSFCell;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;

          /**
          ?* @author parable
          ?*
          ?*/
          public class TestReadPoi {

          ??? /**
          ??? ?* @param args
          ??? ?*/
          ??? /** Excel文件的存放位置。注意是正斜線 */
          ??? public static String fileToBeRead = "D:/gongye.xls";

          ??? public static void main(String[] args) {
          ??? ??? // TODO Auto-generated method stub
          ??? ??? try {
          ??? ??? ??? // 創(chuàng)建對Excel工作簿文件的引用
          ??? ??? ??? HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
          ??? ??? ???
          ??? ??? ??? // 創(chuàng)建對工作表的引用。
          ??? ??? ??? // 本例是按名引用(讓我們假定那張表有著缺省名"Sheet1")
          ??? ??? ??? HSSFSheet sheet = workbook.getSheetAt(0);//workbook.getSheet("Sheet0");
          ??? ??? ??? // 也可用getSheetAt(int index)按索引引用,
          ??? ??? ??? // 在Excel文檔中,第一張工作表的缺省索引是0,
          ??? ??? ??? // 其語句為:HSSFSheet sheet = workbook.getSheetAt(0);
          ??? ??? ??? // 讀取左上端單元
          ??? ??? ??? HSSFRow row = sheet.getRow(0);??? ??? ???
          ??? ??? ??? HSSFCell cell = row.getCell((short) 0);
          ??? ??? ??? // 輸出單元內(nèi)容,cell.getStringCellValue()就是取所在單元的值
          ??? ??? ??? System.out.println("左上端單元是: " + cell.getStringCellValue());
          ??? ??? ??? HSSFSheet sheet1 = workbook.getSheetAt(1);
          ??? ??? ??? HSSFRow row1=sheet1.getRow(0);
          ??? ??? ??? HSSFCell cell1 = row1.getCell((short) 0);
          ??? ??? ??? System.out.println("左上端單元是: " + cell1.getStringCellValue());
          ??? ??? ???
          ??? ??? } catch (Exception e) {
          ??? ??? ??? System.out.println("已運行xlRead() : " + e);
          ??? ??? }
          ??? }

          }
          [/code]

          posted on 2006-12-04 12:44 無聲 閱讀(391) 評論(0)  編輯  收藏 所屬分類: 職場生活
          主站蜘蛛池模板: 绥阳县| 宽城| 吉木萨尔县| 铜川市| 滦平县| 个旧市| 珠海市| 灵石县| 嵩明县| 营口市| 洱源县| 灵丘县| 德清县| 华蓥市| 资阳市| 辛集市| 长宁区| 砀山县| 漳平市| 平顶山市| 马关县| 什邡市| 鲁山县| 莲花县| 阳城县| 房山区| 凯里市| 凤阳县| 怀安县| 古蔺县| 连江县| 左权县| 潞西市| 建德市| 昭觉县| 景谷| 绥化市| 蓬溪县| 霍城县| 舒兰市| 江山市|