在我们实际的开发中,表现层的解决方案虽然有多样,但是IE浏览器已成为最多人使用的浏览器,因为大家都用Windows。在企业办公系统中,常常有客户这样子要求:你要把我们的报表直接用Excel打开(电信系统、银行系统)。或者是:我们已经习惯用Excel打印。
Apache的Jakata项目的POI子项目,目前比较成熟的是HSSF接口,处理MSExcel对象。它不象我们仅仅是用csv生成的没有格式的可以由Excel转换的东西,而是真正的Excel对象,你可以控制一些属性如sheet,cell等等。
那么,如何利用POI在Excel文档任意单元格写入数据?最近做了个项目,一个人研究了下,现在把代码拿出来和大家分享下!不足之处请前辈们多多指教!
import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import javax.imageio.ImageIO; import javax.imageio.stream.ImageInputStream; import org.apache.poi.hssf.record.PageBreakRecord.Break; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFPatriarch; 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.poifs.filesystem.POIFSFileSystem; public class ReportBuilder { public static String outFile_Erro = "Load Template Erro,文件加载失败!!"; FileOutputStream fileOutputStream = null; HSSFWorkbook workbook = null; HSSFSheet sheet = null; HSSFPatriarch patriarch = null; /** * @用途:加载一个已经存在的模板,将生成的内容保存到 workbook中 * @参数:String templateFile:指索要加载的模板的路径,如:"C:/Tamplates/texting-1.xls" * @用法:templateFile: String templateFile_Name1 = "C:/Tamplates/texting-1.xls" * @author Yangcl */ public void loadTemplate(String templateURL) { // TODO Auto-generated method stub boolean a = templateURL.trim().indexOf(".xls") == -1; boolean b = templateURL.trim().indexOf(".XLS") == -1; // boolean c = templateURL.trim().indexOf(".xlsx") == -1; // boolean d = templateURL.trim().indexOf(".XLSX") == -1; if(templateURL == null || templateURL.trim().equals("") ) { //文件不能为空提示 System.out.println("文件不能为空提示"); } else if(a&&b)// && c&&d) { System.out.println("文件格式不正确!"); } else{ try{ FileInputStream templateFile_Input = new FileInputStream(templateURL); POIFSFileSystem fs = new POIFSFileSystem(templateFile_Input); workbook = new HSSFWorkbook(fs); sheet = workbook.getSheetAt(0); System.out.println("========"+templateURL+"文件加载已完成========"); }catch(Exception e){ System.err.println(outFile_Erro); } } } /** * 写入非图片格式信息 * @描述:这是一个实体类,提供了相应的接口,用于操作Excel,在任意坐标处写入数据。 * @参数:String newContent:你要输入的内容 * int beginRow :行坐标,Excel从 0 算起 * int beginCol :列坐标,Excel从 0 算起 * @author Yangcl */ public void writeInTemplate( String newContent, int beginRow, int beginCell) { HSSFRow row = sheet.getRow(beginRow); if(null == row ){ //如果不做空判断,你必须让你的模板文件画好边框,beginRow和beginCell必须在边框最大值以内 //否则会出现空指针异常 row = sheet.createRow(beginRow); } HSSFCell cell = row.getCell(beginCell); if(null == cell){ cell = row.createCell(beginCell); } //设置存入内容为字符串 cell.setCellType(HSSFCell.CELL_TYPE_STRING); //向单元格中放入值 cell.setCellValue(newContent); } /** * 写入图片格式信息 * @描述:这是一个实体类,提供了相应的接口,用于操作Excel,在任意坐标处写入数据。 * @参数: * String imageFileURL:他接受一个外界传入的图片路径,图片以 *.jpeg 形式存在。 * * @用法: * ReportBuilder twi = new ReportBuilder(); * String imageFileURL = "D:/workspace/Tamplates/1.jpeg"; * twi.writeInTemplate(imageFileURL , 0,0, 0, 0, (short)6, 5, (short)8, 8); * * @param dx1 :第一个cell开始的X坐标 * @param dy1 :第一个cell开始的Y坐标 * @param dx2 :第二个cell开始的X坐标 * @param dy2 :第二个cell开始的Y坐标 * @param col1 :图片的左上角放在第几个列cell (the column(o based); of the first cell) * @param row1 :图片的左上角放在第几个行cell (the row(o based); of the first cell) * @param col2 :图片的右下角放在第几个列cell (the column(o based); of the second cell) * @param row2 :图片的右下角放在第几个行cell (the row(o based); of the second cell) * * @author Yangcl */ public void writeInTemplate(String imageFileURL , int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2 ) { BufferedImage bufferImage = null; //写入图片格式信息 try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //先把读入的图片放到第一个 ByteArrayOutputStream 中,用于产生ByteArray File fileImage = new File(imageFileURL); bufferImage = ImageIO.read(fileImage); ImageIO.write(bufferImage, "JPG", byteArrayOutputStream); System.out.println("ImageIO 写入完成"); //准备插入图片 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2); //插入图片 byte[] pictureData = byteArrayOutputStream.toByteArray(); int pictureFormat = HSSFWorkbook.PICTURE_TYPE_JPEG; int pictureIndex = workbook.addPicture(pictureData, pictureFormat); patriarch.createPicture(anchor, pictureIndex); }catch(Exception e){ e.printStackTrace(); System.out.println("IO Erro:" + e.getMessage()); }finally { if(fileOutputStream != null) { try{ fileOutputStream.close(); }catch(IOException io){ io.printStackTrace(); } } } } /** * 写入图片格式信息 * @描述:这是一个实体类,提供了相应的接口,用于操作Excel,在任意坐标处写入数据。 * @参数: * ImageInputStream imageInputStream:他接受一个外界传入的图片流,图片以流形式存在。 * * @用法: * * * * * @param dx1 :第一个cell开始的X坐标 * @param dy1 :第一个cell开始的Y坐标 * @param dx2 :第二个cell开始的X坐标 * @param dy2 :第二个cell开始的Y坐标 * @param col1 :图片的左上角放在第几个列cell (the column(o based); of the first cell) * @param row1 :图片的左上角放在第几个行cell (the row(o based); of the first cell) * @param col2 :图片的右下角放在第几个列cell (the column(o based); of the second cell) * @param row2 :图片的右下角放在第几个行cell (the row(o based); of the second cell) * * @author Yangcl */ public void writeInTemplate(ImageInputStream imageInputStream , int dx1, int dy1, int dx2, int dy2, short col1, int row1, short col2, int row2 ) { BufferedImage bufferImage = null; //写入图片格式信息 try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); //先把读入的图片放到一个 ByteArrayOutputStream 中,用于产生ByteArray bufferImage = ImageIO.read(imageInputStream); ImageIO.write(bufferImage, "JPG", byteArrayOutputStream); System.out.println("ImageIO 写入完成"); //准备插入图片 HSSFPatriarch patriarch = sheet.createDrawingPatriarch(); HSSFClientAnchor anchor = new HSSFClientAnchor(dx1, dy1, dx2, dy2, col1, row1, col2, row2); //插入图片 byte[] pictureData = byteArrayOutputStream.toByteArray(); int pictureFormat = HSSFWorkbook.PICTURE_TYPE_JPEG; int pictureIndex = workbook.addPicture(pictureData, pictureFormat); patriarch.createPicture(anchor, pictureIndex); }catch(Exception e){ e.printStackTrace(); System.out.println("IO Erro:" + e.getMessage()); }finally { if(fileOutputStream != null) { try{ fileOutputStream.close(); }catch(IOException io){ io.printStackTrace(); } } } } /** * 保存模板 * @描述:这个方法用于保存workbook(工作薄)中的内容,并写入到一个Excel文件中 * @参数:String templateFile:取得已经保存的类模板 路径名称 * @用法:templateFile:String templateFile_Name1 = "C:/Tamplates/texting-1.xls" * TemplateAdapter ta = new TemplateAdapter(); * ta.SaveTemplate(templateFile_Name1); * @param templateFile */ public void SaveTemplate(String templateFile) { try{ //建立输出流 fileOutputStream = new FileOutputStream(templateFile); workbook.write(fileOutputStream); }catch(Exception e){ e.printStackTrace(); System.out.println("IO Erro" + e.getMessage()); }finally { if(fileOutputStream != null) { try{ fileOutputStream.close(); }catch(IOException io){ io.printStackTrace(); } } } } }
这段代码是完整的。我开始找了很久关于如何利用POI在Excel文档任意单元格写入数据的文章,从而实现一个以面向服务为目标的报表系统,但很遗憾,在国内的网站上没有找到。而且介绍的比较基础,对于急需使用POI,但又没有时间仔细研究的人来说,难过急了。上面的代码有比较详细的注释,相信大家看的懂。如果您要转载这篇文章,请注明出处。开源万岁~祝您成功。文档依赖的两个 jar包:commons-codec-1.5.jar和poi-3.8-20120326.jar见附件。这字体真让人蛋疼菊紧!
更多信息请浏览:http://www.open-open.com/home/space-135360-do-blog-id-9614.html
相关推荐
poi生成excel及合并单元格示例,详见文章http://blog.csdn.net/geloin/article/details/17219885
poi获取exccel有合并单元格的数据,存入List
POI操作Excel 合并单元格 边框问题解决方法,这个方法是我亲自测试,并运用于项目中的,可以放心使用,还一并写出了很多注释,其中有写poi的bug的解决方法
利用poi进行excel相同内容合并单元格,只摘要了传入部分功能,请关注addMergedRegion方法
利用POI读取excel写入到word 利用POI读取excel写入到word 利用POI读取excel写入到word,压缩包里包含了jar包
java excel poi合并单元格
前几天项目上需要加上导入导出,在网上找了一下,么有...于是自己写,源码里所需要的jar都有,从jsp界面进行Excel的导入导出,并可用于上传和下载。如有所用,喜不自禁,分享,开源的意义。 写的不是特别好,不喜勿喷!
使用poi解析excel文件,并将数据写入到数据库 项目说明 这个项目实现的功能是读取excel文件中的数据,解析并写入数据库。 读取的excel文件位于项目目录下的 excel\0805.xlsx 使用IntelliJ IDEA开发此项目 使用MYSQL...
POI中设置Excel单元格格式
这个是使用java语言 poi导入excel到数据库中,同时使用了xml对excel的每个单元格数据进行了非空验证。经测试,导入数据库成功。
POI读写excel文件+poi简单文档; POI读写excel文件+poi简单文档。
可以运行的POI导出Excel文件实例,里面有两种方法,一个是Servlet,一个是main
java使用poi在excel单元格添加超链接,设置字体颜色(csdn)————程序
利用 Apache POI 对 excel 操作并存入MySQL数据库的一个小demo。
POI excel单元格顔色设定
ExcelDemo_Excel导出_下载_POI读excel_JXL读写excel_java读写excel_列宽_读取合并或拆分单元格内容
poi读写excel,poi总结; poi读写excel,poi总结 poi读写excel,poi总结
poi获取合并单元格,Java 对excel 文档处理。
该资源主要实现如下功能,利用poi获取excel中大量数据,然后利用jdbc批量插入到mysql
POI导出Excel工具类,自动设置标题 列名 文件名,可插入图片,合并单元格