How to insert range from template file into export file (Java)?

Hi there,

I want to insert range from template file into export file.
In .NET, this can be done by following easilly steps:

1. Define name “Header” for range in template file;
2. Get workbook and worksheet from template file
3. Range templateHeader = workbook.Worksheets.GetRangeByName(“Header”);
4. Range rangeHeader = worksheet.Cells.CreateRange(currentRow, 0, templateHeader.RowCount, templateHeader.ColumnCount)
5. rangeHeader.CopyStyle(templateHeader)

How I can do this in Java?

Hi,

Thanks for your inquiry.

Well, NamedRange/Range .copyStyle() is not available for the Java version yet, we will consider it to support it soon for your need. I think as a workaround you may try Cells.copyCellRange() method to copy the range's data with its formattings and then clears the contents (data) and fill your desired data to the those cells.

I have created a sample code for your requirement, may the following sample code help your for your need, kindly consult it. Also, the input and output files are attached.

Sample code:

Workbook workbook = new Workbook();
workbook.open("e:\\files\\bkStyles.xls");
Worksheet worksheet = workbook.getWorksheets().getSheet(0);
Cells cells = worksheet.getCells();
NamedRange templateHeader = workbook.getWorksheets().getRangeByName("Header");
NamedRange rangeHeader = worksheet.getCells().createNamedRange("RangeHeader","G9","I11");
cells.copyCellRange(cells,templateHeader.getStartRow(),templateHeader.getStartColumn(), rangeHeader.getStartRow(),rangeHeader.getStartColumn(), 3,3);
cells.clearContents( rangeHeader.getStartRow(), rangeHeader.getStartColumn(), rangeHeader.getEndRow(), rangeHeader.getEndColumn());
workbook.save("e:\\files\\testbkStyles.xls");

Thank you.

Thanks for your helping,

I’ve found NamedRange.getCells() property is annoying because of return cells array (Cell[][]) instead of needed Cells object.

Cells.copyCellRange() is pretty but this lack copy row height and column width features.

Java version can’t clear names, it’s got only get, not set.

Hi,

I think you may use NamedRange.getCells() in the way (mentioned below) to get the individual cells with their row height / col width in the range, may the following sample code help you for your need, kindly consult it if it suits your need:

Sample code:

Cell[][] cellrange = templateHeader.getCells();

int r;
int c;
for(int i = 0;i<templateHeader.getEndRow();i++)

{

for(int j = 0;j<templateHeader.getEndColumn();j++)

{


System.out.println(cellrange[i][j].getName());
r = cellrange[i][j].getRowIndex();
c = cellrange[i][j].getColumnIndex();
float rht = cells.getRowHeight(r);
float cwd = cells.getColumnWidth(c);
System.out.println("Cell " + cellrange[i][j].getName() + " Row height: " + rht + " ColumnWidth: " + cwd);


}


}

Thank you.