1.Poi生成excel
1)创建HSSFWorkbook对象
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet("运单数据");
2)设置表头:
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("运单号");
headRow.createCell(1).setCellValue("寄件人");
headRow.createCell(2).setCellValue("寄件人电话");
headRow.createCell(3).setCellValue("寄件人地址");
headRow.createCell(4).setCellValue("收件人");
headRow.createCell(5).setCellValue("收件人电话");
headRow.createCell(6).setCellValue("收件人地址");
3)添加表头数据
// 表格数据
for (WayBill wayBill : wayBills) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1); //创建一个新的列对象
dataRow.createCell(0).setCellValue(wayBill.getWayBillNum()); //创建单元格对象并设置值,wayBill为从数据库中查询到的运单对象
dataRow.createCell(1).setCellValue(wayBill.getSendName());
dataRow.createCell(2).setCellValue(wayBill.getSendMobile());
dataRow.createCell(3).setCellValue(wayBill.getSendAddress());
dataRow.createCell(4).setCellValue(wayBill.getRecName());
dataRow.createCell(5).setCellValue(wayBill.getRecMobile());
dataRow.createCell(6).setCellValue(wayBill.getRecAddress());
}
4)设置浏览器头信息
1)设置向浏览器响应的数据类型:
ServletActionContext.getResponse().setContentType("application/vnd.ms-excel");
2)设置文件名
String filename = "运单数据.xls";
String agent = ServletActionContext.getRequest().getHeader("user-agent"); //获取浏览器的类型
filename = FileUtils.encodeDownloadFilename(filename, agent); //采用FileUtils对文件名进行编码 //在资料中获取工具类
ServletActionContext.getResponse().setHeader("Content-Disposition", "attachment;filename=" + filename); //设置文件名
3)获取输出响应流并向浏览器写数据
ServletOutputStream outputStream = ServletActionContext.getResponse()
.getOutputStream();
hssfWorkbook.write(outputStream);
4)关闭hssfWorkbook
hssfWorkbook.close();
2.IText生成PDF
1)导入坐标:
itext ; itext-asian //itext-asian提供对中文的支持
2)设置头信息 //一样包括向浏览器响应的内容,文件名;操作同Poi生成excel
ServletActionContext.getResponse().setContentType("application/pdf");
String filename = "运单数据.pdf";
String agent = ServletActionContext.getRequest().getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
ServletActionContext.getResponse().setHeader("Content-Disposition","attachment;filename=" + filename);
3)创建文件对象
Document document = new Document();
PdfWriter.getInstance(document, ServletActionContext.getResponse().getOutputStream());
document.open();
4)创建表格,设置表格样式
Table table = new Table(7);
table.setWidth(80); // 宽度
table.setBorder(1); // 边框
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); // 水平对齐方式
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP); // 垂直对齐方式
5)设置字体
BaseFont cn = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H",false);
Font font = new Font(cn, 10, Font.NORMAL, Color.BLUE); // 设置表格字体
6)设置表头
table.addCell(buildCell("运单号", font));
table.addCell(buildCell("寄件人", font));
table.addCell(buildCell("寄件人电话", font));
table.addCell(buildCell("寄件人地址", font));
table.addCell(buildCell("收件人", font));
table.addCell(buildCell("收件人电话", font));
table.addCell(buildCell("收件人地址", font));
7)设置表格数据
for (WayBill wayBill : wayBills) {
table.addCell(buildCell(wayBill.getWayBillNum(), font));
table.addCell(buildCell(wayBill.getSendName(), font));
table.addCell(buildCell(wayBill.getSendMobile(), font));
table.addCell(buildCell(wayBill.getSendAddress(), font));
table.addCell(buildCell(wayBill.getRecName(), font));
table.addCell(buildCell(wayBill.getRecMobile(), font));
table.addCell(buildCell(wayBill.getRecAddress(), font));
}
8)将表格加入文档
document.add(table);
9)关闭文件对象
document.close(); //文件对象关闭时会自动向浏览器响应数据
3.jasperResport生成PDF报表
1)IReport图形化报表开发工具的下载安装
2)新建 JasperReport 模板文件 .jrxml
文件--new-blankA4--Open this Template--设置文件存储路径--下一步-完成
3)配置数据库连接 //可以通过此配置让ireport连接数据库获取数据
导入jar包:工具--选项--add jar --ojdbc.jar
report datasources 按钮--new--database JDBC connection--next--设置数据库类型,实例(orcale),用户名密码
4)点击工具栏中的report query打开窗口,可输入sql语句,直接查询到数据库数据
此时根据查询出来的数据会在fields中显示字段,可将字段直接拖到要显示的位置
5)常用报表组件:
static text :静态文本
text field :动态文本 //是一个字段的引用
6)对中文内容进行设置: //默认情况下不支持中文
工具--选项--add jar --导入jar:iTextAsian.jar
要想正确显示中文,需要设置三个地方:
选中要设置的文本框--在右侧属性中设置:
font name 为 新宋体 //设置显示字体
pdf font name is... 为 STSong-light //设置支持中文
pdf encoding 为 UniGB-UCS2-H (Chinese Simplified) //设置pdf编码
设置自动换行:点击要设置的文本--在右侧属性中设置: stretch type 为 relative to tallest object
4.在项目中根据模板生成pdf报表:
1)将waybill.jrxml复制到项目中
2)编写代码实现pdf报表的生成
// 从数据库中查询出要生成报表的数据
List<WayBill> wayBills = wayBillService.findWayBills(model);
// 下载导出
// 设置头信息
ServletActionContext.getResponse().setContentType("application/pdf");
String filename = "运单数据.pdf";
String agent = ServletActionContext.getRequest()
.getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
ServletActionContext.getResponse().setHeader("Content-Disposition",
"attachment;filename=" + filename);
// 根据 jasperReport模板 生成pdf
// 读取模板文件
String jrxml = ServletActionContext.getServletContext().getRealPath("/WEB-INF/jasper/waybill.jrxml"); //这里的路径为模板文件所在的路径
JasperReport report = JasperCompileManager.compileReport(jrxml);
// 设置模板数据
// Parameter变量
Map<String, Object> parameters = new HashMap<String, Object>();
parameters.put("company", "传智播客");
// Field变量
JasperPrint jasperPrint = JasperFillManager.fillReport(report,
parameters, new JRBeanCollectionDataSource(wayBills)); //这里第三个参数表示根据wayBills集合中的对象生成报表
// 生成PDF客户端
JRPdfExporter exporter = new JRPdfExporter();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM,
ServletActionContext.getResponse().getOutputStream()); //设置响应流
exporter.exportReport();// 导出
ServletActionContext.getResponse().getOutputStream().close();