package com.stylefeng.guns.core.util; import org.apache.poi.hssf.usermodel.*; import javax.servlet.http.HttpServletResponse; import java.io.UnsupportedEncodingException; 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++) { // for (int k = 0; k < values[i][j].length; k++) { cell = row.createCell(j); cell.setCellValue(values[i][j]); cell.setCellStyle(style); // } } } return wb; } public static HSSFWorkbook getHSSFWorkbookTime(String sheetName, String[] title, String[][] values, HSSFWorkbook wb,String time) { // 第一步,创建一个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; cell = row.createCell(0); cell.setCellValue("时间:"+time); cell.setCellStyle(style); // 创建标题 row=sheet.createRow(1); 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 + 2); for (int j = 0; j < values[i].length; j++) { // for (int k = 0; k < values[i][j].length; k++) { cell = row.createCell(j); cell.setCellValue(values[i][j]); cell.setCellStyle(style); // } } } return wb; } /** * 设置相应头部信息 * @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(); } } }