POI3的资料整理

一.POI简介

Jakarta POI 是apache的子项目,目标是处理ole2对象。它提供了一组操纵Windows文档的Java API

目前比较成熟的是HSSF接口,处理MS Excel(97-2002)对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。

二.HSSF概况

HSSF 是Horrible SpreadSheet Format的缩写,也即“讨厌的电子表格格式”。 也许HSSF的名字有点滑稽,就本质而言它是一个非常严肃、正规的API。通过HSSF,你可以用纯Java代码来读取、写入、修改Excel文件。

HSSF 为读取操作提供了两类API:usermodel和eventusermodel,即“用户模型”和“事件-用户模型”。前者很好理解,后者比较抽象,但操作效率要高得多。

三.开始编码

1 . 准备工作

要求:JDK 1.4+POI开发包

可以到 http://www.apache.org/dyn/closer.cgi/jakarta/poi/ 最新的POI工具包

2 . EXCEL 结构

HSSFWorkbook excell 文档对象介绍
HSSFSheet excell的表单
HSSFRow excell的行
HSSFCell excell的格子单元
HSSFFont excell字体
HSSFName 名称
HSSFDataFormat 日期格式
HSSFHeader sheet头
HSSFFooter sheet尾
和这个样式
HSSFCellStyle cell样式
辅助操作包括
HSSFDateUtil 日期
HSSFPrintSetup 打印
HSSFErrorConstants 错误信息表

4 . 可参考文档

POI 主页:http://jakarta.apache.org/poi/,

初学者如何快速上手使用POI HSSF

http://jakarta.apache.org/poi/hssf/quick-guide.html 。

代码例子 http://blog.java-cn.com/user1/6749/archives/2005/18347.html


里面有很多例子代码,可以很方便上手。

四.使用心得

POI HSSF 的usermodel包把Excel文件映射成我们熟悉的结构,诸如Workbook、Sheet、Row、Cell等,它把整个结构以一组对象的形式保存在内存之中,便于理解,操作方便,基本上能够满足我们的要求,所以说这个一个不错的选择。

1.创建工作簿 (WORKBOOK)

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
3.wb.write(fileOut);
4.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


2.创建工作表(SHEET)

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet("new sheet");
3.HSSFSheet sheet2 = wb.createSheet("second sheet");
4.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
5.wb.write(fileOut);
6.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
HSSFSheet sheet2 = wb.createSheet("second sheet");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


3.创建单元格(CELL)

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short)0);
5.// Create a cell and put a value in it.
6.HSSFCell cell = row.createCell((short)0);
7.cell.setCellValue(1);
8.// Or do it on one line.
9.row.createCell((short)1).setCellValue(1.2);
10.row.createCell((short)2).setCellValue("This is a string");
11.row.createCell((short)3).setCellValue(true);
12.// Write the output to a file
13.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
14.wb.write(fileOut);
15.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// 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.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).setCellValue(1.2);
row.createCell((short)2).setCellValue("This is a string");
row.createCell((short)3).setCellValue(true);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


4.创建指定单元格式的单元格

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short)0);
5.// Create a cell and put a date value in it. The first cell is not styled
6.// as a date.
7.HSSFCell cell = row.createCell((short)0);
8.cell.setCellValue(new Date());
9.// we style the second cell as a date (and time). It is important to
10.// create a new cell style from the workbook otherwise you can end up
11.// modifying the built in style and effecting not only this cell but other cells.
12.HSSFCellStyle cellStyle = wb.createCellStyle();
13.cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
14.cell = row.createCell((short)1);
15.cell.setCellValue(new Date());
16.cell.setCellStyle(cellStyle);
17.// Write the output to a file
18.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
19.wb.write(fileOut);
20.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// 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 date value in it. The first cell is not styled
// as a date.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(new Date());
// we style the second cell as a date (and time). It is important to
// create a new cell style from the workbook otherwise you can end up
// modifying the built in style and effecting not only this cell but other cells.
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setDataFormat(HSSFDataFormat.getBuiltinFormat("m/d/yy h:mm"));
cell = row.createCell((short)1);
cell.setCellValue(new Date());
cell.setCellStyle(cellStyle);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


