//前端不要ajax,post请求
@RequestMapping(value = "/export")
@ResponseBody
public void export(HttpServletResponse response) {
//获取数据
List<UserEntity> list = userRepository.findAll();
//excel标题
String[] title = {"编码", "昵称", "名", "姓", "密码"};
//excel文件名
String fileName = "users" + ".xls";
//sheet名
String sheetName = "users";
String[][] content;
content = new String[list.size() + 1][5];
for (int i = 0; i < list.size(); i++) {
UserEntity obj = list.get(i);
// content[i][0] = String.valueOf(obj.getId());
content[i][0] = String.valueOf(obj.getId());
content[i][1] = obj.getNickname();
content[i][2] = obj.getFirstName();
content[i][3] = obj.getLastName();
content[i][4] = obj.getPassword();
}
//创建HSSFWorkbook
HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);
//响应到客户端
try {
this.setResponseHeader(response, fileName);
OutputStream os = response.getOutputStream();
//OutputStream os = new FileOutputStream("E:/test/excel/1.xls");
wb.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
//发送响应流方法
public void setResponseHeader(HttpServletResponse response, String fileName) {
try {
try {
fileName = new String(fileName.getBytes(), "ISO8859-1");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
response.setContentType("application/octet-stream;charset=ISO8859-1");
//统一编码,解决乱码问题
response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
response.addHeader("Pargam", "no-cache");
response.addHeader("Cache-Control", "no-cache");
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class ExcelUtil {
/**
* 导出Excel
* @param sheetName sheet名称
* @param title 标题
* @param values 内容
* @param wb HSSFWorkbook对象
* @return
*/
public static HSSFWorkbook getHSSFWorkbook(String sheetName,String []title,String [][]values, HSSFWorkbook wb){
// 第一步,创建一个HSSFWorkbook,对应一个Excel文件
if(wb == null){
wb = new HSSFWorkbook();
}
// 第二步,在workbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = wb.createSheet(sheetName);
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制
HSSFRow row = sheet.createRow(0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = wb.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); // 创建一个居中格式
//声明列对象
HSSFCell cell = null;
//创建标题
for(int i=0;i<title.length;i++){
cell = row.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
sheet.setColumnWidth(0,4000);
sheet.setColumnWidth(1,2200);
//创建内容
for(int i=0;i<values.length;i++){
row = sheet.createRow(i + 1);
for(int j=0;j<values[i].length;j++){
//将内容按顺序赋给对应的列对象
row.createCell(j).setCellValue(values[i][j]);
}
}
return wb;
}
}
导出图片
BASE64Decoder decoder = new BASE64Decoder();
byte[] b = decoder.decodeBuffer("");//转码得到图片数据
ByteArrayInputStream bais = new ByteArrayInputStream(b);
BufferedImage bufferImg = ImageIO.read(bais);
File w2 = new File("E://test/imgs/test.png");
ImageIO.write(bufferImg, "png", w2);
// BufferedImage bufferImg = null;
//
// bufferImg = ImageIO.read(new File("E:/test/1.jpg"));
ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
//这里要注意formatName要缓存后缀名
ImageIO.write(bufferImg, "jpeg", byteArrayOut);
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
//anchor主要用于设置图片的属性
HSSFClientAnchor anchor =
new HSSFClientAnchor
( 0, //x缩放
0, // y缩放
1023, //最大1023
255, //最大255
(short) 3, //于下下个参数进行定位 0开始
5, //在第几行
(short) 4, //宽度占几格 0开始
6 //占几列
);
// anchor.setAnchorType(3);
//插入图片
patriarch.createPicture(anchor, wb.addPicture(byteArrayOut.toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
提示下载文件;最后解决办法:使用$("#pdfRightlist").load("${path}readFileToHtml/htmlPreview.do",{"id":id,"fileType":type});