乔山办公网我们一直在努力
您的位置:乔山办公网 > excel表格制作 > POI生成excel表格,如何合并单元格-poi合并excel单元格,poi合并单元格并居中

POI生成excel表格,如何合并单元格-poi合并excel单元格,poi合并单元格并居中

作者:乔山办公网日期:

返回目录:excel表格制作


/**
* 获取合并单元格的值
* @e68a847a64339param 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;
}

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++; } // 在后面加e69da5e887aae799bee5baa6e997aee7ad94338上合计百分比 // 合计 在最后加上,还要跨一个单元格 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(); } } }
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");

HSSFRow row = sheet.createRow((short) 1);
HSSFCell cell = row.createCell((short) 1);

HSSFRichTextString content = new HSSFRichTextString("This is a test of message");
cell.setCellValue(content);

sheet.addMergedRegion(new Region(1,(short)1,1,(short)2));

FileOutputStream fileOut = new FileOutputStream("d:/Demo_02_05.xls");
wb.write(fileOut);
fileOut.close();

POI生成excel表格,如何合并单元格

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("项目\\日期"); // 表格的第一行e799bee5baa6e79fa5e98193e59b9ee7ad94331第一列显示的数据
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();
}

}

}

相关阅读

关键词不能为空
极力推荐

ppt怎么做_excel表格制作_office365_word文档_365办公网