POI技术(一)

项目中经常要解析和生成Excel文件,最常用的开源组件有poi与jxl。jxl是韩国人开发的,发行较早,但是更新的很慢,目前似乎还不支持excel2007。poi是apache下的一个子项目,poi应该是处理ms的office系列文档最好的组件了。poi3.6版本已经开始支持excel2007了。但是由于excel2007底层的实现似乎变成xml与excel2003底层存储发生了本质的变化,因此poi解析excel的类就存在差异了。

      现在简单的介绍下poi常用的接口。

      经常用的类一般都在org.apache.poi.hssf.usermodel(excel2003)或org.apache.poi.xssf.usermodel 
(excel2007)。

  • 工作薄:  WorkBook是操作Excel的入口,HSSFWorkbook, XSSFWorkbook实现了该接口。
  • 页:Sheet表示工作薄的分页。HSSFSheet, XSSFChartSheet, XSSFDialogsheet, XSSFSheet实现了该接口。
  • Row:表示页中的一行。HSSFRow, XSSFRow实现了该接口。
  • Cell:行中的一个单元格。HSSFCell, XSSFCell实现了该接口。

从上面的介绍得知:页是通过工作薄对象创建的,行是通过页对象创建的,单元格是通过行对象创建的。接下来,我们就开始发掘poi的强大功能吧。

  1. 创建一个空白的工作薄
Java代码  复制代码
  1. import java.io.FileOutputStream;   
  2. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  3. import org.apache.poi.ss.usermodel.Workbook;   
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;   
  5.   
  6. //2003版本   
  7. Workbook wb = new HSSFWorkbook();   
  8. FileOutputStream fileOut = new FileOutputStream("workbook.xls");   
  9. wb.write(fileOut);   
  10.  fileOut.close();   
  11.   
  12. //2007版本   
  13. // Workbook wb = new XSSFWorkbook();   
  14. // FileOutputStream fileOut = new FileOutputStrea("workbook.xlsx");   
  15. // wb.write(fileOut);   
  16. // fileOut.close();  
Java代码   收藏代码
  1. <span style="font-size: medium;">import java.io.FileOutputStream;  
  2. import org.apache.poi.hssf.usermodel.HSSFWorkbook;  
  3. import org.apache.poi.ss.usermodel.Workbook;  
  4. import org.apache.poi.xssf.usermodel.XSSFWorkbook;  
  5.   
  6. //2003版本  
  7. Workbook wb = new HSSFWorkbook();  
  8. FileOutputStream fileOut = new FileOutputStream("workbook.xls");  
  9. wb.write(fileOut);  
  10.  fileOut.close();  
  11.   
  12. //2007版本  
  13. // Workbook wb = new XSSFWorkbook();  
  14. // FileOutputStream fileOut = new FileOutputStrea("workbook.xlsx");  
  15. // wb.write(fileOut);  
  16. // fileOut.close();</span>  

 注意:Workbook 是org.apache.poi.ss.usermodel包下的一个接口,注意与以前版本的不同,HSSFWorkbook和XSSFWorkbook实现了该接口,这样就达到了面前接口编程。下面的excel工具类中要用到此点知识。

 2. 创建两个空白页

 

 

Java代码  复制代码
  1. Workbook wb = new HSSFWorkbook();   
  2. //Workbook wb = new XSSFWorkbook();   
  3. Sheet sheet1 = wb.createSheet("new sheet");   
  4. Sheet sheet2 = wb.createSheet("second sheet");   
  5. FileOutputStream fileOut = new FileOutputStream("workbook.xls");   
  6. wb.write(fileOut);   
  7. fileOut.close();  
Java代码   收藏代码
  1. <span style="font-size: medium;">Workbook wb = new HSSFWorkbook();  
  2. //Workbook wb = new XSSFWorkbook();  
  3. Sheet sheet1 = wb.createSheet("new sheet");  
  4. Sheet sheet2 = wb.createSheet("second sheet");  
  5. FileOutputStream fileOut = new FileOutputStream("workbook.xls");  
  6. wb.write(fileOut);  
  7. fileOut.close();  
  8. </span>  

 

3. 创建单元格

 