5. 单元格的不同格式

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFRow row = sheet.createRow((short)2);
4.row.createCell((short) 0).setCellValue(1.1);
5.row.createCell((short) 1).setCellValue(new Date());
6.row.createCell((short) 2).setCellValue("a string");
7.row.createCell((short) 3).setCellValue(true);
8.row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
9.// Write the output to a file
10.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
11.wb.write(fileOut);
12.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short)2);
row.createCell((short) 0).setCellValue(1.1);
row.createCell((short) 1).setCellValue(new Date());
row.createCell((short) 2).setCellValue("a string");
row.createCell((short) 3).setCellValue(true);
row.createCell((short) 4).setCellType(HSSFCell.CELL_TYPE_ERROR);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


6.单元格的不通对齐方式

Java代码
1.public static void main(String[] args)
2.throws IOException
3.{
4.HSSFWorkbook wb = new HSSFWorkbook();
5.HSSFSheet sheet = wb.createSheet("new sheet");
6.HSSFRow row = sheet.createRow((short) 2);
7.createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
8.createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
9.createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
10.createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
11.createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
12.createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
13.createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
14.// Write the output to a file
15.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
16.wb.write(fileOut);
17.fileOut.close();
18.}
19./**
20.* Creates a cell and aligns it a certain way.
21.*
22.* @param wb the workbook
23.* @param row the row to create the cell in
24.* @param column the column number to create the cell in
25.* @param align the alignment for the cell.
26.*/
27.private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
28.{
29.HSSFCell cell = row.createCell(column);
30.cell.setCellValue("Align It");
31.HSSFCellStyle cellStyle = wb.createCellStyle();
32.cellStyle.setAlignment(align);
33.cell.setCellStyle(cellStyle);
34.}
public static void main(String[] args)
throws IOException
{
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 2);
createCell(wb, row, (short) 0, HSSFCellStyle.ALIGN_CENTER);
createCell(wb, row, (short) 1, HSSFCellStyle.ALIGN_CENTER_SELECTION);
createCell(wb, row, (short) 2, HSSFCellStyle.ALIGN_FILL);
createCell(wb, row, (short) 3, HSSFCellStyle.ALIGN_GENERAL);
createCell(wb, row, (short) 4, HSSFCellStyle.ALIGN_JUSTIFY);
createCell(wb, row, (short) 5, HSSFCellStyle.ALIGN_LEFT);
createCell(wb, row, (short) 6, HSSFCellStyle.ALIGN_RIGHT);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
}
/**
* Creates a cell and aligns it a certain way.
*
* @param wb the workbook
* @param row the row to create the cell in
* @param column the column number to create the cell in
* @param align the alignment for the cell.
*/
private static void createCell(HSSFWorkbook wb, HSSFRow row, short column, short align)
{
HSSFCell cell = row.createCell(column);
cell.setCellValue("Align It");
HSSFCellStyle cellStyle = wb.createCellStyle();
cellStyle.setAlignment(align);
cell.setCellStyle(cellStyle);
}


7.单元格的边框设置

Java代码
1.//Working with borders
2.HSSFWorkbook wb = new HSSFWorkbook();
3.HSSFSheet sheet = wb.createSheet("new sheet");
4.// Create a row and put some cells in it. Rows are 0 based.
5.HSSFRow row = sheet.createRow((short) 1);
6.// Create a cell and put a value in it.
7.HSSFCell cell = row.createCell((short) 1);
8.cell.setCellValue(4);
9.// Style the cell with borders all around.
10.HSSFCellStyle style = wb.createCellStyle();
11.style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
12.style.setBottomBorderColor(HSSFColor.BLACK.index);
13.style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
14.style.setLeftBorderColor(HSSFColor.GREEN.index);
15.style.setBorderRight(HSSFCellStyle.BORDER_THIN);
16.style.setRightBorderColor(HSSFColor.BLUE.index);
17.style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
18.style.setTopBorderColor(HSSFColor.BLACK.index);
19.cell.setCellStyle(style);
20.// Write the output to a file
21.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
22.wb.write(fileOut);
23.fileOut.close();
//Working with borders
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue(4);
// Style the cell with borders all around.
HSSFCellStyle style = wb.createCellStyle();
style.setBorderBottom(HSSFCellStyle.BORDER_THIN);
style.setBottomBorderColor(HSSFColor.BLACK.index);
style.setBorderLeft(HSSFCellStyle.BORDER_THIN);
style.setLeftBorderColor(HSSFColor.GREEN.index);
style.setBorderRight(HSSFCellStyle.BORDER_THIN);
style.setRightBorderColor(HSSFColor.BLUE.index);
style.setBorderTop(HSSFCellStyle.BORDER_MEDIUM_DASHED);
style.setTopBorderColor(HSSFColor.BLACK.index);
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


