Apache POI. Copying sheets - java

I'm using apache poi to create an excel document. To create new sheet in workbook I write next code:
Workbook wb = new HSSFWorkbook();
Sheet sh = wb.createSheet();
this code create and add sheet to workbook. But I want to create sheet formerly and then add it to workbook. Smth like this:
Sheet sh = new HSSFSheet();
wb.addSheet(sh);
I need such thing, because I want to copy data from one sheet of one workbook to another sheet of another workbook(Workbook interface has method Sheet cloneSheet(int)). But Workbook interface doesn't have method like addSheet(Sheet sh).
Also HSSFWorkbook is final class so I can't extend it to implement add method
How can I do this?

You can't just take a Sheet object from one Workbook, and add it to a different Workbook.
What you'll need to do is to open the old workbook and the new workbooks at the same time, and create the sheet in the new workbook. Next, clone all the styles you used in the old sheet onto the new one (HSSFCellStyle has a method for cloning a style from one workbook to another). Finally, iterate over all the cells and copy them over.

You should use RangeCopier.
XSSFWorkbook workbookFrom = new XSSFWorkbook(new File("/path/to/workbookFrom.xlsx"));
XSSFSheet sheetFrom = workbookFrom.getSheetAt(0);
XSSFWorkbook workbookTo = new XSSFWorkbook(new File("/path/to/workbookTo.xlsx"));
XSSFSheet sheetTo = workbookTo.createSheet("sheet1");
workbookTo.setSheetOrder("sheet1", 0);
XSSFRangeCopier xssfRangeCopier = new XSSFRangeCopier(sheetFrom, sheetTo);
int lastRow = sheetFrom.getLastRowNum();
int lastCol = 0;
for (int i = 0; i < lastRow; i++) {
Row row = sheetFrom.getRow(i);
if (row != null) {
if (row.getLastCellNum() > lastCol) {
lastCol = row.getLastCellNum();
}
sheetTo.setDefaultRowHeight(sheetFrom.getDefaultRowHeight());
}
}
for (int j = 0; j < lastCol; j++) {
sheetTo.setColumnWidth(j, sheetFrom.getColumnWidth(j));
}
CellRangeAddress cellAddresses = new CellRangeAddress(0, lastRow, 0, lastCol);
xssfRangeCopier.copyRange(cellAddresses, cellAddresses, true, true);
workbookTo.write(new FileOutputStream(new File("/path/to/worksheetTo.xlsx")));

POI version < v4.0
Okay I tried to do what Gagravarr said above. This solution works for me. This code will work if the sheets don't have tables, etc. If the sheets contain simple text (String, boolean, int etc), formulas, this solution will work.
Workbook oldWB = new XSSFWorkbook(new FileInputStream("C:\\input.xlsx"));
Workbook newWB = new XSSFWorkbook();
CellStyle newStyle = newWB.createCellStyle(); // Need this to copy over styles from old sheet to new sheet. Next step will be processed below
Row row;
Cell cell;
for (int i = 0; i < oldWB.getNumberOfSheets(); i++) {
XSSFSheet sheetFromOldWB = (XSSFSheet) oldWB.getSheetAt(i);
XSSFSheet sheetForNewWB = (XSSFSheet) newWB.createSheet(sheetFromOldWB.getSheetName());
for (int rowIndex = 0; rowIndex < sheetFromOldWB.getPhysicalNumberOfRows(); rowIndex++) {
row = sheetForNewWB.createRow(rowIndex); //create row in this new sheet
for (int colIndex = 0; colIndex < sheetFromOldWB.getRow(rowIndex).getPhysicalNumberOfCells(); colIndex++) {
cell = row.createCell(colIndex); //create cell in this row of this new sheet
Cell c = sheetFromOldWB.getRow(rowIndex).getCell(colIndex, Row.CREATE_NULL_AS_BLANK ); //get cell from old/original WB's sheet and when cell is null, return it as blank cells. And Blank cell will be returned as Blank cells. That will not change.
if (c.getCellType() == Cell.CELL_TYPE_BLANK){
System.out.println("This is BLANK " + ((XSSFCell) c).getReference());
}
else { //Below is where all the copying is happening. First It copies the styles of each cell and then it copies the content.
CellStyle origStyle = c.getCellStyle();
newStyle.cloneStyleFrom(origStyle);
cell.setCellStyle(newStyle);
switch (c.getCellTypeEnum()) {
case STRING:
cell.setCellValue(c.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cell.setCellValue(c.getDateCellValue());
} else {
cell.setCellValue(c.getNumericCellValue());
}
break;
case BOOLEAN:
cell.setCellValue(c.getBooleanCellValue());
break;
case FORMULA:
cell.setCellValue(c.getCellFormula());
break;
case BLANK:
cell.setCellValue("who");
break;
default:
System.out.println();
}
}
}
}
}
//Write over to the new file
FileOutputStream fileOut = new FileOutputStream("C:\\output.xlsx");
newWB.write(fileOut);
oldWB.close();
newWB.close();
fileOut.close();
If your requirement is to copy full sheets without leaving or adding anything. I think The process of elimination works better and faster then the above code. And you don't have to worry about losing formulas, drawings, tables, styles, fonts, etc.
XSSFWorkbook wb = new XSSFWorkbook("C:\\abc.xlsx");
for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) {
if (!wb.getSheetName(i).contentEquals("January")) //This is a place holder. You will insert your logic here to get the sheets that you want.
wb.removeSheetAt(i); //Just remove the sheets that don't match your criteria in the if statement above
}
FileOutputStream out = new FileOutputStream(new File("C:\\xyz.xlsx"));
wb.write(out);
out.close();
POI version >= v4.0
As of version 4.0, Cell.CELL_TYPE_BLANK and Row.CREATE_NULL_AS_BLANK don't exist (they deprecated). Use CellType.* and Row.MissingCellPolicy.* instead.

