报表导出功能实现
来源:互联网 发布:java浏览器下载 编辑:程序博客网 时间:2024/05/17 23:58
近期做的项目中涉及到报表导出的功能,在这里进行小小总结一下,希望能帮助到到家。
首先实现报表导出到指定文件下,以excel格式报表为例进行说吧。
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import jxl.Workbook;
import jxl.format.BorderLineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class CreateSalaryExcel {
import jxl.format.BorderLineStyle;
import jxl.write.DateFormat;
import jxl.write.DateTime;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;
public class CreateSalaryExcel {
private OutputStream os = null;//写工作流用于在指定位置生成文件
private WritableWorkbook wwb = null;//新建一个工作表
private WritableSheet wb = null;//sheet
private int no = 3;//数据加载的起始行
private WritableWorkbook wwb = null;//新建一个工作表
private WritableSheet wb = null;//sheet
private int no = 3;//数据加载的起始行
public String createExcel(){
String path = f:\test.xls;
try {
os = new FileOutputStream(path);//生成空的表格
wwb = Workbook.createWorkbook(os);//新建工作薄
wb = wwb.createSheet(fileName, 0);//设定工作薄名称
try {
wb.mergeCells(0, 0, 9, 0);//合并第一列第一行到第9列第一行的所有单元格
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 表中要用到的样式
*/
//主标题字体样式
WritableFont font1 = new WritableFont(WritableFont.ARIAL,24,WritableFont.BOLD); //设定字体样式
WritableCellFormat format1 = new WritableCellFormat(font1);//样式添加到
try {
format1.setAlignment(jxl.format.Alignment.CENTRE);//居中
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Label label1 = new Label(0, 0, date+fileName, format1);//添加lable对象
try {
wb.addCell(label1);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("副标题样式");
//副标题样式
WritableFont font2 = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD);//设定字体样式
WritableCellFormat format2 = new WritableCellFormat(font2);//样式加载到
try {
format2.setAlignment(jxl.format.Alignment.CENTRE);//居中
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
wb.mergeCells(0, 1, 1, 1);//合并单元格 第一个参数为第几行,第二个参数为那到列开始合并,第三个参数为到那一列结束,第四个参数为合并几行
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Label label2 = new Label(0,1, "报表生成时间:", format2);
try {
wb.addCell(label2);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 日期样式
DateFormat df = new DateFormat("yyyy-MM-dd");
WritableCellFormat wcfdF = new WritableCellFormat(df);
try {
wcfdF.setAlignment(jxl.format.Alignment.LEFT);
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
wb.mergeCells(2, 1, 3, 1);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateTime label3 = new DateTime(2, 1, new Date(), wcfdF);//所有时间参数用jxl.write
try {
wb.addCell(label3);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//标题样式
System.out.println("标题样式");
WritableFont font3 = new WritableFont(WritableFont.TIMES,10,WritableFont.BOLD);
WritableCellFormat format3 = new WritableCellFormat(font3);
try {
format3.setAlignment(jxl.format.Alignment.CENTRE);
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
format3.setBorder(jxl.format.Border.ALL, BorderLineStyle.THIN);//border要用:jxl.format.Border;
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
format3.setWrap(true);
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("标题内容");
os = new FileOutputStream(path);//生成空的表格
wwb = Workbook.createWorkbook(os);//新建工作薄
wb = wwb.createSheet(fileName, 0);//设定工作薄名称
try {
wb.mergeCells(0, 0, 9, 0);//合并第一列第一行到第9列第一行的所有单元格
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/**
* 表中要用到的样式
*/
//主标题字体样式
WritableFont font1 = new WritableFont(WritableFont.ARIAL,24,WritableFont.BOLD); //设定字体样式
WritableCellFormat format1 = new WritableCellFormat(font1);//样式添加到
try {
format1.setAlignment(jxl.format.Alignment.CENTRE);//居中
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Label label1 = new Label(0, 0, date+fileName, format1);//添加lable对象
try {
wb.addCell(label1);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("副标题样式");
//副标题样式
WritableFont font2 = new WritableFont(WritableFont.ARIAL, 16,WritableFont.BOLD);//设定字体样式
WritableCellFormat format2 = new WritableCellFormat(font2);//样式加载到
try {
format2.setAlignment(jxl.format.Alignment.CENTRE);//居中
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
wb.mergeCells(0, 1, 1, 1);//合并单元格 第一个参数为第几行,第二个参数为那到列开始合并,第三个参数为到那一列结束,第四个参数为合并几行
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Label label2 = new Label(0,1, "报表生成时间:", format2);
try {
wb.addCell(label2);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 日期样式
DateFormat df = new DateFormat("yyyy-MM-dd");
WritableCellFormat wcfdF = new WritableCellFormat(df);
try {
wcfdF.setAlignment(jxl.format.Alignment.LEFT);
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
wb.mergeCells(2, 1, 3, 1);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DateTime label3 = new DateTime(2, 1, new Date(), wcfdF);//所有时间参数用jxl.write
try {
wb.addCell(label3);
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//标题样式
System.out.println("标题样式");
WritableFont font3 = new WritableFont(WritableFont.TIMES,10,WritableFont.BOLD);
WritableCellFormat format3 = new WritableCellFormat(font3);
try {
format3.setAlignment(jxl.format.Alignment.CENTRE);
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
format3.setBorder(jxl.format.Border.ALL, BorderLineStyle.THIN);//border要用:jxl.format.Border;
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
format3.setWrap(true);
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("标题内容");
//表格头部
Label label4 = new Label(0, 2, "时间", format3);
wb.addCell(label4);
label4 = new Label(1, 2,"ip", format3);
wb.addCell(label4);
label4 = new Label(2, 2,文件名", format3);
wb.addCell(label4);
label4 = new Label(3, 2,"MD5值", format3);
wb.addCell(label4);
label4 = new Label(4, 2,"存放路径", format3);
wb.addCell(label4);
//内容字体样式
System.out.println("内容字体样式");
WritableFont font4 = new WritableFont(WritableFont.TIMES, 9, WritableFont.NO_BOLD);
WritableCellFormat format4 = new WritableCellFormat(font4);
format4.setAlignment(jxl.format.Alignment.CENTRE);
//添加数据
System.out.println("添加数据");
wb.addCell(label4);
label4 = new Label(1, 2,"ip", format3);
wb.addCell(label4);
label4 = new Label(2, 2,文件名", format3);
wb.addCell(label4);
label4 = new Label(3, 2,"MD5值", format3);
wb.addCell(label4);
label4 = new Label(4, 2,"存放路径", format3);
wb.addCell(label4);
//内容字体样式
System.out.println("内容字体样式");
WritableFont font4 = new WritableFont(WritableFont.TIMES, 9, WritableFont.NO_BOLD);
WritableCellFormat format4 = new WritableCellFormat(font4);
format4.setAlignment(jxl.format.Alignment.CENTRE);
//添加数据
System.out.println("添加数据");
//这里对要导出的数据进行遍历,填入对应的位置
for (Map<String, String> map : list) {
Label one = new Label(0, no, map.get("time"), format4);
wb.addCell(one);
System.out.println(map.get("filePath"));
Label tow = new Label(1, no, map.get("guid"), format4);
wb.addCell(tow);
Label three = new Label(2, no, map.get("fileName"), format4);
System.out.println( map.get("fileName"));
wb.addCell(three);
Label four = new Label(3, no, map.get("MD5"), format4);
wb.addCell(four);
Label five = new Label(4, no, map.get("filePath"), format4);
wb.addCell(five);
Label one = new Label(0, no, map.get("time"), format4);
wb.addCell(one);
System.out.println(map.get("filePath"));
Label tow = new Label(1, no, map.get("guid"), format4);
wb.addCell(tow);
Label three = new Label(2, no, map.get("fileName"), format4);
System.out.println( map.get("fileName"));
wb.addCell(three);
Label four = new Label(3, no, map.get("MD5"), format4);
wb.addCell(four);
Label five = new Label(4, no, map.get("filePath"), format4);
wb.addCell(five);
}
wwb.write();//把数据写入表中
return path;
} catch (IOException e) {return path;
// TODO Auto-generated catch block
e.printStackTrace();
} catch (RowsExceededException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
//关闭操作对像
wwb.close();
os.close();
} catch (WriteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// }else{
//
// path = "no";
// }
try {
downloadExcel(null, null, path);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return path;
}
protected void downloadExcel(HSSFWorkbook workbook, HttpServletResponse response, String filename) throws IOException {
OutputStream out = response.getOutputStream();
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
response.setContentType("application/msexcel;charset=UTF-8");
workbook.write(out);
out.close();
}
}
这样就把数据导出到了指定的位置了,但是实际开发中并不能让报表导出到指定位置去,是用户选择存档的,所以以上这种方式不正确,下来对以上方式进行一个小小的改动就ok了。
上面的方法不是返回了个path吗,咱就从这下手,我的思路是先把文件存放到服务器上的指定位置上去,然后利用struts的下载功能找到该路径下载该文件,下载完后删不删就是你的事了,自己看着办。
//这个方法实现从服务器下载文件
public void report(String pc){//得到服务器上文件的存放路径
HttpServletResponse response = super.getResponse();
HttpServletRequest request = super.getRequest();
response.setContentType("application/x-msdownload");
//压缩后文件的存放路径
String zipfile = pc.substring(0,76)+"report.zip";//这是我文件的存放路径,存放在tomcat下,如果你也想将文件存放在tomcat下可以使用String dirPath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
String relPath = pc.substring(0, 85);
CompressBook book = new CompressBook();
try {
book.zip(relPath, zipfile);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response.addHeader("Content-Disposition", "attachment;filename=Statements.zip");
FileInputStream fileInputStream = null;
byte[] buf = new byte[1024];
int readLength ;
try {
fileInputStream = new FileInputStream(zipfile);
while ((readLength = fileInputStream.read(buf)) != -1) {
response.getOutputStream().write(buf,0,readLength);
}
response.getOutputStream().flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileInputStream.close();
response.getOutputStream().close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
HttpServletResponse response = super.getResponse();
HttpServletRequest request = super.getRequest();
response.setContentType("application/x-msdownload");
//压缩后文件的存放路径
String zipfile = pc.substring(0,76)+"report.zip";//这是我文件的存放路径,存放在tomcat下,如果你也想将文件存放在tomcat下可以使用String dirPath=Thread.currentThread().getContextClassLoader().getResource("").getPath();
String relPath = pc.substring(0, 85);
CompressBook book = new CompressBook();
try {
book.zip(relPath, zipfile);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
response.addHeader("Content-Disposition", "attachment;filename=Statements.zip");
FileInputStream fileInputStream = null;
byte[] buf = new byte[1024];
int readLength ;
try {
fileInputStream = new FileInputStream(zipfile);
while ((readLength = fileInputStream.read(buf)) != -1) {
response.getOutputStream().write(buf,0,readLength);
}
response.getOutputStream().flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
fileInputStream.close();
response.getOutputStream().close();
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
我这个实现了对文件进行压缩后下载,原因是因为我会有多个文件在一个文件夹中。
- 报表导出功能实现
- Java实现POI导出Excel报表功能
- 报表导出功能
- 报表导出到word或者excel的功能实现
- Jquery构建表单+SpringMVC实现导出Excel报表功能
- 数据报表导出Excel功能
- 报表导出模块实现
- JSP实现报表导出
- 导出报表的实现
- 报表功能实现
- 报表功能实现
- 报表excel导出实现代码
- springmvc poi实现报表导出
- 如何让Web应用程序在Client端实现导出报表功能
- 如何让Web应用程序在Client端实现导出报表功能
- 如何让Web应用程序在Client端实现导出报表功能
- 水晶报表导出为pdf, word , excel 格式(已经测试过,实现了该功能)
- 使用highcharts来实现报表的展示以及打印和导出功能
- 构造函数能不能是虚函数
- [C#]简单重写IComparer接口,实现自己的String.CompareTo 方法,自定义比较规则。
- db2 uuid
- C# winForm程序开机启动和托盘显示
- Project has no default.properties file! Edit the project properties to set one
- 报表导出功能实现
- windows下X server运行OpenGL程序的问题
- socket
- 因为,能够说出来,大约都不是真的
- pendingIntent浅析
- java 环境变量的配置
- Java内存模型详解
- 简析Android平台中的多线程、Handler应用
- qq、旺旺。msn第三方登陆,附代码