8.填充和颜色设置

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short) 1);
5.// Aqua background
6.HSSFCellStyle style = wb.createCellStyle();
7.style.setFillBackgroundColor(HSSFColor.AQUA.index);
8.style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
9.HSSFCell cell = row.createCell((short) 1);
10.cell.setCellValue("X");
11.cell.setCellStyle(style);
12.// Orange "foreground", foreground being the fill foreground not the font color.
13.style = wb.createCellStyle();
14.style.setFillForegroundColor(HSSFColor.ORANGE.index);
15.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
16.cell = row.createCell((short) 2);
17.cell.setCellValue("X");
18.cell.setCellStyle(style);
19.// Write the output to a file
20.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
21.wb.write(fileOut);
22.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Aqua background
HSSFCellStyle style = wb.createCellStyle();
style.setFillBackgroundColor(HSSFColor.AQUA.index);
style.setFillPattern(HSSFCellStyle.BIG_SPOTS);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("X");
cell.setCellStyle(style);
// Orange "foreground", foreground being the fill foreground not the font color.
style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.ORANGE.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
cell = row.createCell((short) 2);
cell.setCellValue("X");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


9.合并单元格操作

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFRow row = sheet.createRow((short) 1);
4.HSSFCell cell = row.createCell((short) 1);
5.cell.setCellValue("This is a test of merging");
6.sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
7.// Write the output to a file
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFRow row = sheet.createRow((short) 1);
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of merging");
sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


9.1合并单元格边框的解决办法

Java代码
1.private void setRegionStyle(HSSFSheet sheet, Region region , HSSFCellStyle cs) {
2. int toprowNum = region.getRowFrom();
3. for (int i = region.getRowFrom(); i <= region.getRowTo(); i ++) {
4. HSSFRow row = HSSFCellUtil.getRow(i, sheet);
5. for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
6. HSSFCell cell = HSSFCellUtil.getCell(row, (short)j);
7. cell.setCellStyle(cs);
8. }
9. }
10.}
private void setRegionStyle(HSSFSheet sheet, Region region , HSSFCellStyle cs) {
int toprowNum = region.getRowFrom();
for (int i = region.getRowFrom(); i <= region.getRowTo(); i ++) {
HSSFRow row = HSSFCellUtil.getRow(i, sheet);
for (int j = region.getColumnFrom(); j <= region.getColumnTo(); j++) {
HSSFCell cell = HSSFCellUtil.getCell(row, (short)j);
cell.setCellStyle(cs);
}
}
}


10.字体设置

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.// Create a row and put some cells in it. Rows are 0 based.
4.HSSFRow row = sheet.createRow((short) 1);
5.// Create a new font and alter it.
6.HSSFFont font = wb.createFont();
7.font.setFontHeightInPoints((short)24);
8.font.setFontName("Courier New");
9.font.setItalic(true);
10.font.setStrikeout(true);
11.// Fonts are set into a style so create a new one to use.
12.HSSFCellStyle style = wb.createCellStyle();
13.style.setFont(font);
14.// Create a cell and put a value in it.
15.HSSFCell cell = row.createCell((short) 1);
16.cell.setCellValue("This is a test of fonts");
17.cell.setCellStyle(style);
18.// Write the output to a file
19.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
20.wb.write(fileOut);
21.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short) 1);
// Create a new font and alter it.
HSSFFont font = wb.createFont();
font.setFontHeightInPoints((short)24);
font.setFontName("Courier New");
font.setItalic(true);
font.setStrikeout(true);
// Fonts are set into a style so create a new one to use.
HSSFCellStyle style = wb.createCellStyle();
style.setFont(font);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short) 1);
cell.setCellValue("This is a test of fonts");
cell.setCellStyle(style);
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