Related

Applying foreground color / highlight to specific rows of the excel using Apache POI

Am copying data/ sheet from one excel workbook to another workbook using copyRow. I want to apply foreground color to specific rows in my target excel. Below is the code i have now. Please help how should i do it.
Found the link for Cell Style from POI's quick guide. But please help to use it to apply it to specific rows in the target excel.
https://poi.apache.org/components/spreadsheet/quick-guide.html#FillsAndFrills
My Program
public class Color1 {
public static void main(String[] args) throws Exception{
XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream("C:\\Color1.xlsx"));
XSSFSheet sheet = workbook.getSheetAt(0);
copyRow(workbook, sheet, 0 , 1);
FileOutputStream out = new FileOutputStream("C:\\Color2.xlsx");
workbook.write(out);
out.close();
}
private static void copyRow(XSSFWorkbook workbook, XSSFSheet worksheet, int sourceRowNum, int destinationRowNum) {
// Getting the source / new row
XSSFRow newRow = worksheet.getRow(destinationRowNum);
XSSFRow sourceRow = worksheet.getRow(sourceRowNum);
// Looping through source columns to add to new row
for (int i = 0; i < sourceRow.getLastCellNum(); i++) {
// Grab a copy of the old/new cell
XSSFCell oldCell = sourceRow.getCell(i);
XSSFCell newCell = newRow.createCell(i);
}
}
}

Apache POI writes only one record

I am writing some data into excel file using Apache POI but for some reason the file shows only the last record (1 record only). I have list of POLJO that I am passing. I am also iterating through the cells but all I get is just one record.
Method to write in excel
public void writeToExcel(List<NYProgramTO> to){
try {
Workbook workBook = new HSSFWorkbook();
CreationHelper helper = workBook.getCreationHelper();
Sheet sheet = workBook.createSheet("NY_PPA_P3_Sheet");
Row headerRow = sheet.createRow(0);
headerRow.createCell(0).setCellValue("First Name");
headerRow.createCell(1).setCellValue("Last Name");
headerRow.createCell(2).setCellValue("Policy Number");
headerRow.createCell(3).setCellValue("Zip Code");
headerRow.createCell(4).setCellValue("Date of Birth");
if(to != null){
int size = to.size();
for(int i = 0; i < size; i++){
NYProgramTO nyP= to.get(i);
Row row = sheet.createRow(1);
row.createCell(0).setCellValue(nyP.getFirstName());
row.createCell(1).setCellValue(nyP.getLastName());
row.createCell(2).setCellValue(nyP.getPolicyNumber());
row.createCell(3).setCellValue(nyP.getZipCode());
row.createCell(4).setCellValue(nyP.getDateOfBirth());
}
}
FileOutputStream stream = new FileOutputStream("NY_PPA_P3.xlsx");
workBook.write(stream);
stream.close();
System.out.println("NY_PPA_P3.xlsx created successfully.");
} catch (Exception ex) {
ex.printStackTrace();
}
}
If by "only one record" you mean that only one row is appearing, this is probably easily fixable by making sure that you increment the Row that is being created before writing the Cells.
Try changing:
Row row = sheet.createRow(1);
to:
Row row = sheet.createRow(i+1);

