以下是通过SSM框架实现的后台POI导出Excel和IText导出PDF,需要在项目中导入POI和IText相关jar包。另外,特别注意的是导出属于文件流,请求不能用ajax,可以用window.location.href='url'实现。
以下是后台代码实现:
package com.echarts.controller;
import java.awt.Color;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.echarts.entity.Phone;
import com.echarts.service.ChartService;
import com.echarts.utils.FileUtils;
import com.lowagie.text.BadElementException;
import com.lowagie.text.Cell;
import com.lowagie.text.Chunk;
import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.Element;
import com.lowagie.text.Font;
import com.lowagie.text.Paragraph;
import com.lowagie.text.Phrase;
import com.lowagie.text.Table;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.pdf.PdfWriter;
@Controller
public class ReportController {
@Autowired
private ChartService chartService;
/**
* 导出Excel
* @throws IOException
*/
@RequestMapping("/report_exportXls")
public void report_exportXls(HttpServletResponse response,HttpServletRequest request) throws IOException{
System.out.println("导出Excel...");
//获取需要的数据
List<Phone> list = chartService.showChart();
// 生成Excel文件
HSSFWorkbook hssfWorkbook = new HSSFWorkbook();
HSSFSheet sheet = hssfWorkbook.createSheet("手机销量数据");
// 表头
HSSFRow headRow = sheet.createRow(0);
headRow.createCell(0).setCellValue("品牌");
headRow.createCell(1).setCellValue("销量");
//数据表格
for (Phone phone : list) {
HSSFRow dataRow = sheet.createRow(sheet.getLastRowNum() + 1);
dataRow.createCell(0).setCellValue(phone.getBrand());
dataRow.createCell(1).setCellValue(phone.getSales());
}
// 下载导出
// 设置头信息
response.setContentType("application/vnd.ms-excel");
String filename = "手机销量数据.xls";
String agent = request.getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
ServletOutputStream outputStream = response.getOutputStream();
hssfWorkbook.write(outputStream);
// 关闭
outputStream.close();
}
/**
* 导出PDF
* @throws IOException
* @throws DocumentException
*/
@RequestMapping("/report_exportPdf")
public void report_exportPdf(HttpServletResponse response,HttpServletRequest request) throws IOException, DocumentException{
System.out.println("导出PDF...");
//获取需要的数据
List<Phone> list = chartService.showChart();
// 下载导出
// 设置头信息
response.setContentType("application/pdf");
String filename = "手机销量数据.pdf";
String agent = request.getHeader("user-agent");
filename = FileUtils.encodeDownloadFilename(filename, agent);
response.setHeader("Content-Disposition", "attachment;filename=" + filename);
// 生成PDF文件
Document document = new Document();
PdfWriter.getInstance(document, response.getOutputStream());
// 生成字体
BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
// 标题字体
Font f30 = new Font(bfChinese, 30, Font.NORMAL, Color.BLACK);
document.open();
// 写PDF数据
// 标题
Paragraph title = new Paragraph("手机销量信息", f30);
//设置位置
title.setAlignment(1);
title.setAlignment(Element.ALIGN_CENTER);
//添加标题
document.add(title);
// 换行
document.add(new Chunk("\n"));
//日期字体
Font f15 = new Font(bfChinese, 15, Font.NORMAL, Color.BLACK);
//当前日期
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
String newDate = format.format(new Date());
//生成日期
Paragraph date = new Paragraph("日期:"+newDate, f15);
//设置位置
date.setAlignment(1);
date.setAlignment(Element.ALIGN_RIGHT);
//添加日期
document.add(date);
// 换行
document.add(new Chunk("\n"));
// 向document 生成pdf表格
Table table = new Table(2);
table.setWidth(50); // 宽度
table.setBorder(1); // 边框
table.getDefaultCell().setHorizontalAlignment(Element.ALIGN_CENTER); // 水平对齐方式
table.getDefaultCell().setVerticalAlignment(Element.ALIGN_TOP); // 垂直对齐方式
// 设置表格字体
BaseFont cn = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", false);
Font font = new Font(cn, 20, Font.NORMAL, Color.BLACK);
// 写表头
table.addCell(buildCell("品牌", font));
table.addCell(buildCell("销量", font));
//写数据
for (Phone phone : list) {
table.addCell(buildCell(phone.getBrand(), font));
table.addCell(buildCell(Integer.toString(phone.getSales()), font));
}
// 将表格加入文档
document.add(table);
document.close();
}
private Cell buildCell(String content, Font font) throws BadElementException {
Phrase phrase = new Phrase(content, font);
return new Cell(phrase);
}
}