poi导出合并单元格工具类
时间: 2023-12-19 09:05:41 浏览: 32
以下是使用POI导出合并单元格的工具类示例代码:
```java
public class ExcelUtil {
/**
* 导出Excel
* @param response HttpServletResponse
* @param fileName 文件名
* @param title 标题
* @param headers 表头
* @param dataset 数据集合
* @throws IOException
*/
public static void exportExcel(HttpServletResponse response, String fileName, String title, String[] headers, Collection<?> dataset) throws IOException {
OutputStream out = null;
try {
// 创建工作簿
HSSFWorkbook workbook = new HSSFWorkbook();
// 创建工作表
HSSFSheet sheet = workbook.createSheet(title);
// 设置表格默认列宽度为15个字符
sheet.setDefaultColumnWidth(15);
// 创建标题行
HSSFRow row = sheet.createRow(0);
HSSFCell cellTitle = row.createCell(0);
// 合并单元格
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, headers.length - 1));
// 设置标题样式
HSSFCellStyle titleStyle = getTitleStyle(workbook);
cellTitle.setCellStyle(titleStyle);
cellTitle.setCellValue(title);
// 创建表头行
row = sheet.createRow(1);
// 设置表头样式
HSSFCellStyle headerStyle = getHeaderStyle(workbook);
for (int i = 0; i < headers.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellStyle(headerStyle);
cell.setCellValue(headers[i]);
}
// 创建数据行
Iterator<?> it = dataset.iterator();
int index = 2;
while (it.hasNext()) {
row = sheet.createRow(index);
Object t = it.next();
Field[] fields = t.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++) {
HSSFCell cell = row.createCell(i);
cell.setCellValue(getFieldValueByName(fields[i].getName(), t));
}
index++;
}
// 输出Excel文件
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
out = response.getOutputStream();
workbook.write(out);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (out != null) {
out.close();
}
}
}
/**
* 根据属性名获取属性值
* @param fieldName 属性名
* @param o 对象
* @return 属性值
*/
private static Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e) {
return null;
}
}
/**
* 获取标题样式
* @param workbook 工作簿
* @return 标题样式
*/
private static HSSFCellStyle getTitleStyle(HSSFWorkbook workbook) {
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
HSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 16);
font.setBold(true);
style.setFont(font);
return style;
}
/**
* 获取表头样式
* @param workbook 工作簿
* @return 表头样式
*/
private static HSSFCellStyle getHeaderStyle(HSSFWorkbook workbook) {
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HorizontalAlignment.CENTER);
style.setVerticalAlignment(VerticalAlignment.CENTER);
style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.getIndex());
style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
HSSFFont font = workbook.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 12);
font.setBold(true);
style.setFont(font);
return style;
}
}
```