利用java怎么实现生成报表(Excel文件)
发布时间:2025-05-22 12:50:13 发布人:远客网络
一、利用java怎么实现生成报表(Excel文件)
1、这是从Tabel导出数据到Excel的一个例子:jxl.jar包你可以去网上找,有很多资源,如果没找到,也可以留个邮箱,我发给你
2、import java.text.SimpleDateFormat;
3、import javax.swing.JOptionPane;
4、import jxl.format.UnderlineStyle;
5、import jxl.format.VerticalAlignment;
6、import jxl.read.biff.BiffException;
7、import jxl.write.WritableCellFormat;
8、import jxl.write.WritableFont;
9、import jxl.write.WritableSheet;
10、import jxl.write.WritableWorkbook;
11、import jxl.write.WriteException;
12、 public static void export(File file,String heading,String note,JTable table){
13、 WritableWorkbook workbook= null;//创建工作薄
14、 if(file.exists()){//如果文件存在
15、 workbook= Workbook.createWorkbook(file, Workbook.getWorkbook(file));
16、 workbook= Workbook.createWorkbook(file);
17、 WritableSheet sheet= workbook.createSheet(heading, workbook.getNumberOfSheets());
18、 int rowNum= table.getRowCount();
19、 int colNum= table.getColumnCount();
20、 fillHeader(sheet,heading,colNum);//填写主标题
21、 fillColName(sheet,table,colNum);//填写列名
22、 fillCell(sheet,table,colNum,rowNum);//填写数据
23、 fillNote(sheet,note,colNum,rowNum);//填写签名档
24、 JOptionPane.showMessageDialog(null,"导入数据前请关闭工作表");
25、 private static void fillHeader(WritableSheet sheet,String heading,int colNum) throws WriteException{
26、 WritableFont font= new WritableFont(WritableFont.ARIAL, 18, WritableFont.BOLD,false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);//设置字体
27、 WritableCellFormat format= new WritableCellFormat(font);//新建格式化对象
28、 format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
29、 format.setVerticalAlignment(VerticalAlignment.CENTRE);//设置垂直居中对齐
30、 sheet.mergeCells(0,0, colNum-1, 0);//合并单元格
31、 sheet.setRowView(0, 600);//设置行高
32、 sheet.addCell(new Label(0,0,heading,format));//填写主标题
33、 private static void fillColName(WritableSheet sheet,JTable table,int colNum) throws WriteException{
34、 WritableFont font= new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体
35、 WritableCellFormat format= new WritableCellFormat(font);//新建格式化对象
36、 format.setAlignment(Alignment.CENTRE);//设置水平居中对齐
37、 sheet.setColumnView(0, 15);//设置列宽
38、 for(int col= 0; col< colNum;col++){
39、 Label colName= new Label(col,1,table.getModel().getColumnName(col),format);
40、 private static void fillCell(WritableSheet sheet,JTable table,int colNum,int rowNum) throws WriteException{
41、 WritableFont font= new WritableFont(WritableFont.ARIAL,12,WritableFont.NO_BOLD);//设置字体
42、 WritableCellFormat format= new WritableCellFormat(font);//新建格式化对象
43、 format.setAlignment(Alignment.CENTRE);//设置水平居中
44、 for(int col= 0;col< colNum;col++){
45、 for(int row= 1;row<= rowNum-1;row++){//填写数据
46、 if(table.getModel().getValueAt(row-1, col)!= null)
47、 value= table.getModel().getValueAt(row-1, col).toString();
48、 sheet.addCell(new Label(col,row+ 1,value));
49、 private static void fillNote(WritableSheet sheet,String inscribe,int colNum,int rowNum) throws WriteException{
50、 if( inscribe== null|| inscribe.length()< 1){
51、 inscribe="导出时间:"+ new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
52、 WritableFont font= new WritableFont(WritableFont.ARIAL, 9, WritableFont.NO_BOLD,
53、 false, UnderlineStyle.NO_UNDERLINE, Colour.BLACK);//定义字体
54、 WritableCellFormat format= new WritableCellFormat(font);//定义格式化对象
55、 format.setAlignment(Alignment.RIGHT);//水平居中显示
56、 sheet.mergeCells(0, rowNum+3, colNum- 1, rowNum+3);//合并单元格
57、 sheet.addCell(new Label(0, rowNum+3, inscribe, format));//填写工作表
二、java做报表
1、把数据库中需要的数据处理后做成可视化图表,根据不同需求可以做成不同形式的图表,节省时间,效果比较好,报表软件国外的话水晶报表,SAP公司的商业报表工具,作为SAP“集团”下的报表组件模块。10年事前盛行一时,后被SAP收购。但水晶报表(Crystal Report)在理论上只支持单数据集,对多集的支持依赖于数据库的运算能力(叉乘与联合等或写存储过程),多库一般难以支持。
2、国内的话FineReport,目前国内报表软件领域发展最成熟也是市场份额最高的。IDC的报告里,17年甚至超过SAP,IBM,在这个细分领域,也是Gartner的BI选型市场指南里唯一推荐的报表工具。零代码开发,类似excel的设计方式,上手简单。尤其数据字典、实际值和显示值等的设计更是切入快速开发的要害,这些放在别的语言和体系下往往要大量代码才能实现且频繁需要的功能放在FineReport里却只是几个操作的事情。移动端报表+可视化大屏。
三、java 实现报表统计
1、java本身没有操作excel的工具,需要第三方的jar包,用jxl就可以,代码入下。
2、jxl你上百度搜索后下载就可以,简单易用,不懂追问。
3、public boolean exportExcel(HttpServletResponse response,List<cityinfo> list)
4、OutputStream os= response.getOutputStream();//取得输出流
5、 response.reset();//清空输出流
6、 response.setHeader("Content-disposition","attachment; filename=fine.xls");//设定输出文件头
7、 response.setContentType("application/msexcel");//定义输出类型
8、 WritableWorkbook wbook= Workbook.createWorkbook(os);//建立excel文件
9、 String tmptitle="标题";//标题
10、 WritableSheet wsheet= wbook.createSheet("详细信息表", 0);// sheet名称
11、WritableSheet wsheet= wbook.createSheet("性别统计表", 1);// sheet名称
12、WritableSheet wsheet= wbook.createSheet("证件类型统计表", 2);// sheet名称
13、WritableFont wfont= new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD,
14、 false,UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
15、WritableCellFormat wcfFC= new WritableCellFormat(wfont);
16、wcfFC.setBackground(Colour.AQUA);
17、wsheet.addCell(new Label(1, 0, tmptitle, wcfFC));
18、wfont= new jxl.write.WritableFont(WritableFont.ARIAL, 14,WritableFont.BOLD,
19、 false, UnderlineStyle.NO_UNDERLINE,Colour.BLACK);
20、wcfFC= new WritableCellFormat(wfont);
21、wsheet.addCell(new Label(0, 2,"具体内容"));
22、for(int i=0;i<list.size();i++)<br="">{
23、 wsheet.addCell(new Label(0, i+3,"");
24、 wsheet.addCell(new Label(1, i+3,"");