11.自定义颜色

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet();
3.HSSFRow row = sheet.createRow((short) 0);
4.HSSFCell cell = row.createCell((short) 0);
5.cell.setCellValue("Default Palette");
6.//apply some colors from the standard palette,
7.// as in the previous examples.
8.//we'll use red text on a lime background
9.HSSFCellStyle style = wb.createCellStyle();
10.style.setFillForegroundColor(HSSFColor.LIME.index);
11.style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
12.HSSFFont font = wb.createFont();
13.font.setColor(HSSFColor.RED.index);
14.style.setFont(font);
15.cell.setCellStyle(style);
16.//save with the default palette
17.FileOutputStream out = new FileOutputStream("default_palette.xls");
18.wb.write(out);
19.out.close();
20.//now, let's replace RED and LIME in the palette
21.// with a more attractive combination
22.// (lovingly borrowed from freebsd.org)
23.cell.setCellValue("Modified Palette");
24.//creating a custom palette for the workbook
25.HSSFPalette palette = wb.getCustomPalette();
26.//replacing the standard red with freebsd.org red
27.palette.setColorAtIndex(HSSFColor.RED.index,
28.(byte) 153, //RGB red (0-255)
29.(byte) 0, //RGB green
30.(byte) 0 //RGB blue
31.);
32.//replacing lime with freebsd.org gold
33.palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
34.//save with the modified palette
35.// note that wherever we have previously used RED or LIME, the
36.// new colors magically appear
37.out = new FileOutputStream("modified_palette.xls");
38.wb.write(out);
39.out.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet();
HSSFRow row = sheet.createRow((short) 0);
HSSFCell cell = row.createCell((short) 0);
cell.setCellValue("Default Palette");
//apply some colors from the standard palette,
// as in the previous examples.
//we'll use red text on a lime background
HSSFCellStyle style = wb.createCellStyle();
style.setFillForegroundColor(HSSFColor.LIME.index);
style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
HSSFFont font = wb.createFont();
font.setColor(HSSFColor.RED.index);
style.setFont(font);
cell.setCellStyle(style);
//save with the default palette
FileOutputStream out = new FileOutputStream("default_palette.xls");
wb.write(out);
out.close();
//now, let's replace RED and LIME in the palette
// with a more attractive combination
// (lovingly borrowed from freebsd.org)
cell.setCellValue("Modified Palette");
//creating a custom palette for the workbook
HSSFPalette palette = wb.getCustomPalette();
//replacing the standard red with freebsd.org red
palette.setColorAtIndex(HSSFColor.RED.index,
(byte) 153, //RGB red (0-255)
(byte) 0, //RGB green
(byte) 0 //RGB blue
);
//replacing lime with freebsd.org gold
palette.setColorAtIndex(HSSFColor.LIME.index, (byte) 255, (byte) 204, (byte) 102);
//save with the modified palette
// note that wherever we have previously used RED or LIME, the
// new colors magically appear
out = new FileOutputStream("modified_palette.xls");
wb.write(out);
out.close();


12.读和重写EXCEL文件

