1、得到POI包java
wget http://apache.fayea.com/poi/release/bin/poi-bin-3.14.tar.gzapache
2、解开后导入构建路径测试
3、测试excel
package www.zjptcc.wxw.poi; import org.apache.poi.hpsf.DocumentSummaryInformation; import org.apache.poi.hpsf.SummaryInformation; import org.apache.poi.hssf.usermodel.*; import java.io.FileOutputStream;// code run against the jakarta-poi-1.5.0-FINAL-20020506.jar. import java.util.Date; public class PoiTestWrite { private static HSSFWorkbook wb; static public void main(String[] args) throws Exception { FileOutputStream fos = new FileOutputStream( "./test.xls"); wb = new HSSFWorkbook(); HSSFSheet first_sheet = wb.createSheet(); wb.setSheetName(0, "first sheet "); HSSFRow row = first_sheet.createRow(0); HSSFCell cell = row.createCell(0); cell.setCellValue( "Hello! This message is generated from POI. "); //建立单元格 HSSFSheet test_sheet = wb.createSheet("Test");// 建立工做表(Sheet) row = test_sheet.createRow(0);// 建立行,从0开始 cell = row.createCell(0);// 建立行的单元格,也是从0开始 cell.setCellValue("李志伟");// 设置单元格内容 row.createCell(1).setCellValue(false);// 设置单元格内容,重载 row.createCell(2).setCellValue(new Date());// 设置单元格内容,重载 row.createCell(3).setCellValue(12.345);// 设置单元格内容,重载 //建立批注 HSSFPatriarch patr = test_sheet.createDrawingPatriarch(); HSSFClientAnchor anchor = patr.createAnchor(0, 0, 0, 0, 5, 1, 8, 3);//建立批注位置 HSSFComment comment = patr.createCellComment(anchor);//建立批注 comment.setString(new HSSFRichTextString("这是一个批注段落!"));//设置批注内容 comment.setAuthor("李志伟");//设置批注做者 comment.setVisible(true);//设置批注默认显示 cell = test_sheet.createRow(2).createCell(1); cell.setCellValue("测试"); cell.setCellComment(comment);//把批注赋值给单元格 //建立文档摘要信息 wb.createInformationProperties();//建立文档信息 DocumentSummaryInformation dsi= wb.getDocumentSummaryInformation();//摘要信息 dsi.setCategory("类别:Excel文件");//类别 dsi.setManager("管理者:李志伟");//管理者 dsi.setCompany("公司:--");//公司 SummaryInformation si = wb.getSummaryInformation();//摘要信息 si.setSubject("主题:--");//主题 si.setTitle("标题:测试文档");//标题 si.setAuthor("做者:李志伟");//做者 si.setComments("备注:POI测试文档");//备注 wb.write(fos); fos.close(); System.out.println("xls文件被保存!"); } }
4、code
5、读orm
package www.zjptcc.wxw.poi; import org.apache.poi.poifs.filesystem.*; import org.apache.poi.hssf.usermodel.*; import java.io.*; public class PoiTestRead { private static HSSFWorkbook wb; static public void main(String[] args) throws Exception { POIFSFileSystem fs=new POIFSFileSystem(new FileInputStream("./111.xls")); wb = new HSSFWorkbook(fs); HSSFSheet sheet=wb.getSheetAt(1); //获得Excel工做表的sheet编号,0开始 HSSFRow row=sheet.getRow(1); //获得Excel工做表指定行,参数0开始对应excel表第1行 HSSFCell cell=row.getCell(3); //获得Excel工做表指定行的单元格(列),参数0开始对应excel表第A列 String msg = cell.getStringCellValue(); //获得单元格内容 System.out.println("从当前目录下读取excel文件:111.xls..."); System.out.print("第二个sheet 2D单元格的内容:"); System.out.println(msg); sheet=wb.getSheetAt(0); row=sheet.getRow(0); cell=row.getCell(0); System.out.print("第一个sheet 1A单元格的内容:"); System.out.println(cell.getStringCellValue()); } }