We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Copy table header from one workbook to another

My usecase is copying a table from one workbook to another. For that, I am trying to convert the ListObject to DataRange and using the copy function and PasteOptions to copy the listobject to target workbook.

Following is the code snippet:

private Workbook createWorkBook3(final ListObject table){
Workbook newWorkbook = new Workbook();
WorksheetCollection tempWsc = newWorkbook.getWorksheets();
com.aspose.cells.Worksheet tempWs = (com.aspose.cells.Worksheet)tempWsc.get(0);
table.setShowHeaderRow(true);
Range sourceRange = table.getDataRange();
Range targetRange = tempWs.getCells().createRange(0, 0, sourceRange.getRowCount(), sourceRange.getColumnCount());
PasteOptions options = new PasteOptions();
options.setPasteType(PasteType.ALL);
targetRange.copy(sourceRange, options);

return newWorkbook;
}

I am able to copy the table data along with styles info except the header row.

To copy the header row, I tried using table.getListColumns() but somehow it gives me text of the header and not its styling info. I also try fetching the range from eaqch header column using listColumn.getRange(), but this also returns null.

Kindly suggest a way to copy the table header from one workbook to another

Thanks

Hi,


I think you may try to add the header row to the source range as well, so when you copy the range from source workbook to target workbook range, it also gets copied. I tried the following sample code with the attached template file, it works fine and the list object/ table also gets copied with header row as well.
e.g
Sample code:

Workbook book = new Workbook(“f:\files\Bk_Tables.xlsx”);

ListObjectCollection listObjects = book.getWorksheets().get(0).getListObjects();
ListObject table = listObjects.get(0);
Workbook newWorkbook = new Workbook(FileFormatType.XLSX);
WorksheetCollection tempWsc = newWorkbook.getWorksheets();
com.aspose.cells.Worksheet tempWs = (com.aspose.cells.Worksheet)tempWsc.get(0);
table.setShowHeaderRow(true);
Range sourceRange = table.getDataRange();
int fcol = sourceRange.getFirstColumn();
int frow = sourceRange.getFirstRow()-1; //including the first header row.
int rowCount = sourceRange.getRowCount()+1;
int colCount = sourceRange.getColumnCount();
Range nSourceRange = book.getWorksheets().get(0).getCells().createRange(frow,fcol,rowCount,colCount);
Range targetRange = tempWs.getCells().createRange(frow, fcol, nSourceRange.getRowCount(), nSourceRange.getColumnCount());
targetRange.copy(nSourceRange);
newWorkbook.save(“f:\files\outBk_Tables1.xlsx”);

Hope, this helps a bit.

Thank you.

Thanks for the quick response.


It works fine.but I have another question over this.

This code would work fine if the table has as header row. Would there be a case where table is without header row. If yes, then how to recognize such cases?

Hi,


Well, you may use table.getShowHeaderRow() method to check if the list object/ table has a header row (you may evaluate in an expression (e.g if construct)), if it has you will include header row in the range in your code otherwise not.

Thank you.