如何使用POI删除合并的区域? [英] How to remove Merged region using POI?

查看:4875
本文介绍了如何使用POI删除合并的区域?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我们可以合并使用sheet.addMergedRegion(范围)细胞。我想知道如何删除合并。


  1. 我们可以使用sheet.removeMergedRegion(INT)

  2. 如果是的话那么告诉我应该是什么参数

  3. 将发生在合并单元格中的数据present什么。


解决方案

快速解答



  1. 合并后的区域中板材的ID

  2. 的数据被保持在该区域的第一单元,其它单元将是空

说明

您可以用sheet.getMergedRegion(i),其中i是其在片标识符得到合并的区域。

  / * ...初始化工作簿和工作表在这里... * ///循环在每片区域合并
的for(int i = 0; I< sheet.getNumMergedRegions(); ++ I)
{
    //删除区域
    sheet.removeMergedRegion(ⅰ);
}

当你做这样的事情,合并后的区域的内容将保持在这一地区的第一个单元格(左上一个)。其他细胞将有一个空的内容。

示例:
如果你有(A1和A2)合并与内容合并区域
分裂的结果将是(A2)的空和(A1)和该内容合并区域

请注意,这是如何将数据存储在内部,甚至当你尝试获取包含在合并区域单元格的值。保持同样的例子,你将有:

  CellReference REF =新CellReference(A1);
鳞次栉比= sheet.getRow(ref.getRow());
电池单元= row.getCell(ref.getCol());
的System.out.println(新DataFormatter()formatCellValue(单元)); //将打印合并区域REF =新CellReference(A2);
行= sheet.getRow(ref.getRow());
细胞= row.getCell(ref.getCol());
的System.out.println(新DataFormatter()formatCellValue(单元)); //将打印空字符串

这将是相同的行为之前和除去合并的区域之后

更多信息

如果你想获得一个特定的区域合并,拆分它,你将不得不在你的循环添加一个条件,以确定它:

  //如果我们要拆分合并的单元格开始D3
CellReference REF =新CellReference(D3);的for(int i = 0; I< sheet.getNumMergedRegions(); ++ I)
{
    的CellRangeAddress范围= sheet.getMergedRegion(ⅰ);    如果(range.getFirstRow()== ref.getRow()及&放大器; range.getLastRow()== ref.getCol())
    {
        sheet.removeMergedRegion(ⅰ);
    }
}

I know we can merge cells using sheet.addMergedRegion(range). I want to know how to remove merge.

  1. Can we use sheet.removeMergedRegion(int)
  2. If yes then tell me what should be the argument
  3. What will happen to the data present in the Merged cell.

解决方案

Quick answer

  1. Yes
  2. The id of the merged region in the sheet
  3. The data is kept in the first cell of the region, other cells will be empty

Explanation

You can get the merged regions with sheet.getMergedRegion(i), where i is its identifier in the sheet.

/* ... initialize your workbook and sheet here ... */

// Loop through every merged region in the sheet
for(int i=0; i < sheet.getNumMergedRegions(); ++i)
{
    // Delete the region
    sheet.removeMergedRegion(i);
}

When you do such a thing, the content of the merged region will be kept in the first cell of this region (the top-left one). Other cells will have an empty content.

Example: If you have (A1 and A2) merged with the content "merged region", the result of the split will be (A2) empty and (A1) with the content "merged region".

Note that this is how the data is stored internally, even when you try to get the value of a cell contained in a merged region. Keeping the same example, you will have:

CellReference ref = new CellReference("A1");
Row row = sheet.getRow( ref.getRow() );
Cell cell = row.getCell( ref.getCol() );
System.out.println( new DataFormatter().formatCellValue(cell) ); // Will print "merged region"

ref = new CellReference("A2");
row = sheet.getRow( ref.getRow() );
cell = row.getCell( ref.getCol() );
System.out.println( new DataFormatter().formatCellValue(cell) ); // Will print empty string

This will be the same behaviour before and after removing the merged region.

More information

If you want to get a specific merged region to split it, you will have to add a condition in your loop to identify it:

// If we want to split the merged cell starting at D3
CellReference ref = new CellReference("D3");

for(int i=0; i < sheet.getNumMergedRegions(); ++i)
{
    CellRangeAddress range = sheet.getMergedRegion(i);

    if ( range.getFirstRow() == ref.getRow() && range.getLastRow() == ref.getCol() )
    {
        sheet.removeMergedRegion(i);
    }
}

这篇关于如何使用POI删除合并的区域?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