package com.zcloud.util; import java.awt.image.BufferedImage; import java.io.*; import java.net.HttpURLConnection; import java.net.URL; import java.util.Date; import java.util.List; import java.util.Map; import java.util.logging.Logger; import javax.imageio.ImageIO; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.fasterxml.jackson.databind.introspect.AnnotationMap; import net.coobird.thumbnailator.Thumbnails; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFClientAnchor; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.usermodel.HSSFPatriarch; 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 org.apache.poi.ss.usermodel.Workbook; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.servlet.view.document.AbstractXlsView; import com.zcloud.entity.PageData; /** * 说明:导出到EXCEL * 作者:luoxiaobao * 官网:www.qdkjchina.com */ public class HiddenExcelImgView extends AbstractXlsView{ private String fileUrl = "http://192.168.192.201:8991/file/"; @Override protected void buildExcelDocument(Map model, Workbook workbook, HttpServletRequest request, HttpServletResponse response) throws Exception { // TODO Auto-generated method stub String proPath = PathUtil.getProjectpath(); String beji = fileUrl; //beji =beji.replaceAll("\\\\", "/"); Date date = new Date(); String filename = DateUtil.date2Str(date, "yyyyMMddHHmmss"); HSSFSheet sheet; response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename="+filename+".xls"); HSSFWorkbook book = (HSSFWorkbook) workbook; sheet = book.createSheet("sheet1"); List titles = (List) 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; i250) { 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 varList = (List) model.get("varList"); int varCount = varList.size(); for(int i=0; i desFileSize * ONE_ZERO_TWO_FOUR) { ByteArrayInputStream inputStream = new ByteArrayInputStream(imageBytes); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(imageBytes.length); Thumbnails.of(inputStream) .scale(accuracy) .outputQuality(accuracy) .toOutputStream(outputStream); imageBytes = outputStream.toByteArray(); } System.out.println("图片原大小={}kb | 压缩后大小={}kb"); // logger.info("图片原大小={}kb | 压缩后大小={}kb", // srcSize / ONE_ZERO_TWO_FOUR, imageBytes.length / ONE_ZERO_TWO_FOUR); } catch (Exception e) { // logger.error("【图片压缩】msg=图片压缩失败!", e); } return imageBytes; } }