本文暂时给出一部分基础运用,其他内容待本人翻译完成之后再写出了。
创建一个新的Workbook
1 HSSFWorkbook wb = new HSSFWorkbook();
2
3 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
4
5 wb.write(fileOut);
6
7 fileOut.close();
8
9
10
创建一个新的 Sheet
1 HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet1 = wb.createSheet("new sheet");
4
5 HSSFSheet sheet2 = wb.createSheet("second sheet");
6
7 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
8
9 wb.write(fileOut);
10
11 fileOut.close();
12
创建单元格 Cells
1 HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet = wb.createSheet("new sheet");
4
5// 创建一个行row同时在上面设置一些单元格,注意,行是从0开始,这里创建第一行。
6
7HSSFRow row = sheet.createRow((short)0);
8
9 // 创建单元格并为他设置一个值,注意,单元格也是从0开始
10
11 HSSFCell cell = row.createCell((short)0);
12
13 cell.setCellValue(1);
14
15 // 可以在同一行创建多个单元格.
16
17 row.createCell((short)1).setCellValue(1.2);
18
19 row.createCell((short)2).setCellValue("This is a string");
20
21 row.createCell((short)3).setCellValue(true);
22
23 // 将这些数据输出为Excel
24
25 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
26
27 wb.write(fileOut);
28
29 fileOut.close();
30
31
创建关于时间的单元格 Cells
1HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet = wb.createSheet("new sheet");
4
5 //创建一个行row同时在上面设置一些单元格,注意,行是从0开始,这里创建第一行
6
7 HSSFRow row = sheet.createRow((short)0);
8
9 // 创建一个单元格并为其设置时间值,第一个单元格是原始时间值Double类型,没有格式
10
11 HSSFCell cell = row.createCell((short)0);
12
13 cell.setCellValue(new Date());
14
15 // 现在将第二个单元格格式化为日期+时间.
16
17// 通过workbook创建一个新的单元格风格(cell style)是很重要的
18
19// 否则你在修改这个单元格的风格的时候可能会影响到其他单元格的风格。
20
21HSSFCellStyle cellStyle = wb.createCellStyle();
22
23 cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
24
25 cell = row.createCell((short)1);
26
27 cell.setCellValue(new Date());
28
29 cell.setCellStyle(cellStyle);
30
31 // 写出文件
32
33 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
34
35 wb.write(fileOut);
36
37 fileOut.close();
38
39
操作不同类型的单元格
1HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet = wb.createSheet("new sheet");
4
5 HSSFRow row = sheet.createRow((short)2);
6
7 row.createCell((short) 0).setCellValue(1.1);
8
9 row.createCell((short) 1).setCellValue(new Date());
10
11 row.createCell((short) 2).setCellValue("a string");
12
13 row.createCell((short) 3).setCellValue(true);
14
15 row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
16
17 // 写出文件
18
19 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
20
21 wb.write(fileOut);
22
23fileOut.close();
24
25
//一点废话:由此可见,在上面的关于日期的格式化我们不一定要通过poi来做,我们可以将数据都格式化好了以后,转换成String类型来写入Excel,这样从一定程度上统一写入单元格的代码,实现代码复用。
示范不同的队列选项(也就是单元格内容居左、居右等等)
1public static void main(String[] args)
2
3 throws IOException
4
5 {
6
7 HSSFWorkbook wb = new HSSFWorkbook();
8
9 HSSFSheet sheet = wb.createSheet("new sheet");
10
11 HSSFRow row = sheet.createRow((short) 2);
12
13 createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
14
15 createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
16
17 createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
18
19 createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
20
21 createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
22
23 createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
24
25 createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
26
27 // 写入文件
28
29 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
30
31 wb.write(fileOut);
32
33 fileOut.close();
34
35 }
36
37 /** *//**
38
39 * 创建一个单元格使其按照某种方式排列
40
41 *
42
43 * @param wb the workbook
44
45 * @param row 生成单元格的行
46
47 * @param column 在这个行中单元格所处的列数
48
49 * @param align 单元格内容的排列方式.
50
51 */
52
53 private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
54
55 {
56
57 HSSFCell cell = row.createCell(column);
58
59 cell.setCellValue("Align It");
60
61 HSSFCellStyle cellStyle = wb.createCellStyle();
62
63 cellStyle.setAlignment(align);
64
65 cell.setCellStyle(cellStyle);
66
67 }
68
69
70
操作边框
1HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet = wb.createSheet("new sheet");
4
5 // 创建一行(row)并为其设置单元格,行从0开始.
6
7 HSSFRow row = sheet.createRow((short) 1);
8
9 // 创建单元格并为其设置数值.
10
11 HSSFCell cell = row.createCell((short) 1);
12
13 cell.setCellValue(4);
14
15 // 在单元格周围设置边框.
16
17 HSSFCellStyle style = wb.createCellStyle();
18
19 style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
20
21 style.setBottomBorderColor(HSSFColor.BLACK.index);
22
23 style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
24
25 style.setLeftBorderColor(HSSFColor.GREEN.index);
26
27 style.setBorderRight(HSSFCellStyle.BORDER_THIN);
28
29 style.setRightBorderColor(HSSFColor.BLUE.index);
30
31 style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
32
33 style.setTopBorderColor(HSSFColor.BLACK.index);
34
35 cell.setCellStyle(style);
36
37 // 写入文件
38
39 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
40
41 wb.write(fileOut);
42
43 fileOut.close();
44
45
46
迭代遍历行及其单元格
有时,我们喜欢遍历一个Sheet的所有行,或者一行的所有单元格,这时可以通过循环来遍历还是比较简单的。
幸运的是,我们这有个非常简单的方法。HSSFRow定义了一个CellIterator的内部类来迭代遍历所有的单元格(通过调用row.celIterator获取),而且HSSFSheet也提供了一个rowIterator方法给出了遍历所有行的迭代器。
(Unfortunately, due to the broken and backwards-incompatible way that Java 5 foreach loops were implemented, it isn't possible to use them on a codebase that supports Java 1.4, as POI does)
1HSSFSheet sheet = wb.getSheetAt(0);
2
3 for (Iterator rit = sheet.rowIterator(); rit.hasNext(); ) {
4
5 HSSFRow row = (HSSFRow)rit.next();
6
7 for (Iterator cit = row.cellIterator(); cit.hasNext(); ) {
8
9 HSSFCell cell = (HSSFCell)cit.next();
10
11 // Do something here
12
13 }
14
15 }
16
17
18
19 HSSFSheet sheet = wb.getSheetAt(0);
20
21 for (Iterator<HSSFRow> rit = (Iterator<HSSFRow>)sheet.rowIterator(); rit.hasNext(); ) {
22
23 HSSFRow row = rit.next();
24
25 for (Iterator<HSSFCell> cit = (Iterator<HSSFCell>)row.cellIterator(); cit.hasNext(); ) {
26
27 HSSFCell cell = cit.next();
28
29 // Do something here
30
31 }
32
33 }
34
35
36
使用java5的特有的前端循环(foreach loops)遍历行和单元格- OOXML Branch Only
有时候我们需要遍历一个Sheet的所有行或者一行的所有列,这时如果使用java5或者更高版本的话,可以采用这种新的循环方式的处理。
幸运的是,这也非常简单。HSSFSheet和HSSFRow都实现了java.lang.Iterator接口来允许新的循环方式的运行。HSSFRow允许通过CellIterator内部类来处理单元格的循环,HSSFSheet给出了rowIterator方法来遍历所有的行。
1 HSSFSheet sheet = wb.getSheetAt(0);
2
3 for (HSSFRow row : sheet.rowIterator()) {
4
5 for (HSSFCell cell : row.cellIterator()) {
6
7 // Do something here
8
9 }
10
11 }
12
13
内容提取
对于大多数的内容提取需求,标准ExcelExtractor类应该能满足您所有的需求。
1InputStream inp = new FileInputStream("workbook.xls");
2
3 HSSFWorkbook wb = new HSSFWorkbook(new POIFSFileSystem(inp));
4
5 ExcelExtractor extractor = new ExcelExtractor(wb);
6
7 extractor.setFormulasNotResults(true);
8
9 extractor.setIncludeSheetNames(false);
10
11 String text = extractor.getText();
12
13
填充和颜色
1HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet = wb.createSheet("new sheet");
4
5 // 创建一行,并为其设置单元格,行从0开始 .
6
7 HSSFRow row = sheet.createRow((short) 1);
8
9 // Aqua 背景色
10
11 HSSFCellStyle style = wb.createCellStyle();
12
13 style.setFillBackgroundColor(HSSFColor.AQUA.index);
14
15 style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
16
17 HSSFCell cell = row.createCell((short) 1);
18
19 cell.setCellValue("X");
20
21 cell.setCellStyle(style);
22
23 // Orange "前景色".
24
25 style = wb.createCellStyle();
26
27 style.setFillForegroundColor(HSSFColor.ORANGE.index);
28
29 style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
30
31 cell = row.createCell((short) 2);
32
33 cell.setCellValue("X");
34
35 cell.setCellStyle(style);
36
37 // 写入文件
38
39 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
40
41 wb.write(fileOut);
42
43 fileOut.close();
44
45
46
合并单元格
1HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet = wb.createSheet("new sheet");
4
5 HSSFRow row = sheet.createRow((short) 1);
6
7 HSSFCell cell = row.createCell((short) 1);
8
9 cell.setCellValue("This is a test of merging");
10
11 sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
12
13 // 写入文件
14
15 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
16
17 wb.write(fileOut);
18
19 fileOut.close();
20
21
一点废话:关于new Region(1,(short)1,1,(short)2):四个参数分别为起始行、起始列、终止行、终止列,如果要在合并的单元格中写入数据,首先在起始行列的单元格中写入内容,之后从起始行列处开始拉伸合并单元格到终止行列。
字体设置
1HSSFWorkbook wb = new HSSFWorkbook();
2
3 HSSFSheet sheet = wb.createSheet("new sheet");
4
5 //创建行
6
7 HSSFRow row = sheet.createRow((short) 1);
8
9 // 创建一个系的呢字体并设置其属性.
10
11 HSSFFont font = wb.createFont();
12
13 font.setFontHeightInPoints((short)24);
14
15 font.setFontName("Courier New");
16
17 font.setItalic(true);
18
19 font.setStrikeout(true);
20
21 // 字体设置给一个HSSFCellStyle对象.
22
23 HSSFCellStyle style = wb.createCellStyle();
24
25 style.setFont(font);
26
27 // 创建一个单元格并为其设置值
28
29 HSSFCell cell = row.createCell((short) 1);
30
31 cell.setCellValue("This is a test of fonts");
32
33 cell.setCellStyle(style);
34
35 //写入文件
36
37 FileOutputStream fileOut = new FileOutputStream("c://workbook.xls");
38
39 wb.write(fileOut);
40
41 fileOut.close();
42
43
注意:一个workbook中字体数目被限制在32767个以内。你应该在程序中重用字体来代替为每个单元格创建新字体
下面写法是错误的:
1for (int i = 0; i < 10000; i++) {
2
3 HSSFRow row = sheet.createRow(i);
4
5 HSSFCell cell = row.createCell((short) 0);
6
7 HSSFCellStyle style = workbook.createCellStyle();
8
9 HSSFFont font = workbook.createFont();
10
11 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
12
13 style.setFont(font);
14
15 cell.setCellStyle(style);
16
17 }
18
19
修正后:
1HSSFCellStyle style = workbook.createCellStyle();
2
3 HSSFFont font = workbook.createFont();
4
5 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
6
7 style.setFont(font);
8
9 for (int i = 0; i < 10000; i++) {
10
11 HSSFRow row = sheet.createRow(i);
12
13 HSSFCell cell = row.createCell((short) 0);
14
15 cell.setCellStyle(style);
16
17 }
18
19
一点废话:将创建字体的代码移出循环体。
如何读取超链接
1HSSFSheet sheet = workbook.getSheetAt(0);
2
3 HSSFCell cell = sheet.getRow(0).getCell((short)0);
4
5 HSSFHyperlink link = cell.getHyperlink();
6
7 if(link != null){
8
9 System.out.println(link.getAddress());
10
11 }
12
13
14
15 HSSFWorkbook wb = new HSSFWorkbook();
16
17 //超链接的单元格风格
18
19 //超链接默认的是蓝色底边框
20
21 HSSFCellStyle hlink_style = wb.createCellStyle();
22
23 HSSFFont hlink_font = wb.createFont();
24
25 hlink_font.setUnderline(HSSFFont.U_SINGLE);
26
27 hlink_font.setColor(HSSFColor.BLUE.index);
28
29 hlink_style.setFont(hlink_font);
30
31 HSSFCell cell;
32
33 HSSFSheet sheet = wb.createSheet("Hyperlinks");
34
35 //URL
36
37 cell = sheet.createRow(0).createCell((short)0);
38
39 cell.setCellValue("URL Link");
40
41 HSSFHyperlink link = new HSSFHyperlink(HSSFHyperlink.LINK_URL);
42
43 link.setAddress("http://poi.apache.org/");
44
45 cell.setHyperlink(link);
46
47 cell.setCellStyle(hlink_style);
48
49 //链接到当前路径的一个文件
50
51 cell = sheet.createRow(1).createCell((short)0);
52
53 cell.setCellValue("File Link");
54
55 link = new HSSFHyperlink(HSSFHyperlink.LINK_FILE);
56
57 link.setAddress("link1.xls");
58
59 cell.setHyperlink(link);
60
61 cell.setCellStyle(hlink_style);
62
63 //链接到e-mail
64
65 cell = sheet.createRow(2).createCell((short)0);
66
67 cell.setCellValue("Email Link");
68
69 link = new HSSFHyperlink(HSSFHyperlink.LINK_EMAIL);
70
71 //note, if subject contains white spaces, make sure they are url-encoded
72
73 link.setAddress("mailto:poi@apache.org?subject=Hyperlinks");
74
75 cell.setHyperlink(link);
76
77 cell.setCellStyle(hlink_style);
78
79 //链接到 workbook的某个地方
80
81 //创建一个目标Sheet和单元格
82
83 HSSFSheet sheet2 = wb.createSheet("Target Sheet");
84
85 sheet2.createRow(0).createCell((short)0).setCellValue("Target Cell");
86
87 cell = sheet.createRow(3).createCell((short)0);
88
89 cell.setCellValue("Worksheet Link");
90
91 link = new HSSFHyperlink(HSSFHyperlink.LINK_DOCUMENT);
92
93 link.setAddress("'Target Sheet'!A1");
94
95 cell.setHyperlink(link);
96
97 cell.setCellStyle(hlink_style);
98
99 FileOutputStream out = new FileOutputStream("c://hssf-links.xls");
100
101 wb.write(out);
102
103 out.close();
104
105
106
一点废话:link.setAddress("'Target Sheet'!A1");这一句中,Target Sheet是Sheet的名字,A是Excel中的列号,1是Excel中的行号,A1表示第一行第一列。
posted on 2008-11-25 12:50
Vincent-chen 阅读(544)
评论(0) 编辑 收藏 所属分类:
POI 、
Print