poi 新手教程

xiaoxiao2024-04-12  17

Jakarta_POI 使用Java读写Excel(97-2002)文件,可以满足大部分的需要。因为刚好有一个项目使用到了这个工具,花了点时间顺便翻译了一下POI本身带的一个Guide.有一些节减和修改,希望给使用这个项目的人一些入门帮助。POI 下面有几个自项目:HSSF用来实现Excel 的读写.以下是HSSF的主页http://jakarta.apache.org/poi/hssf/index.html下面的介绍是基于以下地址的翻译:http://jakarta.apache.org/poi/hssf/quick-guide.html目前的版本为1.51应该是很长时间之内的一个稳定版,但HSSF提供的Sample不是基于1.51所写,所以使用的时候需要适当的注意.其实POI下面的几个子项目侧重不同读写 Word 的HDF正在开发当中.XML下的FOP(http://xml.apache.org/fop/index.html)可以输出pdf文件,也是比较好的一个工具目录:创建一个workbook创建一个sheet创建cells创建日期cells设定单元格格式说明:以下可能需要使用到如下的类import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFCellStyle;import org.apache.poi.hssf.usermodel.HSSFDataFormat;import org.apache.poi.hssf.usermodel.HSSFFont;import org.apache.poi.hssf.usermodel.HSSFRow;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.HSSFColor;创建workbookHSSFWorkbook wb = new HSSFWorkbook();//使用默认的构造方法创建workbookFileOutputStream fileOut = new FileOutputStream("workbook.xls");//指定文件名wb.write(fileOut);//输出到文件fileOut.close();创建一个sheetHSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet1 = wb.createSheet("new sheet");//workbook创建sheetHSSFSheet sheet2 = wb.createSheet("second sheet");//workbook创建另外的sheetFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();创建cellsHSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("new sheet");//注意以下的代码很多方法的参数是short 而不是int 所以需要做一次类型转换HSSFRow row = sheet.createRow((short)0);//sheet 创建一行HSSFCell cell = row.createCell((short)0);//行创建一个单元格cell.setCellValue(1);//设定单元格的值//值的类型参数有多中double ,String ,boolean,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 fileFileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();创建日期cellsHSSFWorkbook wb = new HSSFWorkbook();HSSFSheet sheet = wb.createSheet("new sheet");HSSFRow row = sheet.createRow((short)0);HSSFCell cell = row.createCell((short)0);//设定值为日期cell.setCellValue(new Date());HSSFCellStyle cellStyle = wb.createCellStyle();//指定日期显示格式cellStyle.setDataFormat(HSSFDataFormat.getFormat("m/d/yy h:mm"));cell = row.createCell((short)1);cell.setCellValue(new Date());//设定单元格日期显示格式cell.setCellStyle(cellStyle);FileOutputStream fileOut = new FileOutputStream("workbook.xls");wb.write(fileOut);fileOut.close();设定单元格格式单元格格式的设定有很多形式包括单元格的对齐方式,内容的字体设置,单元格的背景色等,因为形式比较多,只举一些例子.以下的例子在POI1.5中可能会有所改变具体查看API...........// Aqua backgroundHSSFCellStyle style = wb.createCellStyle();//创建一个样式style.setFillBackgroundColor(HSSFCellStyle.AQUA);//设定此样式的的背景颜色填充style.setFillPattern(HSSFCellStyle.BIG_SPOTS);//样式的填充类型。//有多种式样如://HSSFCellStyle.BIG_SPOTS//HSSFCellStyle.FINE_DOTS//HSSFCellStyle.SPARSE_DOTS等style.setAlignment(HSSFCellStyle.ALIGN_CENTER );//居中对齐style.setFillBackgroundColor(HSSFColor.GREEN.index);//设定单元个背景颜色style.setFillForegroundColor(HSSFColor.RED.index);//设置单元格显示颜色HSSFCell cell = row.createCell((short) 1);cell.setCellValue("X");cell.setCellStyle(style);

 

 

使用poi的hssf生成一个excel文件以后有一个主类Workbook(相当于一个excel文件)的方法Workbook.write(OutputStream)可以写到response.getOutputStream()里面如果事先设置response的contentType为excel和下载的附件名称就可下载excel

    HSSFWorkbook book = _proxy.expertExcel(_formBean,_login);     if(book!=null)     {         response.setContentType ( "application/ms-excel" ) ;         response.setHeader ( "Content-Disposition" ,                              "attachment;filename="+new String("导出Excel.xls".getBytes(),"iso-8859-1")) ;         book.write(response.getOutputStream());     }

其中expertExcel无非是从数据库或者其他地方获取数据创建excel即可.

转载请注明原文地址: https://www.6miu.com/read-5014937.html

最新回复(0)