当前位置:网站首页>Apache POI hssf操作Excel
Apache POI hssf操作Excel
2022-04-21 06:28:00 【小雅痞】
合并某列具有相同元素的单元格,并递归合并其他列。
参考文档:
1.java自动合并excel相同单元格
2.拿到不同类型单元格中的值
1.希望达成的效果

2.合并单元格函数
参考文献中,取cellValue此处如果取数字格式的单元格,会报错。改为使用getCellValue参数
import java.math.BigDecimal;
import java.text.DecimalFormat;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.CellRangeAddress;
/** * * @param sheet HSSFSheet表格 * @param colIndex 合并的列 * @param startRow 起始行 * @param stopRow 结束行 * @param isForward 是否递进合并其它列 * @param forwardToColIndex 递进到的列 */
public void mergeRowCell(HSSFSheet sheet,int colIndex,int startRow,int stopRow ,boolean isForward,int forwardToColIndex){
String compareValue = null;
int beginRow = startRow;
int endRow = startRow;
for(int i=startRow;i<=stopRow; ++i){
//String value = sheet.getRow(i).getCell(colIdx).getRichStringCellValue()==null ? "":sheet.getRow(i).getCell(colIdx).getRichStringCellValue().toString();
String value = getCellValue(sheet.getRow(i).getCell(colIndex)); //改动
if(i == startRow){
compareValue = value;
}else{
if(compareValue.equals(value)){
//相同,则设置重复的值为空
sheet.getRow(i).getCell(colIndex).setCellValue("");
endRow = i;
}else {
//不同,则合并之前相同的单元格
if(beginRow < endRow){
sheet.addMergedRegion(new CellRangeAddress(beginRow,endRow,colIndex,colIndex));
if(isForward){
//递进合并下一列
int nextColIndex = colIndex;
if(colIndex < forwardToColIndex){
nextColIndex ++;
}else if(colIndex > forwardToColIndex){
nextColIndex --;
}else{
return;
}
mergeRowCell(sheet, nextColIndex, beginRow, endRow, isForward, forwardToColIndex);
}
}
compareValue = value;
beginRow = i;
endRow = i;
}
}
}
if(beginRow < endRow){
sheet.addMergedRegion(new CellRangeAddress(beginRow,endRow,colIndex,colIndex));
if(isForward){
//递进合并下一列
int nextColIndex = colIndex;
if(colIndex < forwardToColIndex){
nextColIndex ++;
}else if(colIndex > forwardToColIndex){
nextColIndex --;
}else{
return;
}
mergeRowCell(sheet, nextColIndex, beginRow, endRow, isForward, forwardToColIndex);
}
}
}
3.获取不同类型单元格中的值
cell和String的判空做了修改,拿去即用不会报错。
/** * 拿到不同类型单元格中的值 * 1. 字符串: 字符串 * 2. 布尔: toString * 3. 数值(double): 格式化后的字符串 * @param cell 获取的单元格 * @return 单元格中的值 */
private static String getCellValue(Cell cell) {
String resultValue = "";
// 判空
if(cell == null || cell.getCellType() == Cell.CELL_TYPE_BLANK){
return resultValue;
}
// 拿到单元格类型
int cellType = cell.getCellType();
switch (cellType) {
// 字符串类型
case Cell.CELL_TYPE_STRING:
resultValue = cell.getStringCellValue().isEmpty() ? "" : cell.getStringCellValue().trim();
break;
// 布尔类型
case Cell.CELL_TYPE_BOOLEAN:
resultValue = String.valueOf(cell.getBooleanCellValue());
break;
// 数值类型
case Cell.CELL_TYPE_NUMERIC:
resultValue = new DecimalFormat("#.######").format(cell.getNumericCellValue());
break;
// 取空串
default:
break;
}
return resultValue;
}
3.修改合并后的单元格格式
sheet.getRow(i).getCell(j).setCellStyle(cellStyle); //补合并后的单元格样式
4.填充序号
int num = 1;
for(int i=0;i<=len;i++){
if(!sheet.getRow(i).getCell(0).getStringCellValue().equals(""))
{
sheet.getRow(i).getCell(0).setCellValue(String.valueOf(num++));
}
}
沾衣欲湿杏花雨,吹面不寒杨柳风。
版权声明
本文为[小雅痞]所创,转载请带上原文链接,感谢
https://blog.csdn.net/shijizhe1/article/details/124276851
边栏推荐
- 顺序表的建立和增删改查
- The use of NuMA by dpdk and the basis of performance tuning under multi NUMA architecture
- 图形学基础|抗锯齿(Anti-Aliasing)
- 【LeetCode169】多数元素
- 常用的MySQL语句
- Dpdk process Deadlock of blood case caused by program startup sequence
- 如何使用u深度启动u盘清除系统登录密码
- PowerShell - 批量文件延迟复制
- sqlmap的安装和使用
- Reflection cannot find the class message classnotfound of UDF when executing flinksql code
猜你喜欢

【LeetCode 67】二个进制数求和

Fundamentals of graphics | area light rendering based on LTC

The difference between on and where followed by conditions in the left join association table

图形学基础|深度缓冲(DepthBuffer)

【保姆安装教程】Linux操作系统中源码下载MySQL5.7

图形学基础|泛光(Bloom)

unreal连接MySQL

图形学基础|基于LTC的面光源渲染

VMWare vSphere Failed to create object

力扣-354.俄罗斯套娃信封问题
随机推荐
AWD平台搭建–Cardinal
Reading notes - how to be a great person
dpdk程序启动顺序引发的血案
【手部姿态估计】【论文精读】3D Hand Shape and Pose Estimation from a Single RGB Image
Process record of a failed migration of vmfstools from exsi
Udevd retrieves the kernel module and loads the demo
3D-MAX之Shader工具(描边)
PowerShell 执行策略 about_Execution_Policies
Dpdk-16.04 IGB CRC length statistics
Dpdk process Deadlock of blood case caused by program startup sequence
Dpdk-16.04 example demo and internal implementation analysis of monitoring UIO file to detect interrupt
图形学基础|景深效果(Depth of Field/DOF)
HLOJ 1936 铺满方格
WordPress modify upload file size limit
TX2上安装deepstream
C语言版:循环链表的建立
PowerShell - because running scripts is prohibited on this system
一次从 EXSI 移植 vmfstools 失败的过程记录
C语言版:二叉树的静态建立
Dpdk problem analysis: dpdk-20.11 ice 100g network card RSS_ Invalid hash configuration