0734w-月下竹音

          0734是來自家鄉(xiāng)的聲音

          常用鏈接

          統(tǒng)計(jì)

          最新評(píng)論

          用POI創(chuàng)建EXCEL表格的幾個(gè)例子

          POI為應(yīng)用最廣的,EXCEL報(bào)表生成工具,可以在http://www.apache.org/dyn/closer.cgi/jakarta/poi/上得到最新的下載包.
          一,生成一個(gè)簡單的EXCEL文件;
          import org.apache.poi.hssf.usermodel.HSSFWorkbook;
          import org.apache.poi.hssf.usermodel.HSSFSheet;
          import org.apache.poi.hssf.usermodel.HSSFRow;
          import org.apache.poi.hssf.usermodel.HSSFCell;
          import java.io.FileOutputStream;

          public class CreateXL {

          ?/** Excel 文件要存放的位置 */
          ?public static String outputFile = "c:/ excel.xls";

          ?public static void main(String args[]) {

          ??try {

          ???// 創(chuàng)建新的Excel 工作簿

          ???HSSFWorkbook workbook = new HSSFWorkbook();

          ???// 在Excel工作簿中建一工作表,其名為缺省值
          ???// 如要新建一名為"效益指標(biāo)"的工作表,其語句為:
          ???HSSFSheet sheet = workbook.createSheet("Sheet1");
          ???// HSSFSheet sheet = workbook.createSheet();

          ???// 在索引0的位置創(chuàng)建行(最頂端的行)

          ???HSSFRow row = sheet.createRow((short) 0);
          ???// 在索引0的位置創(chuàng)建單元格(左上端)
          ???HSSFCell cell = row.createCell((short) 0);
          ???// 定義單元格為字符串類型
          ???cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          ???// 在單元格中輸入一些內(nèi)容
          ???cell.setCellValue(" number 1");
          ???HSSFCell cell01 = row.createCell((short)1);
          ???// 定義單元格為字符串類型
          ???cell01.setCellType(HSSFCell.CELL_TYPE_STRING);
          ???// 在單元格中輸入一些內(nèi)容
          ???cell01.setCellValue(" number2");???
          ???
          ???
          ???// 新建一輸出文件流
          ???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("已運(yùn)行 xlCreate() : " + e);
          ??}
          ?}
          }

          二生成一個(gè)包含中文的EXCEL文件;

          import java.io.FileOutputStream;
          import java.io.IOException;
          import java.util.Date;

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

          public class CreateCells

          {

          ?public static void main(String[] args)

          ?throws IOException

          ?{

          ??HSSFWorkbook wb = new HSSFWorkbook();// 建立新HSSFWorkbook對(duì)象

          ??HSSFSheet sheet = wb.createSheet("new sheet");// 建立新的sheet對(duì)象

          ??// Create a row and put some cells in it. Rows are 0 based.

          ??HSSFRow row = sheet.createRow((short) 0);// 建立新行

          ??// Create a cell and put a value in it.

          ??HSSFCell cell = row.createCell((short) 0);// 建立新cell

          ??cell.setCellValue(1);// 設(shè)置cell的整數(shù)類型的值

          ??// Or do it on one line.

          ??row.createCell((short) 1).setCellValue(1.2);// 設(shè)置cell浮點(diǎn)類型的值

          ??row.createCell((short) 2).setCellValue("test");// 設(shè)置cell字符類型的值

          ??row.createCell((short) 3).setCellValue(true);// 設(shè)置cell布爾類型的值

          ??HSSFCellStyle cellStyle = wb.createCellStyle();// 建立新的cell樣式

          ??cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));// 設(shè)置cell樣式為定制的日期格式

          ??HSSFCell dCell = row.createCell((short) 4);

          ??dCell.setCellValue(new Date());// 設(shè)置cell為日期類型的值

          ??dCell.setCellStyle(cellStyle); // 設(shè)置該cell日期的顯示格式

          ??HSSFCell csCell = row.createCell((short) 5);

          ??csCell.setEncoding(HSSFCell.ENCODING_UTF_16);// 設(shè)置cell編碼解決中文高位字節(jié)截?cái)?/p>

          ??csCell.setCellValue("中文測(cè)試_Chinese Words Test");// 設(shè)置中西文結(jié)合字符串

          ??row.createCell((short) 6).setCellType(HSSFCell.CELL_TYPE_ERROR);// 建立錯(cuò)誤cell

          ??// Write the output to a file

          ??FileOutputStream fileOut = new FileOutputStream("c:/workbook.xls");

          ??wb.write(fileOut);

          ??fileOut.close();

          ?}

          }
          三讀取EXCEL文件;

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

          public class ReadXL {
          ?/** Excel文件的存放位置。注意是正斜線 */
          ?public static String fileToBeRead =? "c:/ excel.xls";

          ?public static void main(String args[]) {
          ??try {
          ???// 創(chuàng)建對(duì)Excel工作簿文件的引用
          ???HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(
          ?????fileToBeRead));
          ???// 創(chuàng)建對(duì)工作表的引用。
          ???// 本例是按名引用(讓我們假定那張表有著缺省名"Sheet1")
          ???HSSFSheet sheet = workbook.getSheet("Sheet1");
          ???// 也可用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());
          ??} catch (Exception e) {
          ???System.out.println("已運(yùn)行xlRead() : " + e);
          ??}
          ?}
          }

          以下是一些應(yīng)用:
           1、創(chuàng)建字體,設(shè)置其為紅色、粗體:

          HSSFFont font = workbook.createFont();
          font.setColor(HSSFFont.COLOR_RED);
          font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
            2、創(chuàng)建格式

          HSSFCellStyle cellStyle= workbook.createCellStyle();
          cellStyle.setFont(font);
            3、應(yīng)用格式

          HSSFCell cell = row.createCell((short) 0);
          cell.setCellStyle(cellStyle);
          cell.setCellType(HSSFCell.CELL_TYPE_STRING);
          cell.setCellValue("標(biāo)題 ");  

          posted on 2006-04-24 15:52 sparkwu 閱讀(5954) 評(píng)論(1)  編輯  收藏

          評(píng)論

          # re: 用POI創(chuàng)建EXCEL表格的幾個(gè)例子 2006-12-04 16:06 ice

          不錯(cuò)!  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 桑日县| 平乐县| 遵义市| 卢龙县| 基隆市| 原阳县| 南和县| 陆川县| 苏尼特左旗| 阜新市| 大姚县| 屯留县| 新宁县| 五家渠市| 安泽县| 璧山县| 大埔区| 平谷区| 万荣县| 静乐县| 大同市| 台南市| 马龙县| 南川市| 民县| 广饶县| 仪征市| 丰台区| 水富县| 合肥市| 裕民县| 离岛区| 历史| 阳江市| 高青县| 庆城县| 富裕县| 雅江县| 康保县| 洞头县| 赤壁市|