技术开发 频道

java报表EXCEL解决方案



    这个要结合数据库和WEB应用来说,最好让数据库返回的值刚好是EXCEL的值类型,具体调试文件见下面:


public TestExcel(){ public static viod main(String[] args){ TestExcel test=new TestExcel(); Test.test(); } public void test(){ ExcelIn excelIn=new ExcelIn(); ExcelIn.setPath(“./”); Vtr..add(2004-10); ……… createExcelFile(Vtr); } private void createExcelFile(ExcelIn excelIn){ Vector vtr= excelIn. getData(); excelIn.setPath(this.excelPath); int[] width = {10, 22, 22, 22, 22, 12}; excelIn.setWidth(width); Vector vtrTitle = new Vector(); vtrTitle.add("年月"); vtrTitle.add("PM工作量"); vtrTitle.add("PM成本预算"); vtrTitle.add("CM工作量"); vtrTitle.add("PM成本预算"); vtrTitle.add("期段"); excelIn.setTitle(vtrTitle); excelIn.setData(vtr); new Excel().createExcelFile(excelIn); } }   (目录)   五、 代码 package common; import java.io.*; import java.util.Vector; import org.apache.poi.hssf.usermodel.*; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: MRO</p> * @author Kevin zhou * @version 1.0 */ public class Excel { public void createExcelFile(ExcelIn excelIn) throws CommonException{ Vector vtrData = excelIn.getData(); HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet sheet = wb.createSheet("Sheet1"); setColumnWidth(sheet, excelIn.getWidth()); HSSFFont font = wb.createFont(); font.setFontName("宋体"); HSSFCellStyle style = wb.createCellStyle(); style.setFont(font); if(vtrData != null){ Vector vtrTitle = excelIn.getTitle(); HSSFRow rowTitle = sheet.createRow(0); for(int i=0;i<vtrTitle.size();i++){ HSSFCell cell = rowTitle.createCell((short)i); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue((String)vtrTitle.get(i)); cell.setCellStyle(style); } for(int i=0;i<vtrData.size();i++){ HSSFRow row = sheet.createRow(i+1); Vector vtrRow = (Vector)vtrData.get(i); for(int j=0;j<vtrRow.size();j++){ String strTemp = (String)vtrRow.get(j); if("&nbsp;".equals(strTemp)){ strTemp = " "; } HSSFCell cell = row.createCell((short)j); cell.setEncoding(HSSFCell.ENCODING_UTF_16); cell.setCellValue(strTemp); cell.setCellStyle(style); } } } try{ // Write the output to a file FileOutputStream fileOut = new FileOutputStream(excelIn.getPath()); wb.write(fileOut); fileOut.close(); }catch(Exception e){ throw new CommonException("文件已经打开,请关闭后再生成"); } } /** * Set Column Width */ private void setColumnWidth(HSSFSheet sheet, int[] width){ for(int i=0;i<width.length;i++){ sheet.setColumnWidth((short)i, (short)(width[i]*256)); } } } package common; import java.util.Vector; /** * <p>Title: </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: MRO</p> * @author Kouken * @version 1.0 */ public class ExcelIn { private String path = null; Vector vtrData = null; // vector->vector->String Vector vtrTitle = null; // vector->String int width[]; public String getPath(){ return this.path; } public void setPath(String path){ this.path = path; } public Vector getData(){ return this.vtrData; } public void setData(Vector vtrData){ this.vtrData = vtrData; } public Vector getTitle(){ return this.vtrTitle; } public void setTitle(Vector vtrTitle){ this.vtrTitle = vtrTitle; } public int[] getWidth(){ return this.width; } public void setWidth(int width[]){ this.width = width; } }

  六、 总结

  B/S报表有很多实现方式,本文是利用EXCEL本身的特性实现报表的各种需求,但还不能完全满足需要,比如财务报表和政策法规或支付凭证不能修改的要求,需采用其他方式解决。本文作为一个引子,期盼与您交流,得到更好的方案。

0
相关文章