java报表EXCEL解决方案
2024-07-13 09:54:58
供稿:网友
目录:
开发背景
开发思路
程序设计
如何调用
代码
总结
正文:
一、 开发背景
对于程序员特别是从事信息管理编程的程序员来说,报表打印是整个编程过程中最麻烦但又必须做的事情,我们常用的方法就是:
1、将数据库记录导出到excell中;
2、用crystalreport或activereport等报表工具生成报表文件然后再在程序中调用;
3、辅助active打印插件定制格式,直接打印窗体;
对于第二种方法网上与crystalreport或activereport相关的资料很少,而且使用也相当复杂,怎么制作报表全凭程序员自己摸索;
对于第三种方法一般是特别行业(即报表格式及数据不许修改)需要的,比如财务报表和政策法规或支付凭证等;
对第一种方法对普遍的行业适用,本文今天就重点就介绍了怎样将数据表记录导入到excell中并怎样控件excell单元格式以便做出合适的报表。
二、 开发思路
其实要开发一个控件只要做两件事,其一是定义各种属性和方法,其二是根据各种属性绘制控件界面。
下面我们来分析一下excell的结构,excell呈现给我们的是一张二维结构的表格,每一行相当于数据表的一个记录,每一列相当于数据表的字段。但excell的每个单元又有许多的格式控件单元的呈现方式,这就是excell与别的表格控件如datagrid不相同的地方,那么如何实现每个单元都有不同的呈现方式呢?
三、 程序设计
⑴基于以上设计思想,我首先设计了一个vo类excelin,它能模仿excell的结构,其属性为
文件生成在服务器的路径名:
private string path = null;
excell的每个单元又有许多的格式控件单元的呈现方式,每列的表头:
vector vtrtitle = null; // vector->string
每列所占的宽度:
int width[];
数据源,为简单设计用vector,如果结合数据持久层用ibatis的话,最好用list
vector vtrdata = null; // vector->vector->string。
⑵具体实现,使用类excel,它实现了数据到excel之间的交互
主要有两个方法:一个是生成excel文件createexcelfile(excelin excelin),
①得到数据源
vector vtrdata = excelin.getdata();
②建一个excel工作簿
hssfworkbook wb = new hssfworkbook();
③建一个excel工作表
hssfsheet sheet = wb.createsheet("sheet1");
④设置excel列宽
setcolumnwidth(sheet, excelin.getwidth());
⑤设置excel字体格式及其它设置
hssffont font = wb.createfont();
font.setfontname("宋体");
hssfcellstyle style = wb.createcellstyle();
style.setfont(font);
⑥最后通过循环将数据源一一写到指定的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(" ".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本身的特性实现报表的各种需求,但还不能完全满足需要,比如财务报表和政策法规或支付凭证不能修改的要求,需采用其他方式解决。本文作为一个引子,期盼与您交流,得到更好的方案。
菜鸟学堂: