您的位置:首页 > 编程语言 > Java开发

Java 导出Excel方法三

2016-03-07 15:07 561 查看
/**
* 功能描述:导出Excel方法三(多行表头导出)
*
*
* @param fileName
*            文件名
* @param list
*            数据
* @param keys
*            map key 值
* @param headers
*            Excel 列名
* @param title
*            标题栏 传空则不需要
* @param listhander
*            头部json hander:名称 size 跨行
* @param first
*            表头从第几行开始
*/
@SuppressWarnings("rawtypes")
public static void exportMapExcel(HttpServletResponse response,
String fileName, List<Map> list, String[] keys, String dateFormat,
String title, ArrayList<JSONObject> listhander, int begin, int end)
throws Exception {
int rowIndex = 0;
Workbook workbook = new HSSFWorkbook(); // 创建一个工作簿
Sheet sheet = workbook.createSheet(); // 创建一个Sheet页
sheet.autoSizeColumn((short) 0); // 单元格宽度自适应
CreationHelper helper = workbook.getCreationHelper();
CellStyle style = workbook.createCellStyle(); // 设置单元格样式
style.setDataFormat(helper.createDataFormat().getFormat(dateFormat)); // 格式化日期类型
style.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中
style.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 垂直居中
Row row = null;
if (EmptyUtils.isNotEmpty(title)) {
row = sheet.createRow(rowIndex++); // 创建第一行(标题)
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, keys.length - 1));
row.setHeight((short) 600);
// 设置字体
Font font = workbook.createFont();
font.setFontHeightInPoints((short) 16);
style.setFont(font);
row.createCell(0).setCellValue(title);
row.getCell(0).setCellStyle(style);
for (int i = 0; i < begin - 2; i++) {
row = sheet.createRow(rowIndex++);
}
}
else {
for (int i = 0; i < begin - 1; i++) {
row = sheet.createRow(rowIndex++);
}
}
CellStyle style1 = workbook.createCellStyle(); // 设置单元格样式
style1.setAlignment(CellStyle.ALIGN_CENTER); // 水平居中
style1.setVerticalAlignment(CellStyle.VERTICAL_CENTER); // 垂直居中
if (EmptyUtils.isNotEmpty(listhander)) {
for (int i = 0; i < (end - begin + 1); i++) {
row = sheet.createRow(rowIndex++); // 创建头部
for (JSONObject json1 : listhander) {
int rowBegin = StringUtil.StringToInt(json1
.getString("rowBegin"));
int colBegin = StringUtil.StringToInt((String) json1
.get("colBegin"));
int rowEnd = StringUtil.StringToInt(json1
.getString("rowEnd"));
int colEnd = StringUtil.StringToInt((String) json1
.get("colEnd"));
if (((rowBegin + 1) - begin) == i) {
sheet.addMergedRegion(new CellRangeAddress(rowBegin,
rowEnd, colBegin, colEnd));
row.createCell(colBegin).setCellValue(
(String) json1.get("hander"));
row.getCell(colBegin).setCellStyle(style1);
}
}
}
}
for (Map map : list) {
row = sheet.createRow(rowIndex++);
for (int i = 0; i < keys.length; i++) {
row.createCell(i).setCellValue(getValue(map.get(keys[i])));
row.getCell(i).setCellStyle(style1);
}
}
String ddate = new SimpleDateFormat("yyyyMMddhhmmss").format(Calendar
.getInstance().getTime());
response.setContentType("application/vnd.ms-excel;charset=UTF-8");
response.setHeader("Content-Disposition", "attachment; filename="
+ new String(fileName.getBytes("gb2312"), "iso8859-1") + "_"
+ ddate + ".xls");// 设定输出文件头
OutputStream output = response.getOutputStream();
workbook.write(output);
output.flush();
output.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: