java poi合并单元表格(求帮助啊)
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java poi合并单元表格(求帮助啊)相关的知识,希望对你有一定的参考价值。
java代码如何实现excel表格里相同的值合拼如图
参考技术A /***
* @param context
* @param dictionary
* @param rows 数据行
* @param fileName 文件名
* @param fields 列名
* @param fieldsName 字段名
*/
private void outExcelFile(HttpContext context,
IContextDictionary dictionary, DataRowCollections rows,
String fileName, List<String> fields, List<String> fieldsName)
int cellSize = fields.size();
if(cellSize == 0)
LogManager.debug("没有指定列头信息,无法导出Excel文件!");
return;
//============创建样式 start
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet();
//列头字体样式
HSSFFont headerFont = workbook.createFont();
headerFont.setFontName("宋体");
headerFont.setFontHeightInPoints((short) 12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//列头样式
HSSFCellStyle headerStyle = workbook.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
headerStyle.setLocked(true);
headerStyle.setWrapText(true);
//标题样式
HSSFFont titleFont = workbook.createFont();
titleFont.setFontName("宋体");
titleFont.setFontHeightInPoints((short) 15);
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyle titleStyle = workbook.createCellStyle();
titleStyle.setFont(titleFont);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleStyle.setLocked(true);
titleStyle.setWrapText(true);
//普通单元格字体样式
HSSFFont cellFont = workbook.createFont();
cellFont.setFontName("宋体");
cellFont.setFontHeightInPoints((short)12);
//普通单元格样式
HSSFCellStyle cellStyle = workbook.createCellStyle();
cellStyle.setFont(cellFont);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);// 左右居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
cellStyle.setLocked(true);
cellStyle.setWrapText(true);
//============创建样式 end
//设置序号列、列宽和标题行 start
HSSFRow titleRow = sheet.createRow(0);
titleRow.setHeightInPoints(50);
HSSFCell titleCell = titleRow.createCell(0);
titleCell.setCellValue(new HSSFRichTextString(fileName));
titleCell.setCellStyle(titleStyle);
//sheet.addMergedRegion(new Region(0,(short)0,0,(short)cellSize));//合并单元格--方法过时
//CellRangeAddress 起始行 结束行 起始列 结束列
sheet.addMergedRegion(new CellRangeAddress(0,0,(short)0,(short)cellSize));//合并单元格
//显示总的数据个数 start
HSSFRow countRow = sheet.createRow(1);
countRow.setHeightInPoints(40);
HSSFCell countCell = countRow.createCell(0);
countCell.setCellValue(new HSSFRichTextString("共计专项检查("+rows.size()+")项"));
countCell.setCellStyle(headerStyle);
sheet.addMergedRegion(new CellRangeAddress(1,1,(short)0,(short)cellSize));//合并单元格
//显示总的数据个数 end
HSSFRow headerRow = sheet.createRow(2);
headerRow.setHeightInPoints(35);
HSSFCell headerCell = null;
//序号
int startIndex = 0 ;
headerCell = headerRow.createCell(0);
sheet.setColumnWidth(0, 2000);
headerCell.setCellValue(new HSSFRichTextString("序号"));
headerCell.setCellStyle(headerStyle);
startIndex ++ ;
//列头
for(int i = 0; i < cellSize; i ++)
sheet.setColumnWidth(startIndex + i, 7000);
headerCell = headerRow.createCell(startIndex + i);
headerCell.setCellValue(new HSSFRichTextString(fields.get(i)));
headerCell.setCellStyle(headerStyle);
//设置序号列、列宽和标题行 end
//文件正文 start
int rowNum = 1;
int rowIndex = 0;
HSSFRow row = null;
List<Integer[]> cellRangeLst = new ArrayList<Integer[]>(0);
Integer [] arr = null;
int l = 0;
String orgName = "";
for(int j = 2; j<rows.size()+2; j++)//循环行
DataRow dataRow = rows.get(rowIndex); //对应数据库字段
HSSFCell cell = null;
row = sheet.createRow(j + 1);
row.setHeightInPoints(55);
//序号
cell = row.createCell(0);
cell.setCellValue(rowNum++);
cell.setCellStyle(cellStyle);
if(StringHelper.isNullOrEmpty(orgName))
arr = new Integer[2];
arr[0] = j + 1;
l =j + 1;
orgName = dataRow.getString("ORGNAME");
else
if(!orgName.equals(dataRow.getString("ORGNAME")))
arr[1] = j;
cellRangeLst.add(arr);
sheet.addMergedRegion(new CellRangeAddress(l,j,1,1));
arr = new Integer[2];
l = j+1;
orgName = dataRow.getString("ORGNAME");
if(rowIndex == rows.size() - 1)
arr[1] = j+1;
cellRangeLst.add(arr);
sheet.addMergedRegion(new CellRangeAddress(l,j+1,1,1));
for(int k = 0; k < cellSize; k++)
cell = row.createCell(k + startIndex);
String column = fieldsName.get(k); //对应数据库字段
String value = "";
if("APSJ".equals(column))
value = getAPSJValue(dataRow.getString(column));
else
value = dataRow.getString(column);
cell.setCellValue(new HSSFRichTextString(value));
cell.setCellStyle(cellStyle);
rowIndex ++;
//文件正文 end
//for(Integer[] te : cellRangeLst)
// sheet.addMergedRegion(new CellRangeAddress(te[0],te[1],1,1));//合并处室单元格
//
//下载
HttpServletResponse response = context.getResponse();
response.setContentType("application/x-download;charset=UTF-8");
String title = "export";
try
title = java.net.URLEncoder.encode(fileName, "UTF-8");
catch (UnsupportedEncodingException e)
e.printStackTrace();
response.addHeader("Content-Disposition", "attachment;filename=" + title + ".xls");
try
OutputStream out = response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
catch (IOException e)
e.printStackTrace();
//参考一下吧
追问谢谢你的帮助
参考技术B去年哥也写过这么个东东。
new Region 这个已经过时了,现在是用addMergedRegion(new CellRangeAddress(row,row,col,col);
追问呵呵,你的这个我喜欢,求源码。。。594689303
追答我这个是根据一颗树生成的excel。我是用递归将这些数据封装成树,然后再写入到excel的,这个可能跟你的不大一样。而且我这个是边写数据,边合并的。你的是写完了数据,然后再考虑合并。。现在合并的方法是这个addMergedRegion(new CellRangeAddress(row,row,col,col);
本回答被提问者采纳 参考技术C poi打印Excel中你可以用你定义的sheet里面的自带合并单元格方法。EG:sheet.addMergedRegion(new Region(0, (short) 0, 0, (short) 7));
sheet就是你自己定义的sheet啦,里面的参数是行,列,行,列。刚刚我写的那个就是合并第一行从
第0个单元格到底7个单元格的合并语句。
另外poi打印过程中系统是不是识别自动合并那几个单元格,需要你手动提前设置好。追问
谢谢你的回答
追答不客气
参考技术D 你是想用JAVA生成这样一个文档么?是的话留下邮箱,我写了一个工具类,发给你。这个不是一两句代码就搞定的。追问要啊!594689303
POI导出Excel表格(多行表头合并单元格)
Controller
@RequestMapping("/excel/export/common") public void exportExcelCommon(HttpServletRequestrequest, HttpServletResponse response )throws IOException{ List<TParams> list = new ArrayList<>(); List<TParams> params = paramService.getlist(); for (TParams tParams : params) { list.add(tParams); } HSSFWorkbook wb = paramService.exportCommon(list); response.setContentType("application/vnd.ms-excel"); response.addHeader("Content-Disposition", "attachment;filename="+ java.net.URLEncoder.encode("通用参数列表", "UTF-8")+".xls"); OutputStream outputStream = response.getOutputStream(); wb.write(outputStream); outputStream.flush(); outputStream.close(); }
ServiceImpl
@SuppressWarnings("deprecation") @Override public HSSFWorkbook export(List<TAQIDataReport> list) { // 声明String数组,并初始化元素(表头名称) //第一行表头字段,合并单元格时字段跨几列就将该字段重复几次 String[] excelHeader0 = { "城市名称", "监测点", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "污染物浓度及空气质量分指数(AQI)", "空气质量指数(AQI)", "首要污染物", "空气质量指数级别", "空气质量指数类别", "空气质量指数类别" }; // “0,2,0,0” ===> “起始行,截止行,起始列,截止列” String[] headnum0 = { "0,2,0,0", "0,2,1,1", "0,0,2,13", "0,2,14,14", "0,2,15,15", "0,2,16,16", "0,1,17,18" }; //第二行表头字段,其中的空的双引号是为了补全表格边框 String[] excelHeader1 = { "二氧化硫(SO?)24小时平均", "二氧化硫(SO?)24小时平均", "二氧化氮(NO?)24小时平均", "二氧化氮(NO?)24小时平均", "颗粒物(粒径小于等于10μm)24小时平均", "颗粒物(粒径小于等于10μm)24小时平均", "一氧化碳(CO)24小时平均", "一氧化碳(CO)24小时平均", "臭氧(O?)最大8小时平均", "臭氧(O?)最大8小时平均", "颗粒物(粒径小于等于2.5μm)24小时平均", "颗粒物(粒径小于等于2.5μm)24小时平均","","","","","" }; // 合并单元格 String[] headnum1 = { "1,1,2,3", "1,1,4,5", "1,1,6,7", "1,1,8,9", "1,1,10,11", "1,1,12,13" }; //第三行表头字段 String[] excelHeader2 = { "", "", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数", "浓度/(μg/m3)", "分指数","", "类别", "颜色" }; String[] headnum2 = { "2,2,2,2", "2,2,3,3", "2,2,4,4", "2,2,5,5", "2,2,6,6", "2,2,7,7", "2,2,8,8", "2,2,9,9", "2,2,10,10", "2,2,11,11", "2,2,12,12", "2,2,13,13", "2,2,17,17", "2,2,18,18" }; // 声明一个工作簿 HSSFWorkbook wb = new HSSFWorkbook(); // 生成一个表格 HSSFSheet sheet = wb.createSheet("TAQIDataReport"); // 生成一种样式 HSSFCellStyle style = wb.createCellStyle(); // 设置样式 style.setFillForegroundColor(HSSFColor.SKY_BLUE.index); style.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style.setBorderBottom(HSSFCellStyle.BORDER_THIN); style.setBorderLeft(HSSFCellStyle.BORDER_THIN); style.setBorderRight(HSSFCellStyle.BORDER_THIN); style.setBorderTop(HSSFCellStyle.BORDER_THIN); style.setAlignment(HSSFCellStyle.ALIGN_CENTER); style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成一种字体 HSSFFont font = wb.createFont(); // 设置字体 font.setFontName("微软雅黑"); // 设置字体大小 font.setFontHeightInPoints((short) 12); // 字体加粗 font.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 在样式中引用这种字体 style.setFont(font); // 生成并设置另一个样式 HSSFCellStyle style2 = wb.createCellStyle(); style2.setFillForegroundColor(HSSFColor.LIGHT_YELLOW.index); style2.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND); style2.setBorderBottom(HSSFCellStyle.BORDER_THIN); style2.setBorderLeft(HSSFCellStyle.BORDER_THIN); style2.setBorderRight(HSSFCellStyle.BORDER_THIN); style2.setBorderTop(HSSFCellStyle.BORDER_THIN); style2.setAlignment(HSSFCellStyle.ALIGN_CENTER); style2.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER); // 生成另一种字体2 HSSFFont font2 = wb.createFont(); // 设置字体 font2.setFontName("微软雅黑"); // 设置字体大小 font2.setFontHeightInPoints((short) 12); // 字体加粗 // font2.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD); // 在样式2中引用这种字体 style2.setFont(font2); // 生成表格的第一行 // 第一行表头 HSSFRow row = sheet.createRow(0); for (int i = 0; i < excelHeader0.length; i++) { sheet.autoSizeColumn(i, true);// 根据字段长度自动调整列的宽度 HSSFCell cell = row.createCell(i); cell.setCellValue(excelHeader0[i]); cell.setCellStyle(style); // System.out.println(excelHeader0[i]); if (i >= 0 && i <= 18) { for (int j = 0; j < excelHeader0.length; j++) { // 从第j列开始填充 cell = row.createCell(j); // 填充excelHeader1[j]第j个元素 cell.setCellValue(excelHeader0[j]); cell.setCellStyle(style); } } // 设置列宽 // sheet.setColumnWidth(0, 5500); // sheet.setColumnWidth(1, 6500); // sheet.setColumnWidth(2, 6500); // sheet.setColumnWidth(3, 6000); // sheet.setColumnWidth(4, 6500); // sheet.setColumnWidth(5, 6500); // sheet.setColumnWidth(6, 6500); // sheet.setColumnWidth(7, 6500); // sheet.setColumnWidth(8, 6500); // sheet.setColumnWidth(9, 6500); // sheet.setColumnWidth(10, 6500); // sheet.setColumnWidth(11, 6500); // sheet.setColumnWidth(12, 6500); // sheet.setColumnWidth(13, 6500); // sheet.setColumnWidth(14, 6500); // sheet.setColumnWidth(15, 6500); // sheet.setColumnWidth(16, 6500); // sheet.setColumnWidth(17, 6500); // sheet.setColumnWidth(18, 6500); // sheet.setDefaultRowHeight((short) 360);// 设置行高 } // 动态合并单元格 for (int i = 0; i < headnum0.length; i++) { sheet.autoSizeColumn(i, true); String[] temp = headnum0[i].split(","); Integer startrow = Integer.parseInt(temp[0]); Integer overrow = Integer.parseInt(temp[1]); Integer startcol = Integer.parseInt(temp[2]); Integer overcol = Integer.parseInt(temp[3]); sheet.addMergedRegion(new CellRangeAddress(startrow, overrow, startcol, overcol)); } // 第二行表头 row = sheet.createRow(1); for (int i = 0; i < excelHeader1.length; i++) { sheet.autoSizeColumn(i, true);// 自动调整宽度 HSSFCell cell = row.createCell(i + 1); cell.setCellValue(excelHeader1[i]); cell.setCellStyle(style); if (i >= 2 && i <= 18) { for (int j = 0; j < excelHeader1.length; j++) { // 从第j+1列开始填充 cell = row.createCell(j + 2); // 填充excelHeader1[j]第j个元素 cell.setCellValue(excelHeader1[j]); cell.setCellStyle(style); } } } // 动态合并单元格 for (int i = 0; i < headnum1.length; i++) { sheet.autoSizeColumn(i, true); String[] temp = headnum1[i].split(","); Integer startrow = Integer.parseInt(temp[0]); Integer overrow = Integer.parseInt(temp[1]); Integer startcol = Integer.parseInt(temp[2]); Integer overcol = Integer.parseInt(temp[3]); sheet.addMergedRegion(new CellRangeAddress(startrow, overrow, startcol, overcol)); } // 第三行表头 row = sheet.createRow(2); for (int i = 0; i < excelHeader2.length; i++) { HSSFCell cell = row.createCell(i + 2); cell.setCellValue(excelHeader2[i]); cell.setCellStyle(style); // System.out.println(excelHeader2[i]); sheet.autoSizeColumn(i, true);// 自动调整宽度 if (i > 1 && i <= 18) { for (int j = 0; j < excelHeader2.length; j++) { // 从第j+2列开始填充 cell = row.createCell(j); // 填充excelHeader1[j]第j个元素 cell.setCellValue(excelHeader2[j]); cell.setCellStyle(style); } } } // 动态合并单元格 for (int i = 0; i < headnum2.length; i++) { sheet.autoSizeColumn(i, true); String[] temp = headnum2[i].split(","); Integer startrow = Integer.parseInt(temp[0]); Integer overrow = Integer.parseInt(temp[1]); Integer startcol = Integer.parseInt(temp[2]); Integer overcol = Integer.parseInt(temp[3]); sheet.addMergedRegion(new CellRangeAddress(startrow, overrow, startcol, overcol)); } // 第四行数据 for (int i = 0; i < list.size(); i++) { row = sheet.createRow(i + 3); TAQIDataReport report = list.get(i); // 导入对应列的数据 HSSFCell cell = row.createCell(0); cell.setCellValue(report.getCity()); cell.setCellStyle(style2); HSSFCell cell1 = row.createCell(1); cell1.setCellValue(report.getAdd()); cell1.setCellStyle(style2); HSSFCell cell2 = row.createCell(2); cell2.setCellValue(report.getSo2Concentration()); cell2.setCellStyle(style2); HSSFCell cell3 = row.createCell(3); cell3.setCellValue(report.getSo2Subindex()); cell3.setCellStyle(style2); HSSFCell cell4 = row.createCell(4); cell4.setCellValue(report.getNo2Concentration()); cell4.setCellStyle(style2); HSSFCell cell5 = row.createCell(5); cell5.setCellValue(report.getNo2Subindex()); cell5.setCellStyle(style2); HSSFCell cell6 = row.createCell(6); cell6.setCellValue(report.getPm10Concentration()); cell6.setCellStyle(style2); HSSFCell cell7 = row.createCell(7); cell7.setCellValue(report.getPm10Subindex()); cell7.setCellStyle(style2); HSSFCell cell8 = row.createCell(8); cell8.setCellValue(report.getCoConcentration()); cell8.setCellStyle(style2); HSSFCell cell9 = row.createCell(9); cell9.setCellValue(report.getCoSubindex()); cell9.setCellStyle(style2); HSSFCell cell10 = row.createCell(10); cell10.setCellValue(report.getO3Concentration()); cell10.setCellStyle(style2); HSSFCell cell11 = row.createCell(11); cell11.setCellValue(report.getO3Subindex()); cell11.setCellStyle(style2); HSSFCell cell12 = row.createCell(12); cell12.setCellValue(report.getPm25Concentration()); cell12.setCellStyle(style2); HSSFCell cell13 = row.createCell(13); cell13.setCellValue(report.getPm25Subindex()); cell13.setCellStyle(style2); HSSFCell cell14 = row.createCell(14); cell14.setCellValue(report.getAirSubindex()); cell14.setCellStyle(style2); HSSFCell cell15 = row.createCell(15); cell15.setCellValue(report.getKeyPollution()); cell15.setCellStyle(style2); HSSFCell cell16 = row.createCell(16); cell16.setCellValue(report.getLevel()); cell16.setCellStyle(style2); HSSFCell cell17 = row.createCell(17); cell17.setCellValue(report.getType()); cell17.setCellStyle(style2); HSSFCell cell18 = row.createCell(18); cell18.setCellValue(report.getColor()); cell18.setCellStyle(style2); } return wb; }
以上是关于java poi合并单元表格(求帮助啊)的主要内容,如果未能解决你的问题,请参考以下文章