- 浏览: 44521 次
文章分类
最新评论
-
sghcel:
根本就不是讲原理的
FreeMarker小结 -
laobian:
仔细看了,觉得有道理。
程序员的出路,其实并不难寻找,低中高各等级且听我来分析 -
kobicc:
很值得去好好地品味迷茫时的两条意见
程序员的出路,其实并不难寻找,低中高各等级且听我来分析 -
jerryhanwei:
买房就得跳啦,人民币才是硬道理。不过,老跳槽,对自己也很不好, ...
程序员的出路,其实并不难寻找,低中高各等级且听我来分析 -
linanlolly:
我写的导出EXCEL怎么老是报java.lang.OutOfM ...
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)
2.创建工作表(SHEET)
3.创建单元格(CELL)
4.创建指定单元格式的单元格
5. 单元格的不同格式
6.单元格的不通对齐方式
7.单元格的边框设置
8.填充和颜色设置
9.合并单元格操作
9.1合并单元格边框的解决办法
10.字体设置
11.自定义颜色
12.读和重写EXCEL文件
13.在EXCEL单元格中使用自动换行
14.数字格式自定义
15.调整工作单位置
16.设置打印区域
17.标注脚注
18.使用方便的内部提供的函数
19.在工作单中移动行,调整行的上下位置
20.选种指定的工作单
21.工作单的放大缩小
22.头注和脚注
23.图片的使用
//-------------------------------以上实例代码均来自官方网站
//-------------------------------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
);
*/
//-------------------------------------------------
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)
HSSFWorkbook wb = new HSSFWorkbook(); FileOutputStream fileOut = new FileOutputStream("workbook.xls"); wb.write(fileOut); fileOut.close();
2.创建工作表(SHEET)
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)
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
4.创建指定单元格式的单元格
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. 单元格的不同格式
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.单元格的不通对齐方式
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.单元格的边框设置
//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.填充和颜色设置
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.合并单元格操作
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合并单元格边框的解决办法
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.字体设置
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.自定义颜色
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文件
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单元格中使用自动换行
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.数字格式自定义
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.调整工作单位置
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.设置打印区域
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.标注脚注
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.使用方便的内部提供的函数
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.在工作单中移动行,调整行的上下位置
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.选种指定的工作单
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.工作单的放大缩小
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.头注和脚注
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.图片的使用
//先把读进来的图片放到一个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
);
*/
//-------------------------------------------------
- POI.rar (95.1 KB)
- 描述: 附件主要关于对word文挡的解析.有需要的可以下载看看
- 下载次数: 63
评论
2 楼
linanlolly
2009-10-11
我写的导出EXCEL怎么老是报java.lang.OutOfMemoryError: Java heap space错误呢?能不能在不动JVM的情况下修正这个错误呢?
1 楼
mida
2008-11-14
好详细,只是我在创建excel时,小于1的小数只显示小数点和小数,前面的0不显示,痛苦
相关推荐
新增2024全国POI矢量数据 数据时间2024年5月 shp格式 36万个数据 数据来源OpenStreetMap 一、全国范围2012、2014、2016、2018、2020、2022年和2023年常用POI数据集 数据是全国范围2012、2014、2016、2018、2020、...
3. **POIFSFileSystem** `POIFSFileSystem` 是 POI 中用于处理 Office 文件系统的类,主要用于读写 .xls 文件。它是一个低级别的 API,用于管理和存储文件系统的目录和文件。当从输入流加载或保存到输出流时,`...
在Java编程环境中,Apache POI 是一个非常流行的库,它允许开发者读取、写入以及操作Microsoft Office格式的文件,尤其是Excel(.xls 和 .xlsx)文档。在本篇文章中,我们将深入探讨如何使用POI库中的HSSFWorkbook类...
3. **读取Excel文件**: 使用POI,你可以通过WorkbookFactory.create()方法打开Excel文件。然后,你可以遍历工作簿中的每个工作表,进一步遍历每个工作表中的行和单元格,获取或处理数据。 4. **写入Excel文件**: ...
在描述中提到的“全国大多数国家POI数据”,意味着这个资源包含全球多个国家的POI信息,可能是经过整理和归类的。用户可以下载这些数据进行各种分析和应用,例如研究全球商业分布、规划旅行路线或者进行市场潜力评估...
3. 在进行栅格化处理时,要根据实际需求选择合适的栅格分辨率,过高的分辨率可能导致内存占用过大,过低则可能损失细节信息。 4. 在进行以表格显示分区统计时,要理解各个统计选项的含义,以免得出错误的结果。 ...
数据整理全国各地级市POI兴趣点数据,是高德地图的一级分类和二级分类,主要分类包含饮美食、购物消费、商务住宅、休闲娱乐、医疗保健、运动健身、科教文化、旅游景点、酒店住宿、生活服务、公司企业、交通设施、...
3. 安顺POI20210804.zip:安顺市的POI数据,可能包含黄果树瀑布等著名景点的数据。 4. 黔东南POI20210804.zip:黔东南苗族侗族自治州的POI数据,体现了丰富的民族文化特色。 5. 黔西南POI20210804.zip:黔西南布依族...
6. **POI整理.doc**:这可能是用户或社区成员整理的POI知识点总结,包含了常见用法、最佳实践和可能的陷阱,对开发者来说是一份宝贵的参考资料。 Apache POI的主要知识点包括: - **Excel处理**:使用HSSF和XSSF...
例如,要从Excel导入数据,只需调用工具类的相应方法,传入文件路径、行和列信息,就可以得到整理好的数据结构。对于Java对象的导入,通常需要提供一个对应的Java类,该类的属性与Excel中的列对应,通过反射技术实现...
自己整理的poi导出Excel
常用的JAVA,Android包库,闲的整理一下打包上传,math3主要用于线性计算,回归分析等,jama主要用于矩阵计算,Poi用于输出大量数据到Excel,再导入matlab。commonIO也是用于输入输出流
3. **数据清洗与预处理**:去除重复项,整理数据格式,为后续分析做好准备。 4. **结果导出**:支持将爬取结果导出为Excel、CSV或其他格式,方便进一步分析或导入其他系统。 使用此类插件时,需要注意以下几点: -...
Apache POI 是一个开源项目,专门用于处理Microsoft Office格式的文件,如Excel、Word和PowerPoint。在Java环境中,使用Apache POI库可以方便地读取、写入和操作Excel电子表格。本压缩包文件“poi-bin-3.17-20170915...
标题中的“北京市POI数据_超级全_已分类.zip”指的是一个包含北京市各种兴趣点(Points of Interest,简称POI)的压缩文件,这些数据经过精心整理并进行了分类。POI数据是地理信息系统(GIS)中常用的一种信息,用于...
今天用POI解析了excel,现在整理了一下,附件中包含POI操作2007office需要用到的所有jar包 dom4j-1.6.1.jar geronimo-stax-api_1.0_spec-1.0.jar ooxml-schemas-1.0.jar openxml4j-bin-beta.jar poi-3.0-FINAL.jar ...