@RequestMapping("doExport")
public void doExport(HttpServletResponse response,String username,String type){
try {
response.setContentType("application/octet-stream");
//response.setHeader("Content-disposition", "attachment;filename=user.xls");
//response.setHeader("Content-disposition", "attachment;filename=user.pdf");
response.setHeader("Content-disposition", "attachment;filename=user."+type);
response.flushBuffer();
//写出workbook
//workbook.write(response.getOutputStream());
OutputStream out=response.getOutputStream();
sysUserService.export(out,username,type);
//out.close();
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("导出文件失败....!");
}
}
/**
* 导出文档
*/
public void export(OutputStream out,String name,String type) {
List<SysUserResult> list = sysUserDao.findObjectsByName(name);
String[] headers = {"ID","用户名","部门","手机号","状态","创建时间","修改时间"};
if("xls".equals(type)){//导出xls格式
ExcelUtils.export(list, headers, out);
}else if("pdf".equals(type)) {//导出pdf格式
PDFUtils.export(list, headers, out);
}
}
package com.jt.common.util;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.List;
import com.itextpdf.text.Document;
import com.itextpdf.text.Font;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.jt.sys.vo.SysUserResult;
public class PDFUtils {
private PDFUtils(){}
public static void export(List<SysUserResult> list,String[] headers,OutputStream out){
//1.创建文档对象实例
Document document = new Document(PageSize.A4.rotate());
//2、建立书写器(Writer)与文档对象(document)关联,
//通过书写器将文档写入磁盘
try {
PdfWriter.getInstance(document,out);
//3、打开文档
document.open();
//4、中文处理
BaseFont bf = BaseFont.createFont("C:/Windows/Fonts/simhei.ttf",BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
Font font = new Font(bf,12,Font.NORMAL);
//5、向文档中添加内容
// 产生表格标题行
int columnNum=headers.length;
PdfPTable table = new PdfPTable(columnNum);
for(int i=0;i<columnNum;i++){
table.addCell(new Paragraph(headers[i],font));//设置表头
}
for (SysUserResult sys : list) {
table.addCell(sys.getId().toString());
table.addCell(sys.getUsername());
table.addCell(new Paragraph(sys.getSysDept().getName(),font));//中文处理
table.addCell(sys.getMobile());
table.addCell(new Paragraph(sys.getValid()==1?"启用":"禁用",font));
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
table.addCell(sdf.format(sys.getCreatedTime()));
table.addCell(sdf.format(sys.getModifiedTime()));
}
document.add(table);
//5、关闭文档
document.close();
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException("导出文件失败!");
}
}
}
package com.jt.common.util;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import com.jt.sys.vo.SysUserResult;
public class ExcelUtils {
private ExcelUtils(){}
public static void export(List<SysUserResult> list,String[] headers,OutputStream out){
//创建工作簿对象
HSSFWorkbook workbook = new HSSFWorkbook();
//设置列样式
HSSFCellStyle style = workbook.createCellStyle();
// 水平居中
style.setAlignment(HorizontalAlignment.CENTER);
// 垂直居中
style.setVerticalAlignment(VerticalAlignment.CENTER);
// 创建工作表
HSSFSheet sheet = workbook.createSheet("sheet1");
// 设置表格默认列宽度为15个字节
sheet.setDefaultColumnWidth((short) 18);
// 产生表格标题行(最顶端的行开始的第一行)
int columnNum=headers.length;
HSSFRow rowRowName = sheet.createRow(0);
// 将列头设置到sheet的单元格中
for (int n = 0; n < columnNum; n++) {
// 创建列头对应个数的单元格
HSSFCell cellRowName = rowRowName.createCell(n);
// 设置列头单元格的数据类型
HSSFRichTextString text = new HSSFRichTextString(headers[n]);
cellRowName.setCellValue(text); // 设置列头单元格的值
cellRowName.setCellStyle(style);// 设置列头单元格格式
}
//设置单元格数据
for(int i=0;i<list.size();i++){
HSSFRow Row = sheet.createRow(i+1);
Row.setRowStyle(style);
// 创建单元格
HSSFCell cell1 = Row.createCell(0);
HSSFCell cell2 = Row.createCell(1);
HSSFCell cell3 = Row.createCell(2);
HSSFCell cell4 = Row.createCell(3);
HSSFCell cell5 = Row.createCell(4);
HSSFCell cell6 = Row.createCell(5);
HSSFCell cell7 = Row.createCell(6);
// 设置单元格的数据类型
cell1.setCellValue(list.get(i).getId());
cell2.setCellValue(list.get(i).getUsername());
cell3.setCellValue(list.get(i).getSysDept().getName());
cell4.setCellValue(list.get(i).getMobile());
cell5.setCellValue(list.get(i).getValid()==1?"启用":"禁用");
cell6.setCellValue(list.get(i).getCreatedTime());
cell7.setCellValue(list.get(i).getModifiedTime());
cell1.setCellStyle(style);
cell2.setCellStyle(style);
cell3.setCellStyle(style);
cell4.setCellStyle(style);
cell5.setCellStyle(style);
cell6.setCellStyle(style);
cell7.setCellStyle(style);
HSSFCellStyle cellStyle = workbook.createCellStyle();
HSSFDataFormat format = workbook.createDataFormat();
cellStyle.setDataFormat(format.getFormat("yyyy年MM月dd日"));
// 水平居中
cellStyle.setAlignment(HorizontalAlignment.CENTER);
// 垂直居中
cellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
cell6.setCellStyle(cellStyle);
cell7.setCellStyle(cellStyle);
}
try {
workbook.write(out);
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException("导出文件失败!");
}
}
}
<!-- itext依赖 pdf实现 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.10</version>
</dependency>
<!-- itext中文处理 -->
<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itext-asian</artifactId>
<version>5.1.0</version>
</dependency>
<!-- excel apache poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.15</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.15</version>
</dependency>