Java代码  复制代码
  1. public void createRow() throws Exception {   
  2.         Workbook wb = new HSSFWorkbook();   
  3.         // Workbook wb = new XSSFWorkbook();   
  4.         CreationHelper createHelper = wb.getCreationHelper();   
  5.         Sheet sheet = wb.createSheet("new sheet");   
  6.   
  7.         //创建一行并放一些单元格到该行中,行的索引是以0开始的   
  8.         Row row = sheet.createRow((short0);   
  9.         // 创建一个单元格并填充一个整数的值   
  10.         Cell cell = row.createCell(0);   
  11.         cell.setCellValue(1);   
  12.   
  13.         //链式写法   
  14.         row.createCell(1).setCellValue(1.2);   
  15.         row.createCell(2).setCellValue(   
  16.                 createHelper.createRichTextString("This is a string"));   
  17.         row.createCell(3).setCellValue(true);   
  18.   
  19.         //输出文件   
  20.         FileOutputStream fileOut = new FileOutputStream("workbook.xls");   
  21.         wb.write(fileOut);   
  22.         fileOut.close();   
  23.   
  24.     }  
Java代码   收藏代码
  1. <span style="font-size: medium;">public void createRow() throws Exception {  
  2.         Workbook wb = new HSSFWorkbook();  
  3.         // Workbook wb = new XSSFWorkbook();  
  4.         CreationHelper createHelper = wb.getCreationHelper();  
  5.         Sheet sheet = wb.createSheet("new sheet");  
  6.   
  7.         //创建一行并放一些单元格到该行中,行的索引是以0开始的  
  8.         Row row = sheet.createRow((short0);  
  9.         // 创建一个单元格并填充一个整数的值  
  10.         Cell cell = row.createCell(0);  
  11.         cell.setCellValue(1);  
  12.   
  13.         //链式写法  
  14.         row.createCell(1).setCellValue(1.2);  
  15.         row.createCell(2).setCellValue(  
  16.                 createHelper.createRichTextString("This is a string"));  
  17.         row.createCell(3).setCellValue(true);  
  18.   
  19.         //输出文件  
  20.         FileOutputStream fileOut = new FileOutputStream("workbook.xls");  
  21.         wb.write(fileOut);  
  22.         fileOut.close();  
  23.   
  24.     }</span>  

 

 4. 创建日期单元格

 

Java代码  复制代码
  1. public void createDateCell() throws Exception {   
  2.         Workbook wb = new HSSFWorkbook();   
  3.         // Workbook wb = new XSSFWorkbook();   
  4.         CreationHelper createHelper = wb.getCreationHelper();   
  5.         Sheet sheet = wb.createSheet("new sheet");   
  6.   
  7.         // Create a row and put some cells in it. Rows are 0 based.   
  8.         Row row = sheet.createRow(0);   
  9.   
  10.         // Create a cell and put a date value in it. The first cell is not   
  11.         // styled   
  12.         // as a date.   
  13.         Cell cell = row.createCell(0);   
  14.         cell.setCellValue(new Date());   
  15.   
  16.         // we style the second cell as a date (and time). It is important to   
  17.         // create a new cell style from the workbook otherwise you can end up   
  18.         // modifying the built in style and effecting not only this cell but   
  19.         // other cells.   
  20.         CellStyle cellStyle = wb.createCellStyle();   
  21.         cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(   
  22.                 "yyyy/MM/dd hh:mm"));   
  23.         cell = row.createCell(1);   
  24.         // cell.setCellValue(new Date());   
  25.         Date date = new Date();   
  26.   
  27.         cell.setCellValue(createHelper.createRichTextString(date.toString()));   
  28.         cell.setCellStyle(cellStyle);   
  29.   
  30.         // you can also set date as java.util.Calendar   
  31.         cell = row.createCell(2);   
  32.         cell.setCellValue(Calendar.getInstance());   
  33.         cell.setCellStyle(cellStyle);   
  34.   
  35.         // Write the output to a file   
  36.         FileOutputStream fileOut = new FileOutputStream("workbook.xls");   
  37.         wb.write(fileOut);   
  38.         fileOut.close();   
  39.   
  40.     }  
Java代码   收藏代码
  1. <span style="font-size: medium;">public void createDateCell() throws Exception {  
  2.         Workbook wb = new HSSFWorkbook();  
  3.         // Workbook wb = new XSSFWorkbook();  
  4.         CreationHelper createHelper = wb.getCreationHelper();  
  5.         Sheet sheet = wb.createSheet("new sheet");  
  6.   
  7.         // Create a row and put some cells in it. Rows are 0 based.  
  8.         Row row = sheet.createRow(0);  
  9.   
  10.         // Create a cell and put a date value in it. The first cell is not  
  11.         // styled  
  12.         // as a date.  
  13.         Cell cell = row.createCell(0);  
  14.         cell.setCellValue(new Date());  
  15.   
  16.         // we style the second cell as a date (and time). It is important to  
  17.         // create a new cell style from the workbook otherwise you can end up  
  18.         // modifying the built in style and effecting not only this cell but  
  19.         // other cells.  
  20.         CellStyle cellStyle = wb.createCellStyle();  
  21.         cellStyle.setDataFormat(createHelper.createDataFormat().getFormat(  
  22.                 "yyyy/MM/dd hh:mm"));  
  23.         cell = row.createCell(1);  
  24.         // cell.setCellValue(new Date());  
  25.         Date date = new Date();  
  26.   
  27.         cell.setCellValue(createHelper.createRichTextString(date.toString()));  
  28.         cell.setCellStyle(cellStyle);  
  29.   
  30.         // you can also set date as java.util.Calendar  
  31.         cell = row.createCell(2);  
  32.         cell.setCellValue(Calendar.getInstance());  
  33.         cell.setCellStyle(cellStyle);  
  34.   
  35.         // Write the output to a file  
  36.         FileOutputStream fileOut = new FileOutputStream("workbook.xls");  
  37.         wb.write(fileOut);  
  38.         fileOut.close();  
  39.   
  40.     }</span>  

 

5.  创建不同的单元格样式

 

Java代码  复制代码
  1. public void createCellType() throws Exception{   
  2.         Workbook wb = new HSSFWorkbook();   
  3.         Sheet sheet = wb.createSheet("new sheet");   
  4.         Row row = sheet.createRow((short)2);   
  5.         row.createCell(0).setCellValue(1.1);   
  6.         row.createCell(1).setCellValue(new Date());   
  7.         row.createCell(2).setCellValue(Calendar.getInstance());   
  8.         row.createCell(3).setCellValue("a string");   
  9.         row.createCell(4).setCellValue(true);   
  10.         row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);   
  11.   
  12.         // Write the output to a file   
  13.         FileOutputStream fileOut = new FileOutputStream("workbook.xls");   
  14.         wb.write(fileOut);   
  15.         fileOut.close();   
  16.     }  
Java代码   收藏代码
  1. <span style="font-size: medium;">public void createCellType() throws Exception{  
  2.         Workbook wb = new HSSFWorkbook();  
  3.         Sheet sheet = wb.createSheet("new sheet");  
  4.         Row row = sheet.createRow((short)2);  
  5.         row.createCell(0).setCellValue(1.1);  
  6.         row.createCell(1).setCellValue(new Date());  
  7.         row.createCell(2).setCellValue(Calendar.getInstance());  
  8.         row.createCell(3).setCellValue("a string");  
  9.         row.createCell(4).setCellValue(true);  
  10.         row.createCell(5).setCellType(HSSFCell.CELL_TYPE_ERROR);  
  11.   
  12.         // Write the output to a file  
  13.         FileOutputStream fileOut = new FileOutputStream("workbook.xls");  
  14.         wb.write(fileOut);  
  15.         fileOut.close();  
  16.     }</span>  

 

6.  设置单元格水平垂直对齐方式

 

Java代码  复制代码
  1. public static void main(String[] args)  throws Exception {   
  2.         Workbook wb = new XSSFWorkbook(); //or new HSSFWorkbook();   
  3.   
  4.         Sheet sheet = wb.createSheet();   
  5.         Row row = sheet.createRow((short2);   
  6.         row.setHeightInPoints(30);   
  7.   
  8.         createCell(wb, row, (short0, XSSFCellStyle.ALIGN_CENTER, XSSFCellStyle.VERTICAL_BOTTOM);   
  9.         createCell(wb, row, (short1, XSSFCellStyle.ALIGN_CENTER_SELECTION, XSSFCellStyle.VERTICAL_BOTTOM);   
  10.         createCell(wb, row, (short2, XSSFCellStyle.ALIGN_FILL, XSSFCellStyle.VERTICAL_CENTER);   
  11.         createCell(wb, row, (short3, XSSFCellStyle.ALIGN_GENERAL, XSSFCellStyle.VERTICAL_CENTER);   
  12.         createCell(wb, row, (short4, XSSFCellStyle.ALIGN_JUSTIFY, XSSFCellStyle.VERTICAL_JUSTIFY);   
  13.         createCell(wb, row, (short5, XSSFCellStyle.ALIGN_LEFT, XSSFCellStyle.VERTICAL_TOP);   
  14.         createCell(wb, row, (short6, XSSFCellStyle.ALIGN_RIGHT, XSSFCellStyle.VERTICAL_TOP);   
  15.   
  16.         // Write the output to a file   
  17.         FileOutputStream fileOut = new FileOutputStream("xssf-align.xlsx");   
  18.         wb.write(fileOut);   
  19.         fileOut.close();   
  20.   
  21.     }   
  22.   
  23.     /**  
  24.      * Creates a cell and aligns it a certain way.  
  25.      *  
  26.      * @param wb     the workbook  
  27.      * @param row    the row to create the cell in  
  28.      * @param column the column number to create the cell in  
  29.      * @param halign the horizontal alignment for the cell.  
  30.      */  
  31.     private static void createCell(Workbook wb, Row row, short column, short halign, short valign) {   
  32.         Cell cell = row.createCell(column);   
  33.         cell.setCellValue(new XSSFRichTextString("Align It"));   
  34.         CellStyle cellStyle = wb.createCellStyle();   
  35.         cellStyle.setAlignment(halign);   
  36.         cellStyle.setVerticalAlignment(valign);   
  37.         cell.setCellStyle(cellStyle);   
  38.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值