javapoi单元格合并单元格
发布时间: 2022-06-04 21:42:21
1. java poi合并单元表格(求帮助啊)
/**
*
*@paramcontext
*@paramdictionary
*@paramrows数据行
*@paramfileName文件名
*@paramfields列名
*@paramfieldsName字段名
*/
privatevoidoutExcelFile(HttpContextcontext,
IContextDictionarydictionary,DataRowCollectionsrows,
StringfileName,List<String>fields,List<String>fieldsName){
intcellSize=fields.size();
if(cellSize==0){
LogManager.debug("没有指定列头信息,无法导出Excel文件!");
return;
}
//============创建样式start
HSSFWorkbookworkbook=newHSSFWorkbook();
HSSFSheetsheet=workbook.createSheet();
//列头字体样式
HSSFFontheaderFont=workbook.createFont();
headerFont.setFontName("宋体");
headerFont.setFontHeightInPoints((short)12);
headerFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
//列头样式
HSSFCellStyleheaderStyle=workbook.createCellStyle();
headerStyle.setFont(headerFont);
headerStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);//左右居中
headerStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
headerStyle.setLocked(true);
headerStyle.setWrapText(true);
//标题样式
HSSFFonttitleFont=workbook.createFont();
titleFont.setFontName("宋体");
titleFont.setFontHeightInPoints((short)15);
titleFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
HSSFCellStyletitleStyle=workbook.createCellStyle();
titleStyle.setFont(titleFont);
titleStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titleStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);
titleStyle.setLocked(true);
titleStyle.setWrapText(true);
//普通单元格字体样式
HSSFFontcellFont=workbook.createFont();
cellFont.setFontName("宋体");
cellFont.setFontHeightInPoints((short)12);
//普通单元格样式
HSSFCellStylecellStyle=workbook.createCellStyle();
cellStyle.setFont(cellFont);
cellStyle.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左右居中
cellStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);//上下居中
cellStyle.setLocked(true);
cellStyle.setWrapText(true);
//============创建样式end
//设置序号列、列宽和标题行start
HSSFRowtitleRow=sheet.createRow(0);
titleRow.setHeightInPoints(50);
HSSFCelltitleCell=titleRow.createCell(0);
titleCell.setCellValue(newHSSFRichTextString(fileName));
titleCell.setCellStyle(titleStyle);
//sheet.addMergedRegion(newRegion(0,(short)0,0,(short)cellSize));//合并单元格--方法过时
//CellRangeAddress起始行结束行起始列结束列
sheet.addMergedRegion(newCellRangeAddress(0,0,(short)0,(short)cellSize));//合并单元格
//显示总的数据个数start
HSSFRowcountRow=sheet.createRow(1);
countRow.setHeightInPoints(40);
HSSFCellcountCell=countRow.createCell(0);
countCell.setCellValue(newHSSFRichTextString("共计专项检查("+rows.size()+")项"));
countCell.setCellStyle(headerStyle);
sheet.addMergedRegion(newCellRangeAddress(1,1,(short)0,(short)cellSize));//合并单元格
//显示总的数据个数end
HSSFRowheaderRow=sheet.createRow(2);
headerRow.setHeightInPoints(35);
HSSFCellheaderCell=null;
//序号
intstartIndex=0;
headerCell=headerRow.createCell(0);
sheet.setColumnWidth(0,2000);
headerCell.setCellValue(newHSSFRichTextString("序号"));
headerCell.setCellStyle(headerStyle);
startIndex++;
//列头
for(inti=0;i<cellSize;i++){
sheet.setColumnWidth(startIndex+i,7000);
headerCell=headerRow.createCell(startIndex+i);
headerCell.setCellValue(newHSSFRichTextString(fields.get(i)));
headerCell.setCellStyle(headerStyle);
}
//设置序号列、列宽和标题行end
//文件正文start
introwNum=1;
introwIndex=0;
HSSFRowrow=null;
List<Integer[]>cellRangeLst=newArrayList<Integer[]>(0);
Integer[]arr=null;
intl=0;
StringorgName="";
for(intj=2;j<rows.size()+2;j++){//循环行
DataRowdataRow=rows.get(rowIndex);//对应数据库字段
HSSFCellcell=null;
row=sheet.createRow(j+1);
row.setHeightInPoints(55);
//序号
cell=row.createCell(0);
cell.setCellValue(rowNum++);
cell.setCellStyle(cellStyle);
if(StringHelper.isNullOrEmpty(orgName)){
arr=newInteger[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(newCellRangeAddress(l,j,1,1));
arr=newInteger[2];
l=j+1;
orgName=dataRow.getString("ORGNAME");
}
if(rowIndex==rows.size()-1){
arr[1]=j+1;
cellRangeLst.add(arr);
sheet.addMergedRegion(newCellRangeAddress(l,j+1,1,1));
}
}
for(intk=0;k<cellSize;k++){
cell=row.createCell(k+startIndex);
Stringcolumn=fieldsName.get(k);//对应数据库字段
Stringvalue="";
if("APSJ".equals(column)){
value=getAPSJValue(dataRow.getString(column));
}else{
value=dataRow.getString(column);
}
cell.setCellValue(newHSSFRichTextString(value));
cell.setCellStyle(cellStyle);
}
rowIndex++;
}
//文件正文end
//for(Integer[]te:cellRangeLst){
//sheet.addMergedRegion(newCellRangeAddress(te[0],te[1],1,1));//合并处室单元格
//}
//下载
HttpServletResponseresponse=context.getResponse();
response.setContentType("application/x-download;charset=UTF-8");
Stringtitle="export";
try{
title=java.net.URLEncoder.encode(fileName,"UTF-8");
}catch(UnsupportedEncodingExceptione){
e.printStackTrace();
}
response.addHeader("Content-Disposition","attachment;filename="+title+".xls");
try{
OutputStreamout=response.getOutputStream();
workbook.write(out);
out.flush();
out.close();
}catch(IOExceptione){
e.printStackTrace();
}
}
//参考一下吧
2. poi excel 怎么合并单元格
3. java poi xwpf操作word生成一个表格怎么合并单元格,求大神指导!
有个最简单,不用使用任何 poi 或其他第三方类库的方法生成 excel ,或 word ,
你把一个生成好的 word 例子用文件另存为 xml , docx之类,然后你再直接用记事本去打开这个 docx ,你就会发现是 xml 格式,
这个时候,你在程序里面就直接用普通代码生成这个 xml 就可以了。
4. 有关java通过poi处理excle中合并单元格的问题
HSSFSheet.getNumMergedRegions取合并格个数,HSSFSheet.getMergedRegionAt取第几个合并格的合并区域
循环几下就可取到当前格的合并行数
5. java excel 怎么合并大的单元格
建议使用库来实现,spire.xls for java的单元格合并教程文章你可以参考看看
6. java poi怎么读取Excel中合并单元格的值
获取合并单元格的值 @param sheet @param row @param column @return。
7. poi Java生成excel合并单元格后字体居中
我想是合并以后再做以下处理吧。
取得现有式样。
调用setAlignment,重新设置居中:
CellStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
再把式样设置到cell中:
HSSFCell.setCellStyle(CellStyle);
热点内容