package com.stylefeng.guns.modular.system.controller.util;
|
|
import org.apache.poi.hssf.usermodel.*;
|
import org.springframework.util.StringUtils;
|
|
import javax.imageio.ImageIO;
|
import javax.servlet.http.HttpServletResponse;
|
import java.awt.geom.AffineTransform;
|
import java.awt.image.AffineTransformOp;
|
import java.awt.image.BufferedImage;
|
import java.io.ByteArrayOutputStream;
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.util.List;
|
import java.util.Map;
|
|
public class ExcelUtil {
|
public static HSSFWorkbook getHSSFWorkbook(String sheetName, String[] title, String[][] values, HSSFWorkbook wb) {
|
// 第一步,创建一个webbook,对应一个Excel文件
|
if (wb == null) {
|
wb = new HSSFWorkbook();
|
}
|
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
|
HSSFSheet sheet = wb.createSheet(sheetName);
|
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
|
HSSFRow row = sheet.createRow(0);
|
// 第四步,创建单元格,并设置值表头 设置表头居中
|
HSSFCellStyle style = wb.createCellStyle();
|
style.setLocked(true);
|
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);
|
}
|
// 创建内容
|
for (int i = 0; i < values.length; i++) {
|
row = sheet.createRow(i + 1);
|
for (int j = 0; j < values[i].length; j++) {
|
cell = row.createCell(j);
|
cell.setCellValue(values[i][j]);
|
cell.setCellStyle(style);
|
}
|
}
|
|
return wb;
|
}
|
private static BufferedImage flipImage(BufferedImage originalImage, boolean horizontalFlip, boolean verticalFlip) {
|
AffineTransform tx = new AffineTransform();
|
if (horizontalFlip) {
|
tx.scale(-1.0, 1.0);
|
}
|
if (verticalFlip) {
|
tx.translate(originalImage.getWidth(null), 0);
|
tx.scale(1.0, -1.0);
|
} else if (horizontalFlip) {
|
tx.translate(-originalImage.getWidth(null), 0);
|
}
|
|
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
|
return op.filter(originalImage, null);
|
}
|
|
/**
|
* 设置相应头部信息
|
* @param response
|
* @param fileName
|
* @return
|
*/
|
public static 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();
|
}
|
}
|
}
|