导航:首页 > 编程语言 > java读取合并单元格

java读取合并单元格

发布时间:2022-08-08 15:24:10

java excel 怎么合并大的单元格

POIexcel表格何合并单元格

Java代码
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
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.Region;

public class ExcelTest {

/**
* @param args
*/
public static void main(String[] args) throws IOException {

try {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
HSSFCellStyle style = wb.createCellStyle(); // 式象

style.setVerticalAlignment(HSSFCellStyle.VERTICAL_CENTER);// 垂直
style.setAlignment(HSSFCellStyle.ALIGN_CENTER);// 水平
HSSFRow row = sheet.createRow((short) 0);
HSSFRow row2 = sheet.createRow((short) 1);

sheet.addMergedRegion(new Region(0, (short) 0, 1, (short) 0));
HSSFCell ce = row.createCell((short) 0);
ce.setEncoding(HSSFCell.ENCODING_UTF_16);// 文处理
ce.setCellValue("项目\\期"); // 表格第行第列显示数据
ce.setCellStyle(style); // 式居
int num = 0;
for (int i = 0; i < 9; i++) { // 循环9每都要跨单元格显示
// 计算单元格跨格
int celln = 0;
int celle = 0;
if (i == 0) {
celln = 0;
celle = 1;
} else {
celln = (i * 2);
celle = (i * 2 + 1);
}
// 单元格合并
// 四参数别:起始行起始列结束行结束列
sheet.addMergedRegion(new Region(0, (short) (celln + 1), 0,
(short) (celle + 1)));
HSSFCell cell = row.createCell((short) (celln + 1));
cell.setCellValue("merging" + i); // 跨单元格显示数据
cell.setCellStyle(style); // 式
// 跨单元格显示数据:两行行别两格格行两格数量金额
HSSFCell cell1 = row2.createCell((short) celle);
HSSFCell cell2 = row2.createCell((short) (celle + 1));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);
num++;
}

// 面加合计百比

// 合计 加要跨单元格
sheet.addMergedRegion(new Region(0, (short) (2 * num + 1), 0,
(short) (2 * num + 2)));
HSSFCell cell = row.createCell((short) (2 * num + 1));
cell.setEncoding(HSSFCell.ENCODING_UTF_16);
cell.setCellValue("合计");
cell.setCellStyle(style);
HSSFCell cell1 = row2.createCell((short) (2 * num + 1));
HSSFCell cell2 = row2.createCell((short) (2 * num + 2));
cell1.setEncoding(HSSFCell.ENCODING_UTF_16);
cell1.setCellValue("数量");
cell1.setCellStyle(style);
cell2.setEncoding(HSSFCell.ENCODING_UTF_16);
cell2.setCellValue("金额");
cell2.setCellStyle(style);

// 百比 同
sheet.addMergedRegion(new Region(0, (short) (2 * num + 3), 0,
(short) (2 * num + 4)));
HSSFCell cellb = row.createCell((short) (2 * num + 3));
cellb.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb.setCellValue("百比");
cellb.setCellStyle(style);
HSSFCell cellb1 = row2.createCell((short) (2 * num + 3));
HSSFCell cellb2 = row2.createCell((short) (2 * num + 4));
cellb1.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb1.setCellValue("数量");
cellb1.setCellStyle(style);
cellb2.setEncoding(HSSFCell.ENCODING_UTF_16);
cellb2.setCellValue("金额");
cellb2.setCellStyle(style);

FileOutputStream fileOut = new FileOutputStream("workbook.xls");
wb.write(fileOut);
fileOut.close();
System.out.print("OK");
} catch (Exception ex) {
ex.printStackTrace();
}

}

}

② java poi 怎么读取Excel中合并单元格的值,我读取合并单元格的第一个格有值,其他的都是空。

/**
* 获取合并单元格的值
* @param sheet
* @param row
* @param column
* @return
*/
public String getMergedRegionValue(Sheet sheet, int row, int column) {
int sheetMergeCount = sheet.getNumMergedRegions();

for (int i = 0; i < sheetMergeCount; i++) {
CellRangeAddress ca = sheet.getMergedRegion(i);
int firstColumn = ca.getFirstColumn();
int lastColumn = ca.getLastColumn();
int firstRow = ca.getFirstRow();
int lastRow = ca.getLastRow();

if (row >= firstRow && row <= lastRow) {
if (column >= firstColumn && column <= lastColumn) {
Row fRow = sheet.getRow(firstRow);
Cell fCell = fRow.getCell(firstColumn);

return getCellValue(fCell);
}
}
}

return null;
}

③ java poi怎么读取Excel中合并单元格的值

获取合并单元格的值 @param sheet @param row @param column @return。

④ java怎么合并excel单元格

有很多格式完全一样的Excel文件,只是每个Excel里面有很过工作薄,而这些工作薄的数据格式都相同,只是数据内容不同。

我想实现的是这样的Excel有很多,我就是想把这些Excle里面的所以数据都汇总在一个大的Excel里面,如果工作薄的名字相同,那么就在这同一个工作薄里面累数据就行....

阅读全文

与java读取合并单元格相关的资料

热点内容
数据库查询系统源码 浏览:612
php5314 浏览:352
完美国际安装到哪个文件夹 浏览:664
什么app可以扫一扫做题 浏览:535
程序员编码论坛 浏览:920
淘点是什么app 浏览:655
中国高等植物pdf 浏览:449
51单片机时间 浏览:177
后台如何获取服务器ip 浏览:261
单片机流水灯程序c语言 浏览:230
程序员第二职业挣钱 浏览:235
运行里怎么输入服务器路径 浏览:835
pythonstepwise 浏览:504
刘一男词汇速记指南pdf 浏览:57
php认证级别 浏览:363
方舟编译啥时候推送 浏览:1006
php手机验证码生成 浏览:670
哲学思维pdf 浏览:12
凌达压缩机有限公司招聘 浏览:528
weblogic命令部署 浏览:31