Data Written in One Excel Column Java

Hi i am new to writing data to excel however after doing much research and reading documentation I have done it.
My Problem:
When string data is stored in my array and written it to excel however it does not put it in one column (which is what I want) it spaces each string out in a column to the right of it in a diagonal descent in my spread sheet. A picture is below.
What I want is for each string/name to be put in one column under each other. Any suggestions would be appreciated.
Code Snippet:
System.out.println("Write data to an Excel Sheet");
FileOutputStream out = new FileOutputStream("C:/Users/hoflerj/Desktop/text2excel/finish.xlsx");
XSSFWorkbook workBook = new XSSFWorkbook();
XSSFSheet spreadSheet = workBook.createSheet("clientid");
XSSFRow row;
XSSFCell cell;
for(int i=0;i<arr.size();i++){
row = spreadSheet.createRow((short) i);
cell = row.createCell(i);
System.out.println(arr.get(i));
cell.setCellValue(arr.get(i).toString());
}
System.out.println("Done");
workBook.write(out);
arr.clear();
out.close();
You are increasing column index as well.
Just change from:
cell = row.createCell(i);
To:
cell = row.createCell(0);
So the For Loop will be like:
for(int i=0;i<arr.size();i++){
row = spreadSheet.createRow((short) i);
cell = row.createCell(0);
System.out.println(arr.get(i));
cell.setCellValue(arr.get(i).toString());
}
Or with fewer lines:
for(int i=0;i<arr.size();i++){
row = spreadSheet.createRow((short) i);
row.createCell(0).setCellValue( arr.get(i).toString() );
System.out.println(arr.get(i));
}

Excel sheet merged cell reading using apache poi in java

