Apache poi apachepoi中的rowspan和colspan

Apache poi apachepoi中的rowspan和colspan,apache-poi,Apache Poi,我正在尝试在ApachePOI中创建rowspan和colspan。您能告诉我如何实现这样的功能吗?我尝试过使用addMergedRegion方法,但这似乎只是colspan 月 储蓄 一月 2月 从您对addMergedRegion的提示中,我怀疑您在谈论Excel部分的ApachePOI 在这里,我可以使用Java和apache-poi版本3.17,如下例所示: import java.io.FileOutputStream; import org.apache.poi.ss.userm

我正在尝试在ApachePOI中创建rowspan和colspan。您能告诉我如何实现这样的功能吗?我尝试过使用addMergedRegion方法,但这似乎只是colspan


月
储蓄
一月
2月

从您对
addMergedRegion
的提示中,我怀疑您在谈论
Excel
部分的
ApachePOI

在这里,我可以使用
Java
apache-poi
版本
3.17
,如下例所示:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;

public class CreateExcelMergedRegions {

 public static void main(String[] args) throws Exception {

 /*
 goal:
     +---------+---------+----------+
     |    A    |    B    |    C     |
 +---+------------------------------+
 | 1 |         |      Savings       |
 +---+ Month   |--------------------+
 | 2 |         | January | February |
 +---+---------+---------+----------+
 */

  Workbook workbook = new XSSFWorkbook();
  //Workbook workbook = new HSSFWorkbook();

  Sheet sheet = workbook.createSheet();

  CellRangeAddress mergedRegion = new CellRangeAddress(0,0,1,2); // row 1 col B and C
  sheet.addMergedRegion(mergedRegion);
  mergedRegion = new CellRangeAddress(0,1,0,0); // row 1 and 2 col A 
  sheet.addMergedRegion(mergedRegion);

  Row row = sheet.createRow(0); // row 1
  Cell cell = row.createCell(0); // cell A1
  cell.setCellValue("Month");
  // set vertical alignment center
  CellUtil.setCellStyleProperty(cell, CellUtil.VERTICAL_ALIGNMENT, VerticalAlignment.CENTER); 

  cell = row.createCell(1); // cell B1
  cell.setCellValue("Savings");
  // set horizontal alignment center
  CellUtil.setCellStyleProperty(cell, CellUtil.ALIGNMENT, HorizontalAlignment.CENTER);

  row = sheet.createRow(1); // row 2
  cell = row.createCell(1); // cell B2
  cell.setCellValue("January");
  cell = row.createCell(2); // cell C2
  cell.setCellValue("February");

  if (workbook instanceof XSSFWorkbook) {
   workbook.write(new FileOutputStream("CreateExcelMergedRegions.xlsx"));
  } else if (workbook instanceof HSSFWorkbook) {
   workbook.write(new FileOutputStream("CreateExcelMergedRegions.xls"));
  }
  workbook.close();

 }

}

我怀疑您错过了对齐设置?

从您对
addMergedRegion
的提示中,我怀疑您在谈论
Excel
部分的
apache poi

在这里,我可以使用
Java
apache-poi
版本
3.17
,如下例所示:

import java.io.FileOutputStream;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import org.apache.poi.ss.util.CellRangeAddress;
import org.apache.poi.ss.util.CellUtil;

public class CreateExcelMergedRegions {

 public static void main(String[] args) throws Exception {

 /*
 goal:
     +---------+---------+----------+
     |    A    |    B    |    C     |
 +---+------------------------------+
 | 1 |         |      Savings       |
 +---+ Month   |--------------------+
 | 2 |         | January | February |
 +---+---------+---------+----------+
 */

  Workbook workbook = new XSSFWorkbook();
  //Workbook workbook = new HSSFWorkbook();

  Sheet sheet = workbook.createSheet();

  CellRangeAddress mergedRegion = new CellRangeAddress(0,0,1,2); // row 1 col B and C
  sheet.addMergedRegion(mergedRegion);
  mergedRegion = new CellRangeAddress(0,1,0,0); // row 1 and 2 col A 
  sheet.addMergedRegion(mergedRegion);

  Row row = sheet.createRow(0); // row 1
  Cell cell = row.createCell(0); // cell A1
  cell.setCellValue("Month");
  // set vertical alignment center
  CellUtil.setCellStyleProperty(cell, CellUtil.VERTICAL_ALIGNMENT, VerticalAlignment.CENTER); 

  cell = row.createCell(1); // cell B1
  cell.setCellValue("Savings");
  // set horizontal alignment center
  CellUtil.setCellStyleProperty(cell, CellUtil.ALIGNMENT, HorizontalAlignment.CENTER);

  row = sheet.createRow(1); // row 2
  cell = row.createCell(1); // cell B2
  cell.setCellValue("January");
  cell = row.createCell(2); // cell C2
  cell.setCellValue("February");

  if (workbook instanceof XSSFWorkbook) {
   workbook.write(new FileOutputStream("CreateExcelMergedRegions.xlsx"));
  } else if (workbook instanceof HSSFWorkbook) {
   workbook.write(new FileOutputStream("CreateExcelMergedRegions.xls"));
  }
  workbook.close();

 }

}
我怀疑你错过了对齐的设置