package com.stylefeng.guns.modular.system.util;
|
|
|
import org.apache.poi.hssf.usermodel.*;
|
import org.apache.poi.ss.usermodel.CellType;
|
import org.apache.poi.ss.usermodel.HorizontalAlignment;
|
|
import javax.servlet.http.HttpServletResponse;
|
import java.net.URLEncoder;
|
|
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.setAlignment(HorizontalAlignment.forInt((short) 2));
|
HSSFCell cell = null;
|
// 创建标题
|
for (int i = 0; i < title.length; i++) {
|
cell = row.createCell(i);
|
cell.setCellType(CellType.forInt(1));
|
cell.setCellValue(title[i]);
|
cell.setCellStyle(style);
|
}
|
if(values!=null){
|
// 创建内容
|
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;
|
}
|
|
public static void setResponseHeader(HttpServletResponse response, String fileName) {
|
try {
|
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName));
|
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
|
response.addHeader("Pragma", "no-cache");
|
response.addHeader("Cache-Control", "no-cache");
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
}
|
}
|
|
}
|