一、存储单元格内容 行标 列标的实体类 PoiModel
package com.zsplat.qrcode.exportexcel.model;
/**
*
* @ClassName:PoiModel
* @Description:TODO(这里用一句话描述这个类的作用)
* @author: ZHOUPAN
* @date 2019年1月25日 下午3:10:30
*
* @Copyright: 2018 www.zsplat.com Inc. All rights reserved.
*/
public class PoiModel {
//内容
private String content;
//上一行同一位置内容
private String oldContent;
//行标
private int rowIndex;
//列标
private int cellIndex;
public String getOldContent() {
return oldContent;
}
public void setOldContent(String oldContent) {
this.oldContent = oldContent;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getRowIndex() {
return rowIndex;
}
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
public int getCellIndex() {
return cellIndex;
}
public void setCellIndex(int cellIndex) {
this.cellIndex = cellIndex;
}
}
二、导出Excel工具类
package com.zsplat.qrcode.commons.utils; import java.io.IOException; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFCellStyle; import org.apache.poi.hssf.usermodel.HSSFFont; import org.apache.poi.hssf.util.HSSFColor; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.ss.util.CellRangeAddress; import org.apache.poi.ss.util.CellUtil; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.google.common.collect.Lists; import com.zsplat.qrcode.commons.utils.lang.DateUtils; import com.zsplat.qrcode.exportexcel.model.PoiModel; /** * 导出 动态合并单元格 * @ClassName:ExportExcelByPoiUtil * @Description:TODO(这里用一句话描述这个类的作用) * @author: ZHOUPAN * @date 2019年1月25日 下午3:22:27 * * @Copyright: 2018 www.zsplat.com Inc. All rights reserved. */ public class ExportExcelByPoiUtil { /** * * @Title: createExcel * @Description: TODO(这里用一句话描述这个方法的作用) * @author: ZHOUPAN * @date: 2019年1月30日 下午4:37:01 * @param @param request * @param @param response * @param @param title 标题数组 * @param @param titleHead Excel标题 * @param @param widthAttr 单元格宽度 * @param @param maps 数据 * @param @param mergeIndex 要合并的列 数组 * @param @return 设定文件 * @return String 返回类型 * @throws */ @SuppressWarnings("rawtypes") public static String createExcel(HttpServletRequest request, HttpServletResponse response,String[] title,String titleHead ,int[] widthAttr,Map<String/*sheet名*/, List<Map<String/*对应title的值*/, String>>> maps, int[] mergeIndex){ if (title.length==0){ return null; } /*初始化excel模板*/ Workbook workbook = new XSSFWorkbook(); Sheet sheet = null; int n = 0; /*循环sheet页*/ for(Map.Entry<String, List<Map<String/*对应title的值*/, String>>> entry : maps.entrySet()){ /*实例化sheet对象并且设置sheet名称,book对象*/ try { sheet = workbook.createSheet(); workbook.setSheetName(n, entry.getKey()); workbook.setSelectedTab(0); }catch (Exception e){ e.printStackTrace(); } // 设置样式 头 cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT); // 水平方向的对齐方式 CellStyle cellStyle_head = style(0, workbook); // 导出时间 CellStyle cellStyle_export = style(3, workbook); // 标题 CellStyle cellStyle_title = style(1, workbook); // 正文 CellStyle cellStyle = style(2, workbook); // 合并单元格CellRangeAddress构造参数依次表示起始行,截至行,起始列, 截至列 CellRangeAddress c1 = new CellRangeAddress(0, 0, 0, title.length-1); sheet.addMergedRegion(c1); CellRangeAddress c2 = new CellRangeAddress(1, 1, 0, title.length-1); sheet.addMergedRegion(c2); // 在sheet里创建第一行,参数为行索引(excel的行),可以是0~65535之间的任何一个 Row row0 = sheet.createRow(0); // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 Cell cell1 = row0.createCell(0); // 设置单元格内容 标题 cell1.setCellValue("上海明华电力———" + titleHead + "一览表"); cell1.setCellStyle(cellStyle_head); // 设置合并单元格边框 setRegionStyle(sheet, c1, cellStyle_head); setRegionStyle(sheet, c2, cellStyle_export); // 设置列宽 for (int i = 0; i < widthAttr.length; i++) { sheet.setColumnWidth((short) i, (short) widthAttr[i] * 200); } // 在sheet里创建第二行 Row row1 = sheet.createRow(1); // 创建单元格(excel的单元格,参数为列索引,可以是0~255之间的任何一个 Cell cell2 = row1.createCell(0); // 设置单元格内容 标题 cell2.setCellValue("导出时间:" + DateUtils.getCurrentTime("yyyy-MM-dd HH: