首页 技术 正文
技术 2022年11月14日
0 收藏 775 点赞 2,442 浏览 5167 个字

在Excel导出过程中,若遇到合并单元格样式只有第一行合并,而下面要合并的行没有边框显示。

一般问题出在将单元格样式设置与合并单元格放在同一个循环中导致。

以下为一个完整版的demo以供参考

定义边框样式方法:

 package com.ccb.excel.export; import java.io.Serializable;
import java.net.URLEncoder; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.HSSFColor; public class ExportFileNameUtils implements Serializable {
public static HSSFCellStyle initColumnHeadStyle(HSSFWorkbook wb) {
HSSFCellStyle columnHeadStyle = wb.createCellStyle();
HSSFFont columnHeadFont = wb.createFont();
columnHeadFont.setFontName("宋体");
columnHeadFont.setFontHeightInPoints((short) 10);
columnHeadFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);
columnHeadStyle.setFont(columnHeadFont);
columnHeadStyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
columnHeadStyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
columnHeadStyle.setLocked(true);
columnHeadStyle.setWrapText(true);
columnHeadStyle.setLeftBorderColor(HSSFColor.BLACK.index);// 左边框的颜色
columnHeadStyle.setBorderLeft((short) 1);// 边框的大小
columnHeadStyle.setRightBorderColor(HSSFColor.BLACK.index);// 右边框的颜色
columnHeadStyle.setBorderRight((short) 1);// 边框的大小
columnHeadStyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
columnHeadStyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色
// 设置单元格的背景颜色(单元格的样式会覆盖列或行的样式)
columnHeadStyle.setFillForegroundColor(HSSFColor.WHITE.index);
return columnHeadStyle;
} public static HSSFCellStyle initColumnCenterstyle(HSSFWorkbook wb) {
HSSFFont font = wb.createFont();
font.setFontName("宋体");
font.setFontHeightInPoints((short) 10);
HSSFCellStyle centerstyle = wb.createCellStyle();
centerstyle.setFont(font);
centerstyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 左右居中
centerstyle.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 上下居中
centerstyle.setWrapText(true);
centerstyle.setLeftBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderLeft((short) 1);
centerstyle.setRightBorderColor(HSSFColor.BLACK.index);
centerstyle.setBorderRight((short) 1);
centerstyle.setBorderBottom(HSSFCellStyle.BORDER_THIN); // 设置单元格的边框为粗体
centerstyle.setBottomBorderColor(HSSFColor.BLACK.index); // 设置单元格的边框颜色.
centerstyle.setFillForegroundColor(HSSFColor.WHITE.index);// 设置单元格的背景颜色.
return centerstyle;
}
}

调用过程中步骤应为:

1、初始化带边框的表头样式

2、填充单元格数据

3、合并单元格

 package com.ccb.excel.export; import java.io.FileOutputStream; import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress; public class ExcelExport {
public static void main(String[] args) throws Exception {
HSSFWorkbook wb = new HSSFWorkbook();// 创建一个Excel文件
HSSFSheet sheet = wb.createSheet("银行存余额表(1)");// 创建一个Excel的Sheet
// 定义样式
HSSFCellStyle cellStyleCenter = ExportFileNameUtils.initColumnHeadStyle(wb);//表头样工
HSSFCellStyle cellStyleRight = ExportFileNameUtils.initColumnCenterstyle(wb);//单元格样式
HSSFCellStyle cellStyleLeft = ExportFileNameUtils.initColumnCenterstyle(wb);
cellStyleRight.setAlignment(HSSFCellStyle.ALIGN_RIGHT);//右对齐
cellStyleLeft.setAlignment(HSSFCellStyle.ALIGN_LEFT);//左对齐
// 设置列宽
sheet.setColumnWidth(0, 7200);
sheet.setColumnWidth(1, 5000);
sheet.setColumnWidth(2, 5000);
sheet.setColumnWidth(3, 5000);
sheet.setColumnWidth(4, 5000);
sheet.setColumnWidth(5, 5000);
try {
HSSFRow row = null;
HSSFCell cell = null;
// ---------------------------1.初始化带边框的表头------------------------------
for (int i = 0; i < 5; i++) {
row = sheet.createRow(i);
for (int j = 0; j <= 5; j++) {
cell = row.createCell(j);
cell.setCellStyle(cellStyleCenter);
}
}
// ---------------------------2.填充单元格数据------------------------------
cell = sheet.getRow(0).getCell(0);
cell.setCellValue(new HSSFRichTextString("银行存余额表"));
cell = sheet.getRow(1).getCell(0);
cell.setCellValue(new HSSFRichTextString("2018-01-31"));
cell = sheet.getRow(2).getCell(0);
cell.setCellValue(new HSSFRichTextString("开户行"));
cell = sheet.getRow(2).getCell(1);
cell.setCellValue(new HSSFRichTextString("活期"));
cell = sheet.getRow(2).getCell(3);
cell.setCellValue(new HSSFRichTextString("定期"));
cell = sheet.getRow(2).getCell(5);
cell.setCellValue(new HSSFRichTextString("存款合计"));
cell = sheet.getRow(3).getCell(1);
cell.setCellValue(new HSSFRichTextString(" "));
cell = sheet.getRow(3).getCell(4);
cell.setCellValue(new HSSFRichTextString("折合本位币合计"));
cell = sheet.getRow(4).getCell(1);
cell.setCellValue(new HSSFRichTextString("人民币"));
cell = sheet.getRow(4).getCell(2);
cell.setCellValue(new HSSFRichTextString("折合本位币合计")); // ---------------------------3.合并单元格------------------------------
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 5));// 开始行,结束行,开始列,结束列
sheet.addMergedRegion(new CellRangeAddress(1, 1, 0, 5));
sheet.addMergedRegion(new CellRangeAddress(2, 4, 0, 0));
sheet.addMergedRegion(new CellRangeAddress(2, 3, 1, 2));
sheet.addMergedRegion(new CellRangeAddress(2, 2, 3, 4));
sheet.addMergedRegion(new CellRangeAddress(3, 4, 4, 4));
sheet.addMergedRegion(new CellRangeAddress(2, 4, 5, 5));
FileOutputStream fileOut = new FileOutputStream("d:\\银行存款余额表.xls");
wb.write(fileOut);
fileOut.close();
} catch (Exception e) {
e.printStackTrace();
}
} }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,491
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,493
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,132
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,294