Java代码
1.POIFSFileSystem fs =
2.new POIFSFileSystem(new FileInputStream("workbook.xls"));
3.HSSFWorkbook wb = new HSSFWorkbook(fs);
4.HSSFSheet sheet = wb.getSheetAt(0);
5.HSSFRow row = sheet.getRow(2);
6.HSSFCell cell = row.getCell((short)3);
7.if (cell == null)
8.cell = row.createCell((short)3);
9.cell.setCellType(HSSFCell.CELL_TYPE_STRING);
10.cell.setCellValue("a test");
11.// Write the output to a file
12.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
13.wb.write(fileOut);
14.fileOut.close();
POIFSFileSystem fs =
new POIFSFileSystem(new FileInputStream("workbook.xls"));
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFRow row = sheet.getRow(2);
HSSFCell cell = row.getCell((short)3);
if (cell == null)
cell = row.createCell((short)3);
cell.setCellType(HSSFCell.CELL_TYPE_STRING);
cell.setCellValue("a test");
// Write the output to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


13.在EXCEL单元格中使用自动换行

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet s = wb.createSheet();
3.HSSFRow r = null;
4.HSSFCell c = null;
5.HSSFCellStyle cs = wb.createCellStyle();
6.HSSFFont f = wb.createFont();
7.HSSFFont f2 = wb.createFont();
8.cs = wb.createCellStyle();
9.cs.setFont( f2 );
10.//Word Wrap MUST be turned on
11.cs.setWrapText( true );
12.r = s.createRow( (short) 2 );
13.r.setHeight( (short) 0x349 );
14.c = r.createCell( (short) 2 );
15.c.setCellType( HSSFCell.CELL_TYPE_STRING );
16.c.setCellValue( "Use \n with word wrap on to create a new line" );
17.c.setCellStyle( cs );
18.s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
19.FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
20.wb.write( fileOut );
21.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet s = wb.createSheet();
HSSFRow r = null;
HSSFCell c = null;
HSSFCellStyle cs = wb.createCellStyle();
HSSFFont f = wb.createFont();
HSSFFont f2 = wb.createFont();
cs = wb.createCellStyle();
cs.setFont( f2 );
//Word Wrap MUST be turned on
cs.setWrapText( true );
r = s.createRow( (short) 2 );
r.setHeight( (short) 0x349 );
c = r.createCell( (short) 2 );
c.setCellType( HSSFCell.CELL_TYPE_STRING );
c.setCellValue( "Use \n with word wrap on to create a new line" );
c.setCellStyle( cs );
s.setColumnWidth( (short) 2, (short) ( ( 50 * 8 ) / ( (double) 1 / 20 ) ) );
FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
wb.write( fileOut );
fileOut.close();


14.数字格式自定义

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFCellStyle style;
4.HSSFDataFormat format = wb.createDataFormat();
5.HSSFRow row;
6.HSSFCell cell;
7.short rowNum = 0;
8.short colNum = 0;
9.row = sheet.createRow(rowNum++);
10.cell = row.createCell(colNum);
11.cell.setCellValue(11111.25);
12.style = wb.createCellStyle();
13.style.setDataFormat(format.getFormat("0.0"));
14.cell.setCellStyle(style);
15.row = sheet.createRow(rowNum++);
16.cell = row.createCell(colNum);
17.cell.setCellValue(11111.25);
18.style = wb.createCellStyle();
19.style.setDataFormat(format.getFormat("#,##0.0000"));
20.cell.setCellStyle(style);
21.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
22.wb.write(fileOut);
23.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFCellStyle style;
HSSFDataFormat format = wb.createDataFormat();
HSSFRow row;
HSSFCell cell;
short rowNum = 0;
short colNum = 0;
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("0.0"));
cell.setCellStyle(style);
row = sheet.createRow(rowNum++);
cell = row.createCell(colNum);
cell.setCellValue(11111.25);
style = wb.createCellStyle();
style.setDataFormat(format.getFormat("#,##0.0000"));
cell.setCellStyle(style);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


15.调整工作单位置

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFPrintSetup ps = sheet.getPrintSetup();
4.sheet.setAutobreaks(true);
5.ps.setFitHeight((short)1);
6.ps.setFitWidth((short)1); // Create various cells and rows for spreadsheet.
7.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
8.wb.write(fileOut);
9.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFPrintSetup ps = sheet.getPrintSetup();
sheet.setAutobreaks(true);
ps.setFitHeight((short)1);
ps.setFitWidth((short)1); // Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


