forked from integrated_whb/integrated_whb
170 lines
5.5 KiB
Java
170 lines
5.5 KiB
Java
|
package com.zcloud.util;
|
|||
|
|
|||
|
import com.zcloud.entity.PageData;
|
|||
|
import org.apache.poi.hssf.usermodel.*;
|
|||
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|||
|
import org.apache.poi.ss.usermodel.VerticalAlignment;
|
|||
|
import org.apache.poi.ss.usermodel.Workbook;
|
|||
|
import org.springframework.web.servlet.view.document.AbstractXlsView;
|
|||
|
|
|||
|
import javax.servlet.http.HttpServletRequest;
|
|||
|
import javax.servlet.http.HttpServletResponse;
|
|||
|
import java.util.Date;
|
|||
|
import java.util.List;
|
|||
|
import java.util.Map;
|
|||
|
|
|||
|
/**
|
|||
|
* 说明:导出到EXCEL(图片压缩)
|
|||
|
* 作者:luoxiaobao
|
|||
|
* 官网:www.qdkjchina.com
|
|||
|
*/
|
|||
|
public class HiddenExcelCompressImg extends AbstractXlsView{
|
|||
|
|
|||
|
@Override
|
|||
|
protected void buildExcelDocument(Map<String, Object> model,
|
|||
|
Workbook workbook, HttpServletRequest request,
|
|||
|
HttpServletResponse response) throws Exception {
|
|||
|
// TODO Auto-generated method stub
|
|||
|
// String proPath = PathUtil.getProjectpath();
|
|||
|
String beji = Const.HTTPFILEURL;
|
|||
|
Date date = new Date();
|
|||
|
String filename = DateUtil.date2Str(date, "yyyyMMddHHmmss");
|
|||
|
response.setContentType("application/octet-stream");
|
|||
|
response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls");
|
|||
|
|
|||
|
HSSFSheet sheet;
|
|||
|
HSSFWorkbook book = (HSSFWorkbook) workbook;
|
|||
|
sheet = book.createSheet("sheet1");
|
|||
|
|
|||
|
List<String> titles = (List<String>) model.get("titles");
|
|||
|
int len = titles.size();
|
|||
|
HSSFCellStyle headerStyle = book.createCellStyle(); //标题样式
|
|||
|
headerStyle.setAlignment(HorizontalAlignment.CENTER);
|
|||
|
headerStyle.setVerticalAlignment(VerticalAlignment.CENTER);
|
|||
|
HSSFFont headerFont = book.createFont(); //标题字体
|
|||
|
headerFont.setBold(true);
|
|||
|
headerFont.setFontHeightInPoints((short)11);
|
|||
|
headerStyle.setFont(headerFont);
|
|||
|
short height=25*20;
|
|||
|
HSSFRow row = sheet.createRow(0);
|
|||
|
if(len>250) {
|
|||
|
len = 250;
|
|||
|
}
|
|||
|
for(int i=0; i<len; i++){ //设置标题
|
|||
|
String title = titles.get(i);
|
|||
|
row.setRowStyle(headerStyle);
|
|||
|
if(title.length()>250) {
|
|||
|
row.createCell(i).setCellValue(title.substring(0, 250));
|
|||
|
}else {
|
|||
|
row.createCell(i).setCellValue(title);
|
|||
|
}
|
|||
|
}
|
|||
|
sheet.getRow(0).setHeight(height);
|
|||
|
|
|||
|
HSSFCellStyle contentStyle = book.createCellStyle(); //内容样式
|
|||
|
HSSFPatriarch patriarch = sheet.createDrawingPatriarch();
|
|||
|
|
|||
|
contentStyle.setAlignment(HorizontalAlignment.CENTER);
|
|||
|
List<PageData> varList = (List<PageData>) model.get("varList");
|
|||
|
|
|||
|
|
|||
|
int varCount = varList.size();
|
|||
|
for(int i=0; i<varCount; i++){
|
|||
|
PageData vpd = varList.get(i);
|
|||
|
HSSFRow rows = sheet.createRow(i+1);
|
|||
|
for(int j=0;j<len;j++){
|
|||
|
String valKey = "var"+(j+1);
|
|||
|
String imgKey = "img" +(j+1);
|
|||
|
String varstr = "";
|
|||
|
if(vpd.containsKey(valKey)) {
|
|||
|
varstr = vpd.getString(valKey) != null ? vpd.getString("var"+(j+1)) : "";
|
|||
|
}
|
|||
|
if(vpd.containsKey(imgKey)) {
|
|||
|
String img = vpd.getString(imgKey) != null ? vpd.getString("img" + (j + 1)) : "";
|
|||
|
if(Tools.notEmpty(img)){
|
|||
|
varstr = beji + img;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
rows.setRowStyle(contentStyle);
|
|||
|
rows.createCell(j).setCellValue(varstr);
|
|||
|
// if(vpd.containsKey(imgKey)) {
|
|||
|
// String varstr = vpd.getString(imgKey) != null ? vpd.getString("img"+(j+1)) : "";
|
|||
|
// if(Tools.isEmpty(varstr)) {
|
|||
|
// continue;
|
|||
|
// }
|
|||
|
// URL url = new URL(beji+varstr);
|
|||
|
// BufferedImage bufferImg = null;
|
|||
|
// try {
|
|||
|
// bufferImg = Thumbnails.of(url).scale(0.3).asBufferedImage();
|
|||
|
// } catch (Exception e) {
|
|||
|
// e.printStackTrace();
|
|||
|
// }
|
|||
|
// ByteArrayOutputStream byteArrayOut = new ByteArrayOutputStream();
|
|||
|
// if(bufferImg != null) {
|
|||
|
// ImageIO.write(bufferImg, "jpg", byteArrayOut);
|
|||
|
// HSSFClientAnchor anchor1 = new HSSFClientAnchor(0, 0, 0, 0,(short) (j), (i+1), (short) (j+1), (i+2));
|
|||
|
// patriarch.createPicture(anchor1, book.addPicture(byteArrayOut
|
|||
|
// .toByteArray(), HSSFWorkbook.PICTURE_TYPE_JPEG));
|
|||
|
// }
|
|||
|
//
|
|||
|
// }
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
// // 用户
|
|||
|
// String filename = DateUtil.date2Str(date, "yyyyMMddHHmmss");
|
|||
|
// String filePath = PathUtil.getProjectpath() + Const.FILEPATHFILE; //文件上传路径
|
|||
|
//
|
|||
|
// FileOutputStream fileOutputStream = null;
|
|||
|
// System.out.println(filePath);
|
|||
|
// try {
|
|||
|
// fileOutputStream = new FileOutputStream(filePath + filename + ".xls");
|
|||
|
// book.write(fileOutputStream);
|
|||
|
// fileOutputStream.flush();
|
|||
|
// } catch (Exception e) {
|
|||
|
// e.printStackTrace();
|
|||
|
// } finally {
|
|||
|
// if(fileOutputStream != null){
|
|||
|
// try {
|
|||
|
// fileOutputStream.close();
|
|||
|
// } catch (IOException e) {
|
|||
|
// e.printStackTrace();
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
// File excel = new File(filePath + filename + ".xls");
|
|||
|
// FileInputStream in = new FileInputStream(excel);
|
|||
|
//
|
|||
|
// ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
|||
|
// ZipOutputStream zos = new ZipOutputStream(bos);
|
|||
|
// try {
|
|||
|
//
|
|||
|
// zos.putNextEntry(new ZipEntry(filename+".xls"));
|
|||
|
// byte[] buf = new byte[1024];
|
|||
|
// int length;
|
|||
|
// while ((length = in.read(buf)) > 0) {
|
|||
|
// zos.write(buf, 0, length);
|
|||
|
// }
|
|||
|
// zos.closeEntry();
|
|||
|
// } catch (Exception e) {
|
|||
|
// e.printStackTrace();
|
|||
|
// } finally {
|
|||
|
//
|
|||
|
// in.close();
|
|||
|
// zos.close();
|
|||
|
// }
|
|||
|
// excel.delete();
|
|||
|
// response.setHeader("Content-Disposition", "attachment; filename=\"" + filename+".zip" + "\"");
|
|||
|
// response.setContentType("application/octet-stream;charset=UTF-8");
|
|||
|
// OutputStream os = response.getOutputStream();
|
|||
|
// os.write(bos.toByteArray());
|
|||
|
// os.close();
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|