Bug 57885 - Add a function to copy a sheet in org.apache.poi.ss.util.SheetUtil class
Summary: Add a function to copy a sheet in org.apache.poi.ss.util.SheetUtil class
Alias: None
Product: POI
Classification: Unclassified
Component: XSSF (show other bugs)
Version: 3.12-dev
Hardware: PC All
: P2 enhancement with 4 votes (vote)
Target Milestone: ---
Assignee: POI Developers List
Keywords: PatchAvailable
Depends on:
Reported: 2015-05-04 12:19 UTC by Prejith Pulikkottil
Modified: 2016-06-19 20:28 UTC (History)
0 users

Merged Cell border style issue (49.99 KB, application/x-zip-compressed)
2015-05-26 10:06 UTC, Prejith Pulikkottil
copy a sheet from source to destination (81.17 KB, application/x-zip-compressed)
2015-06-16 12:33 UTC, Prejith Pulikkottil

Note You need to log in before you can comment on or make changes to this bug.
Description Prejith Pulikkottil 2015-05-04 12:19:24 UTC

Can you please add a copySheet method on org.apache.poi.ss.util.SheetUtil helper class?
It is great help on users to do the copy sheet process
Comment 1 Nick Burch 2015-05-04 13:08:55 UTC
To copy a sheet within one workbook, you just need to call workbook.cloneSheet(sheetIndex)

For any other kind of sheet copying, if you'd care to work up a patch to add the missing functionality, we'd be delighted to review it and commit it if appropriate!
Comment 2 Prejith Pulikkottil 2015-05-05 07:52:15 UTC
How do I copy the cloned to sheet to another workbook? for example copy a sheet from workbook1 and need to paste the copied one to another workbook2
Comment 3 Prejith Pulikkottil 2015-05-05 08:05:14 UTC

Also I have tired to copy sheet based on below open source forum using poi-3.11 jars

copy XLS sheets is working fine (XLS to XLS) I was able to open the copied sheet.
But XLSX sheets (XLSX to XLSX) giving me error while I opening the copied sheet "Removed Records: Merge cells from /xl/worksheets/sheet5.xml part"
Comment 4 Nick Burch 2015-05-05 08:57:11 UTC
I don't think we can use the code from coderanch as I don't believe it's under a compatible license

If you'd be able to write up (from scratch!) code to do the same things, we'd love it as a contribution to the project! See http://poi.apache.org/guidelines.html for a bit more on contributing
Comment 5 Prejith Pulikkottil 2015-05-26 10:06:20 UTC
Created attachment 32756 [details]
Merged Cell border style issue
Comment 6 Prejith Pulikkottil 2015-05-26 10:07:51 UTC
As suggested, I wrote a code snippet for copy sheet functionality from one work book to another. See the code snippet and attached workbook

After the copy functionality, my output file was corrupted and showed the error "Repaired Records: Format from /xl/styles.xml part (Styles)" 

When I look at the reason on different poi forums, I can see the that it is the issue with border style. And see the solution, unsetBorderId and unsetFillId

After applying the unset functions data was successfully copied without border style

In nutshell, I can see some issues with border styling the XSSF type files 

package eis.utils;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Date;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.apache.poi.xssf.usermodel.XSSFCellStyle;

public class XlsxExcelUtil {

  private static void removeRows(Sheet destSheet) {
    if (null != destSheet) {
      for (int i = destSheet.getFirstRowNum(); i <= destSheet.getLastRowNum(); i++) {
        Row row = destSheet.getRow(i);
        if (null != row) {

  private static void addRows(Sheet destSheet, int totalRowCount) {
    if (null != destSheet) {
      for (int i = 0; i <= totalRowCount; i++) {

  private static void copyMergedRegion(Sheet srcSheet, Sheet destSheet) {
    for (int i = 0; i < srcSheet.getNumMergedRegions(); i++) {

  private static void copyRow(Row srcRow, Row destRow) {
    for (int j = srcRow.getFirstCellNum(); j <= srcRow.getLastCellNum(); j++) {
      Cell srcCell = srcRow.getCell(j);
      if (srcCell != null) {
        Cell destCell = destRow.createCell(j);
        copyCell(srcCell, destCell);

  private static void copyCell(Cell srcCell, Cell destCell) {
    CellStyle cellStyleClazz = destCell.getSheet().getWorkbook()
    if (cellStyleClazz instanceof XSSFCellStyle) {
      XSSFCellStyle newCellStyle = (XSSFCellStyle) cellStyleClazz;
      // Issue with border style, so added this code
      // newCellStyle.getCoreXf().unsetBorderId();
      // newCellStyle.getCoreXf().unsetFillId();
      // -------------

    if (srcCell.getCellType() == Cell.CELL_TYPE_BLANK) {
    } else if (srcCell.getCellType() == Cell.CELL_TYPE_STRING) {
    } else if (srcCell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
    } else if (srcCell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
    } else if (srcCell.getCellType() == Cell.CELL_TYPE_FORMULA) {
    } else if (srcCell.getCellType() == Cell.CELL_TYPE_ERROR) {

   * Copy a sheet from one workbook to another workbook. The method supports
   * only two similar type of workbooks Xlsx type
   * @param srcSheet
   * @param destSheet
  public static void copySheet(Sheet srcSheet, Sheet destSheet) {
    addRows(destSheet, srcSheet.getLastRowNum());
    copyMergedRegion(srcSheet, destSheet);
    for (int i = srcSheet.getFirstRowNum(); i <= srcSheet.getLastRowNum(); i++) {
      Row srcRow = srcSheet.getRow(i);
      if (null == srcRow) {
      } else {
        Row destRow = destSheet.createRow(i);
        copyRow(srcRow, destRow);

  public void testXlsxSheetCopy() {
    try {
      File templateFile = new File("C:/TestXlsx/Template_V2.xlsx");
      InputStream inputStream = new FileInputStream(templateFile);
      Workbook merWorkBook = WorkbookFactory.create(inputStream);
      Sheet destPdrSheet = merWorkBook.getSheet("PDR");

      File pdrFile = new File("C:/TestXlsx/P23163.xlsx");
      InputStream pdrInputStream = new FileInputStream(pdrFile);
      Workbook pdrWorkBook = WorkbookFactory.create(pdrInputStream);
      Sheet srcPdrSheet = pdrWorkBook.getSheetAt(0);

      XlsxExcelUtil.copySheet(srcPdrSheet, destPdrSheet);

      ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

      FileOutputStream resultFile = new FileOutputStream(new File(
    } catch (Exception e) {

  public static void main(String[] args) {
    XlsxExcelUtil obj = new XlsxExcelUtil();
Comment 7 Prejith Pulikkottil 2015-06-16 12:33:00 UTC
Created attachment 32826 [details]
copy a sheet from source to destination

I have written the code sheet copy process, added test case in the same class. Current limitation is Font style was not able to copy, but I'm least prioritized this issue.
Attached the code and different type of source sheet and destination sheet 

Prejith George
Comment 8 Javen O'Neal 2016-06-19 20:28:24 UTC
(In reply to Prejith Pulikkottil from comment #7)
> Created attachment 32826 [details]
> copy a sheet from source to destination

Is this code licensed under Apache software license 2.0?

Could you reformulate this as a patch against POI trunk?

SheetUtil.java: https://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/util/SheetUtil.java?view=log
TestSheetUtil.java: https://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/util/TestSheetUtil.java?view=log
Excel files: https://svn.apache.org/viewvc/poi/trunk/test-data/spreadsheet/