16.设置打印区域

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("Sheet1");
3.wb.setPrintArea(0, "$A$1:$C$2");
4.//sets the print area for the first sheet
5.//Alternatively:
6.//wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
7.// Create various cells and rows for spreadsheet.
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("Sheet1");
wb.setPrintArea(0, "$A$1:$C$2");
//sets the print area for the first sheet
//Alternatively:
//wb.setPrintArea(0, 0, 1, 0, 0) is equivalent to using the name reference (See the JavaDocs for more details)
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


17.标注脚注

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("format sheet");
3.HSSFFooter footer = sheet.getFooter()
4.footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );
5.
6.// Create various cells and rows for spreadsheet.
7.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
8.wb.write(fileOut);
9.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("format sheet");
HSSFFooter footer = sheet.getFooter()
footer.setRight( "Page " + HSSFFooter.page() + " of " + HSSFFooter.numPages() );

// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


18.使用方便的内部提供的函数

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet( "new sheet" );
3.// Create a merged region
4.HSSFRow row = sheet1.createRow( (short) 1 );
5.HSSFRow row2 = sheet1.createRow( (short) 2 );
6.HSSFCell cell = row.createCell( (short) 1 );
7.cell.setCellValue( "This is a test of merging" );
8.Region region = new Region( 1, (short) 1, 4, (short) 4 );
9.sheet1.addMergedRegion( region );
10.// Set the border and border colors.
11.final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
12.HSSFRegionUtil.setBorderBottom( borderMediumDashed,
13.region, sheet1, wb );
14.HSSFRegionUtil.setBorderTop( borderMediumDashed,
15.region, sheet1, wb );
16.HSSFRegionUtil.setBorderLeft( borderMediumDashed,
17.region, sheet1, wb );
18.HSSFRegionUtil.setBorderRight( borderMediumDashed,
19.region, sheet1, wb );
20.HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
21.HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
22.HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
23.HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
24.// Shows some usages of HSSFCellUtil
25.HSSFCellStyle style = wb.createCellStyle();
26.style.setIndention((short)4);
27.HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
28.HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
29.HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
30.// Write out the workbook
31.FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
32.wb.write( fileOut );
33.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet( "new sheet" );
// Create a merged region
HSSFRow row = sheet1.createRow( (short) 1 );
HSSFRow row2 = sheet1.createRow( (short) 2 );
HSSFCell cell = row.createCell( (short) 1 );
cell.setCellValue( "This is a test of merging" );
Region region = new Region( 1, (short) 1, 4, (short) 4 );
sheet1.addMergedRegion( region );
// Set the border and border colors.
final short borderMediumDashed = HSSFCellStyle.BORDER_MEDIUM_DASHED;
HSSFRegionUtil.setBorderBottom( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderTop( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderLeft( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBorderRight( borderMediumDashed,
region, sheet1, wb );
HSSFRegionUtil.setBottomBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setTopBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setLeftBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
HSSFRegionUtil.setRightBorderColor(HSSFColor.AQUA.index, region, sheet1, wb);
// Shows some usages of HSSFCellUtil
HSSFCellStyle style = wb.createCellStyle();
style.setIndention((short)4);
HSSFCellUtil.createCell(row, 8, "This is the value of the cell", style);
HSSFCell cell2 = HSSFCellUtil.createCell( row2, 8, "This is the value of the cell");
HSSFCellUtil.setAlignment(cell2, wb, HSSFCellStyle.ALIGN_CENTER);
// Write out the workbook
FileOutputStream fileOut = new FileOutputStream( "workbook.xls" );
wb.write( fileOut );
fileOut.close();


19.在工作单中移动行,调整行的上下位置


Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("row sheet");
3.// Create various cells and rows for spreadsheet.
4.// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
5.sheet.shiftRows(5, 10, -5);
6.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
7.wb.write(fileOut);
8.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("row sheet");
// Create various cells and rows for spreadsheet.
// Shift rows 6 - 11 on the spreadsheet to the top (rows 0 - 5)
sheet.shiftRows(5, 10, -5);
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


20.选种指定的工作单

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("row sheet");
3.sheet.setSelected(true);
4.// Create various cells and rows for spreadsheet.
5.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
6.wb.write(fileOut);
7.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("row sheet");
sheet.setSelected(true);
// Create various cells and rows for spreadsheet.
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


21.工作单的放大缩小

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet1 = wb.createSheet("new sheet");
3.sheet1.setZoom(3,4); // 75 percent magnification
4.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
5.wb.write(fileOut);
6.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet1 = wb.createSheet("new sheet");
sheet1.setZoom(3,4); // 75 percent magnification
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


22.头注和脚注

Java代码
1.HSSFWorkbook wb = new HSSFWorkbook();
2.HSSFSheet sheet = wb.createSheet("new sheet");
3.HSSFHeader header = sheet.getHeader();
4.header.setCenter("Center Header");
5.header.setLeft("Left Header");
6.header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
7.HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
8.FileOutputStream fileOut = new FileOutputStream("workbook.xls");
9.wb.write(fileOut);
10.fileOut.close();
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFHeader header = sheet.getHeader();
header.setCenter("Center Header");
header.setLeft("Left Header");
header.setRight(HSSFHeader.font("Stencil-Normal", "Italic") +
HSSFHeader.fontSize((short) 16) + "Right w/ Stencil-Normal Italic font and size 16");
FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();


23.图片的使用

Java代码
1.//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
2.ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
3.BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
4.ImageIO.write(bufferImg,"jpg",byteArrayOut);
5.
6.//读进一个excel模版
7.FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
8.fs = new POIFSFileSystem(fos);
9.//创建一个工作薄
10.HSSFWorkbook wb = new HSSFWorkbook(fs);
11.HSSFSheet sheet = wb.getSheetAt(0);
12.HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
13.HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
14.patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));
//先把读进来的图片放到一个ByteArrayOutputStream中,以便产生ByteArray
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
BufferedImage bufferImg = ImageIO.read(new File("ok.jpg"));
ImageIO.write(bufferImg,"jpg",byteArrayOut);

//读进一个excel模版
FileInputStream fos = new FileInputStream(filePathName+"/stencil.xlt");
fs = new POIFSFileSystem(fos);
//创建一个工作薄
HSSFWorkbook wb = new HSSFWorkbook(fs);
HSSFSheet sheet = wb.getSheetAt(0);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
HSSFClientAnchor anchor = new HSSFClientAnchor(0,0,1023,255,(short) 0,0,(short)10,10);
patriarch.createPicture(anchor , wb.addPicture(byteArrayOut.toByteArray(),HSSFWorkbook.PICTURE_TYPE_JPEG));


//-------------------------------以上实例代码均来自官方网站
//-------------------------------POI中使用的颜色是用颜色索引来实现,如下:
/*
* 颜色对照表 数字代表颜色索引
8: BLACK
60: BROWN
59: OLIVE_GREEN
58: DARK_GREEN
56: DARK_TEAL
18: DARK_BLUE
32: DARK_BLUE
62: INDIGO
63: GREY_80_PERCENT
53: ORANGE
19: DARK_YELLOW
17: GREEN
21: TEAL
38: TEAL
12: BLUE
39: BLUE
54: BLUE_GREY
23: GREY_50_PERCENT
10: RED
52: LIGHT_ORANGE
50: LIME
57: SEA_GREEN
49: AQUA
48: LIGHT_BLUE
20: VIOLET
36: VIOLET
55: GREY_40_PERCENT
14: PINK
33: PINK
51: GOLD
13: YELLOW
34: YELLOW
11: BRIGHT_GREEN
35: BRIGHT_GREEN
15: TURQUOISE
35: TURQUOISE
16: DARK_RED
37: DARK_RED
40: SKY_BLUE
61: PLUM
25: PLUM
22: GREY_25_PERCENT
45: ROSE
43: LIGHT_YELLOW
42: LIGHT_GREEN
41: LIGHT_TURQUOISE
27:LIGHT_TURQUOISE
44: PALE_BLUE
46: LAVENDER
9: WHITE
24: CORNFLOWER_BLUE
26: LEMON_CHIFFON
25: MAROON
28: ORCHID
29: CORAL
30: ROYAL_BLUE
31: LIGHT_CORNFLOWER_BLUE
*/
//----------------------------------------------------你可以按上面的方法来自定义颜色
/*
* 自定义颜色,去掉注释,贴加,其他则查看颜色对照表
HSSFPalette palette = this.getCustomPalette();
palette.setColorAtIndex(idx,
i, //RGB red (0-255)
j, //RGB green
k //RGB blue
);
*/
资源来自:http://tomgreenintel.iteye.com/blog/204106
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Google POI爬取指的是使用网络爬虫程序对Google上的POI(点 of interest)进行自动化爬取的过程。POI是指地图上的一些地标、商家、景点、公共设施等,通常包括名称、地址、电话、经度、纬度等信息。这些信息对于地图应用、智能导航等方面非常重要,因此许多公司都会尝试通过爬取POI信息来进行商业化开发。 Google是目前全球最大的的互联网公司之一,其网站上包含了大量的POI信息。但是,Google的数据采集和存储都有一定的限制,因此对Google POI进行爬取时要注意相关的法律法规,不得侵犯用户隐私或导致其他安全问题。此外,Google对于频繁访问其网站数据也有一定的反爬机制,需要注意规避封禁或限制。 Google POI爬取的难点主要在于数据量庞大、结构不统一、数据质量不一等方面。因此,需要使用一定的技术手段,如机器学习、自然语言处理、数据清洗等,来优化数据提取和处理的方式。 总的来说,Google POI爬取可以帮助我们获得大量的有用信息,为地图服务和其他商业化应用提供支持。但是,在进行爬取时要注意合法合规,充分保护用户隐私和安全。 ### 回答2: 谷歌POI爬取是指通过专业的软件工具和技术手段从谷歌地图上爬取特定位置的信息。POI代表着“点 of interest”的缩写,它是指在地图上标记的有意义的地点信息。例如,商店、餐馆、公园、学校、医院等等。通过谷歌POI爬取技术,我们可以获取到很多有用的信息,包括位置、名称、地址、电话、评级等等。 在实际应用中,谷歌POI爬取主要应用于商业和科研领域。商业上,谷歌POI爬取可以帮助企业进行市场调研、商业分析、推销营销等业务。科研方面,谷歌POI爬取可以帮助研究人员分析城市的人口、流动、交通等行为特征,以及了解城市发展趋势、研究城市规划等。 谷歌POI爬取技术的具体步骤包括:选择目标区域、确定爬取内容、开发程序进行爬取、整理和分析数据。爬取程序可以通过谷歌地图API和网络爬虫等技术实现。同时,需要注意的是,在使用谷歌POI爬取技术时应遵守相关法律法规,避免侵犯他人的隐私或其他权益。 ### 回答3: 谷歌POI爬取是指通过网络爬虫对谷歌地图上的兴趣点(POI)进行数据采集的过程。在现代数字化时代,随着电子地图的普及和谷歌地图等应用的发展,有越来越多的企业和个人需要从谷歌地图中获取特定的POI数据,如商家信息、地址、联系方式等。通过谷歌POI爬取的方法,可以实现快速、准确、自动化地采集海量的POI数据,显著提高数据获取效率和数据的准确性。 谷歌POI爬取的过程需要借助网络爬虫技术,利用代码自动化爬取谷歌地图上的POI数据。爬取程序会访问特定的URL,通过检索关键词等方式获取POI数据,并进行数据清洗和格式转换,输出最终的数据结果。谷歌POI爬取的实现过程中要考虑到反爬虫策略和数据保密的问题,需要遵守相关的法律法规和道德规范,避免侵犯他人隐私和商业利益,避免被谷歌地图封禁或追究法律责任。 总之,谷歌POI爬取是针对谷歌地图上POI数据采集的一种有效方法,能够帮助企业和个人快捷地获取所需的POI数据,具有极高的实用性和经济效益。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值