I am reading the excel sheet using Apache poi in java and I am using CellRangeAddress to get the region.
Case1: If I am giving 2-3 data for merging and going for next cell then it's ok.
I'm getting the next merged region .
Case2: If I am giving more than 6 values and going for next region, then It is showing IndexOutofBoundException for merged region
Here The Code:
List<OrganizationDB> orgList = new ArrayList<OrganizationDB>();
List<EmployeeDB> empList;
XSSFWorkbook workBook;
XSSFSheet excelSheet;
XSSFRow row;
XSSFCell cells;
TreeViewer treeViewer = null;
File excelFile = new File("D:\\ExcelExport\\ExcelSheet2.xls");
FileInputStream fis;
if (excelFile.exists()) {
fis = new FileInputStream(excelFile);
workBook = new XSSFWorkbook(fis);
excelSheet = workBook.getSheetAt(0);
int count = 1;
while (count <= excelSheet.getLastRowNum()) {
CellRangeAddress region = excelSheet.getMergedRegion(count);
row = excelSheet.getRow(count);
//XSSFCell cell = row.getCell(0);
orgDb = new OrganizationDB();
orgDb.setOrganizationName(row.getCell(0).getStringCellValue());
orgDb.setCityName(row.getCell(4).getStringCellValue());
orgDb.setStateName(row.getCell(5).getStringCellValue());
empList = new ArrayList<EmployeeDB>();
while(count<=region.getLastRow()) {
row = excelSheet.getRow(count);
empDb = new EmployeeDB();
empDb.setCompanyName(row.getCell(0).getStringCellValue());
empDb.setEmpID(row.getCell(1).getStringCellValue());
empDb.setEmpName(row.getCell(2).getStringCellValue());
empDb.setPhoneNo((int) row.getCell(3).getNumericCellValue());
empList.add(empDb);
orgDb.setEmpList(empList);
count++;
}
orgList.add(orgDb);
}
I see a logic in your code, which I do not fully understand. Could you check it please?
You have one counter count used by two nested while loops.
while (count <= excelSheet.getLastRowNum()) {
CellRangeAddress region = excelSheet.getMergedRegion(count);
...
while(count<=region.getLastRow()){
...
count++;
perhaps there are two merged regions with rows 1 to 3 and 4 to 6, then after first run of your top while your count = 3, because nested while increased it.
Then code tries to get mergedRegion(3) and
there is no mergedRegion with the index 3.
It must be mergedRegion(2) with next set of rows instead...
I guess you have to use different counters for mergedRegions and rows in them.

Cloning sheets between files

I have three or more excel files with different sheets among them and I need to create a new blank file with a copy (or clone) that sheets into the new file and place them in the order I need so I can fill out the respective forms with data.
How can I do this by using Jakarta POI (XSSFWorkbook)?
First up, I think you mean Apache POI - it hasn't been Apache Jakarta POI for quite a few years now...
In terms of copying sheets from one workbook to another, it can be done, but it will require some coding. First you'll want to identify the cell styles you use, and clone those across. Make sure you keep track of which source Cell Style goes to which destination Cell Style, as you don't want to keep re-creating or you'll hit the limit! CellStyle.cloneStyleFrom(CellStyle) is the method you'll want.
Then, for each source sheet, create a sheet in the target workbook. Loop over all the source rows, creating new target ones. Then loop over the cells, switch by cell type, grab the appropriate value and set it. Rinse and repeat!
FileOutputStream os = new FileOutputStream("differnetFileName")
readWorkbook.write(os);
guess we can make use of write operation to OS with diff file name will work .
This is my implementation of copying sheets from one workbook to another. I did everything as described by Gagravarr. This solution works for me. This code will work if the sheets don't have tables, etc. If the sheets contain simple text (String, boolean, int etc), formulas, this solution will work.
Workbook oldWB = new XSSFWorkbook(new FileInputStream("C:\\input.xlsx"));
Workbook newWB = new XSSFWorkbook();
CellStyle newStyle = newWB.createCellStyle(); // Need this to copy over styles from old sheet to new sheet. Next step will be processed below
Row row;
Cell cell;
for (int i = 0; i < oldWB.getNumberOfSheets(); i++) {
XSSFSheet sheetFromOldWB = (XSSFSheet) oldWB.getSheetAt(i);
XSSFSheet sheetForNewWB = (XSSFSheet) newWB.createSheet(sheetFromOldWB.getSheetName());
for (int rowIndex = 0; rowIndex < sheetFromOldWB.getPhysicalNumberOfRows(); rowIndex++) {
row = sheetForNewWB.createRow(rowIndex); //create row in this new sheet
for (int colIndex = 0; colIndex < sheetFromOldWB.getRow(rowIndex).getPhysicalNumberOfCells(); colIndex++) {
cell = row.createCell(colIndex); //create cell in this row of this new sheet
Cell c = sheetFromOldWB.getRow(rowIndex).getCell(colIndex, Row.CREATE_NULL_AS_BLANK ); //get cell from old/original WB's sheet and when cell is null, return it as blank cells. And Blank cell will be returned as Blank cells. That will not change.
if (c.getCellType() == Cell.CELL_TYPE_BLANK){
System.out.println("This is BLANK " + ((XSSFCell) c).getReference());
}
else { //Below is where all the copying is happening. First It copies the styles of each cell and then it copies the content.
CellStyle origStyle = c.getCellStyle();
newStyle.cloneStyleFrom(origStyle);
cell.setCellStyle(newStyle);
switch (c.getCellTypeEnum()) {
case STRING:
cell.setCellValue(c.getRichStringCellValue().getString());
break;
case NUMERIC:
if (DateUtil.isCellDateFormatted(cell)) {
cell.setCellValue(c.getDateCellValue());
} else {
cell.setCellValue(c.getNumericCellValue());
}
break;
case BOOLEAN:
cell.setCellValue(c.getBooleanCellValue());
break;
case FORMULA:
cell.setCellValue(c.getCellFormula());
break;
case BLANK:
cell.setCellValue("who");
break;
default:
System.out.println();
}
}
}
}
}
//Write over to the new file
FileOutputStream fileOut = new FileOutputStream("C:\\output.xlsx");
newWB.write(fileOut);
oldWB.close();
newWB.close();
fileOut.close();
If your requirement is to copy full sheets without leaving or adding anything. I think The process of elimination works better and faster then the above code. And you don't have to worry about losing formulas, drawings, tables, styles, fonts, etc.
XSSFWorkbook wb = new XSSFWorkbook("C:\\abc.xlsx");
for (int i = wb.getNumberOfSheets() - 1; i >= 0; i--) {
if (!wb.getSheetName(i).contentEquals("January")) //This is a place holder. You will insert your logic here to get the sheets that you want.
wb.removeSheetAt(i); //Just remove the sheets that don't match your criteria in the if statement above
}
FileOutputStream out = new FileOutputStream(new File("C:\\xyz.xlsx"));
wb.write(out);
out.close();

Categories