昨天学生问我java如何操作Excel,于是google了一下,发现java操作Excel的第三方jar包比较多,什么JXL,POI等等,比较多,听付老师说JXL操作比较简单,但是jxl已经不升级了,我不太喜欢,于是就去看比较流行的POI
Apache的Jakata项目的POI子项目,目标是处理ole2对象。目前比较成熟的是HSSF接口,处理MS Excel(97-2002)对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。
准备工作就是到官方下载到POI:
下载之后还有详细的帮助文档,试了一下,比较方便,当然官方也有在线文档:
主要内容有:
How to create a new workbook
How to create a sheet
How to create cells
How to create date cells
Working with different types of cells
Aligning cells
Working with borders
Fills and color
Merging cells
Working with fonts
Custom colors
Reading and writing
Use newlines in cells.
Create user defined data formats
Fit Sheet to One Page
Set print area for a sheet
Set page numbers on the footer of a sheet
Shift rows
Set a sheet as selected
Set the zoom magnification for a sheet
Create split and freeze panes
Repeating rows and columns
Headers and Footers
Drawing Shapes
Styling Shapes
Shapes and Graphics2d
Outlining
Images
Named Ranges and Named Cells
How to set cell comments
How to adjust column width to fit the contents
How to create a sheet
How to create cells
How to create date cells
Working with different types of cells
Aligning cells
Working with borders
Fills and color
Merging cells
Working with fonts
Custom colors
Reading and writing
Use newlines in cells.
Create user defined data formats
Fit Sheet to One Page
Set print area for a sheet
Set page numbers on the footer of a sheet
Shift rows
Set a sheet as selected
Set the zoom magnification for a sheet
Create split and freeze panes
Repeating rows and columns
Headers and Footers
Drawing Shapes
Styling Shapes
Shapes and Graphics2d
Outlining
Images
Named Ranges and Named Cells
How to set cell comments
How to adjust column width to fit the contents
这些足够咱们使用了,如果非要测一下我们可以先看一小段代码,比较简单:
HSSFWorkbook wb = new HSSFWorkbook();//创建一个供作簿
HSSFSheet sheet1 = wb.createSheet("new sheet");//创建一个工作表
HSSFSheet sheet2 = wb.createSheet("second sheet");
//Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet1.createRow((short)0);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1234);
FileOutputStream fileOut = new FileOutputStream("c:/workbook.xls");
wb.write(fileOut);
fileOut.close();
呵呵!