导航:首页 > 编程语言 > 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读取合并单元格相关的资料

热点内容
无线加密狗教程 浏览:761
十年前的安卓机还能干什么 浏览:255
怎么到文件夹找到王者回放 浏览:624
ad字体文件放在哪个文件夹 浏览:127
界面和算法并行设计 浏览:387
oa安卓系统叫什么 浏览:845
两用u盘加密 浏览:118
文件如何解压再压缩 浏览:622
使用编译命令导入库 浏览:118
app一般用什么服务器 浏览:821
手机端java编译 浏览:147
电脑文件夹一右键就卡 浏览:196
javalistforeach 浏览:28
奇葩解压笔 浏览:335
php判断字符串开头 浏览:270
多个pdf合并成一个pdf 浏览:735
linuxc获取mac 浏览:966
linux线程运行时间 浏览:381
机器人逆运算算法 浏览:304
腰椎压缩性骨折属于几级伤残 浏览:829