随笔-42  评论-42  文章-0  trackbacks-0

包下载地址:http://www.apache.org/dist/jakarta/poi/release/bin/ xxx .zip

参考:http://www.oracle.com/technology/global/cn/pub/articles/saternos_tables.html 使用 Apache Jakarta POI 从 Excel 电子表格生成外部表
   http://blog.tostudy.com.cn/blog/show_996.html
   http://blog.tostudy.com.cn/blog/show_995.html

一 创建Excel 文档

  示例1将演示如何利用Jakarta POI API 创建Excel 文档。

  示例1程序如下:

 1 package all;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 import org.apache.poi.hssf.usermodel.HSSFCell;
 6 import org.apache.poi.hssf.usermodel.HSSFRow;
 7 import org.apache.poi.hssf.usermodel.HSSFSheet;
 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 9 
10 public class CreateXL {
11 
12     /** Excel 文件要存放的位置,假定在D盘下 */
13 
14     public static String outputFile = "D:\\english.xls";
15 
16     public static void main(String argv[]) {
17 
18         try {
19 
20             // 创建新的Excel 工作簿
21 
22             HSSFWorkbook workbook = new HSSFWorkbook();
23 
24             // 在Excel工作簿中建一工作表,其名为缺省值
25             // 如要新建一名为"效益指标"的工作表,其语句为:
26             // HSSFSheet sheet = workbook.createSheet("效益指标");
27             HSSFSheet sheet = workbook.createSheet("EnglishTable");
28 
29             // 在索引0的位置创建行(最顶端的行)
30 
31             HSSFRow row = sheet.createRow((short0);
32 
33             // 在索引0的位置创建单元格(左上端)
34             HSSFCell cell = row.createCell((short0);
35             // 定义单元格为字符串类型
36             cell.setCellType(HSSFCell.CELL_TYPE_STRING);
37             // 在单元格中输入一些内容
38             cell.setCellValue("增加值");
39             // 新建一输出文件流
40             FileOutputStream fOut = new FileOutputStream(outputFile);
41             // 把相应的Excel 工作簿存盘
42             workbook.write(fOut);
43             fOut.flush();
44             // 操作结束,关闭文件
45             fOut.close();
46             System.out.println("文件生成");
47 
48         } catch (Exception e) {
49             System.out.println("已运行 xlCreate() : " + e);
50         }
51     }
52 }
53 

  

二 读取Excel文档中的数据

  示例2将演示如何读取Excel文档中的数据。假定在D盘JTest目录下有一个文件名为test1.xls的Excel文件。

  示例2程序如下:

 

 1 package all;
 2 
 3 import java.io.FileInputStream;
 4 
 5 import org.apache.poi.hssf.usermodel.HSSFCell;
 6 import org.apache.poi.hssf.usermodel.HSSFRow;
 7 import org.apache.poi.hssf.usermodel.HSSFSheet;
 8 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
 9 
10 public class ReadXL {
11     /**
12      * Excel文件的存放位置:注意是两个反斜线。 或者可以用一个正斜线 D:/test.xls
13      * */
14     public static String fileToBeRead = "D:\\test.xls";
15 
16     public static void main(String argv[]) {
17         try {
18             // 创建对Excel工作簿文件的引用
19             HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(fileToBeRead));
20             // 创建对工作表的引用。
21             // 本例是按名引用(让我们假定那张表有着缺省名"Sheet1")
22             HSSFSheet sheet = workbook.getSheet("Sheet");
23             // 也可用getSheetAt(int index)按索引引用,
24             // 在Excel文档中,第一张工作表的缺省索引是0,
25             // 其语句为:HSSFSheet sheet = workbook.getSheetAt(0);
26             // 读取左上端单元
27             HSSFRow row = sheet.getRow(0);
28             HSSFCell cell = row.getCell((short0);
29             // 输出单元内容,cell.getStringCellValue()就是取所在单元的值
30             System.out.println("左上端单元是: " + cell.getStringCellValue());
31         } catch (Exception e) {
32             System.out.println("已运行xlRead() : " + e);
33             e.printStackTrace();
34         }
35     }
36 }
37 



三 设置单元格格式

  在这里,我们将只介绍一些和格式设置有关的语句,我们假定workbook就是对一个工作簿的引用。在Java中,第一步要做的就是创建和设置字体和单元格的格式,然后再应用这些格式:

  1、创建字体,设置其为红色、粗体、字号大小为18

HSSFFont font = workbook.createFont();
font.setColor(HSSFFont.COLOR_RED);
font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
font.setFontHeightInPoints((short) 18);

  2、创建格式

HSSFCellStyle cellStyle= workbook.createCellStyle();
cellStyle.setFont(font);

  3、应用格式

HSSFCell cell = row.createCell((short) 0);
cell.setCellStyle(cellStyle);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("标题 "); 


四 处理WORD文档(还没弄明白关于word文档,有待学习)

import java.io.*;
import org.textmining.text.extraction.WordExtractor;
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;

public class TestPoi {
public TestPoi() {
}
public static void main(String args[]) throws Exception
{
FileInputStream in = new FileInputStream ("D:\\a.doc");
WordExtractor extractor = new WordExtractor();
String str = extractor.extractText(in);
//System.out.println("the result length is"+str.length());
System.out.println(str);
}
}
向EXCEL文件中导入数据以及读取Excel文档中的数据。

posted on 2008-08-04 22:15 BlueSunshine 阅读(424) 评论(1)  编辑  收藏 所属分类: 学习心得

评论:
# re: 生成 Excel文件 2008-08-04 22:24 | BlueSunshine
做了个练习:

 1 package all;
 2 
 3 import java.io.FileOutputStream;
 4 
 5 import org.apache.poi.hssf.usermodel.HSSFFont;
 6 import org.apache.poi.hssf.usermodel.HSSFRichTextString;
 7 import org.apache.poi.hssf.usermodel.HSSFRow;
 8 import org.apache.poi.hssf.usermodel.HSSFSheet;
 9 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
10 
11 public class CreateExcel {
12     public static void main(String[] args) {
13         try {
14             HSSFWorkbook workbook = new HSSFWorkbook();
15             HSSFSheet sheet = workbook.createSheet("mySheet1");
16 
17             HSSFRow row0 = sheet.createRow(0);
18             HSSFRichTextString arg0 = new HSSFRichTextString("number");
19             HSSFFont font = workbook.createFont();
20             font.setColor(HSSFFont.COLOR_RED);
21             // bold : 粗体
22             font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
23             // 字号
24             font.setFontHeightInPoints((short18);
25             arg0.applyFont(font);
26 
27             row0.createCell((short0).setCellValue(arg0);
28             row0.createCell((short1).setCellValue("name");
29             row0.createCell((short2).setCellValue("content");
30 
31             HSSFRow row1 = sheet.createRow(1);
32             row1.createCell((short0).setCellValue("1");
33             row1.createCell((short1).setCellValue("one");
34             row1.createCell((short2).setCellValue("This is one.");
35 
36             HSSFRow row2 = sheet.createRow(2);
37             row2.createCell((short0).setCellValue("2");
38             row2.createCell((short1).setCellValue("two");
39             row2.createCell((short2).setCellValue("This 是 two.");
40 
41             HSSFRow row3 = sheet.createRow(3);
42             row3.createCell((short0).setCellValue("3");
43             row3.createCell((short1).setCellValue("哈哈");
44             row3.createCell((short2).setCellValue("哈哈是pig。");
45 
46             FileOutputStream fileOutputStream = new FileOutputStream("E:\\myExcel1.xls");
47             workbook.write(fileOutputStream);
48             fileOutputStream.flush();
49             fileOutputStream.close();
50             System.out.println("ok");
51         } catch (Exception e) {
52             System.out.println("no create" + e);
53         }
54     }
55 }
56 

HSSFCell 的 sellCellValue(Stirng value) 方法不提倡使用,所以根据 API 改用sellCellValue(HSSFRiceTextString value)方法。第二个方法具体用法见上例18-27行。

建出的Excel文件:


  如果要创建的Excel文档已存在,那么将修改当前的Excel文档。   回复  更多评论
  

只有注册用户登录后才能发表评论。


网站导航: