Hi, i'm suffering a wierd behaviour in my code, and i dont understand why.
Let me explain you my situation first. I'm reading a xlsx file, i'm saving
this buffer in a XSSFWorkBook object. when i read this stream completly,
i've create a new XSSFWorkBook for write the contain of the other
object(gonna call wbToRead) , this create a n Sheets(depending the sheet
number of the wbToRead) for this sheet i create n rows as rows has the
wbToRead as well, and for any row i read the original value of the Cell of
the wbToRead, i've made a modification and i've save in a new cell that has
been created by rowToWrite, so the idea basically as you can imagine is read
a xlsx file, modify some values and then save again, but this time, separate
every sheet in a independient xlsx. In theory every sheet that i have in my
original xlsx now would be a new an modify xlsx, and this is it, but the
wierd behaviour is that files has weight 5,6,15 kb, but when i open one the
values of cells are completely empty. Any idea guys?.
I leave here the code for the code monkeys:
public void process(String fileName){
try {
XSSFWorkbook wb = new XSSFWorkbook(fileName);
readIteration(wb);
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
}
}
private int sheetNumber=0;
private String header="";
private String body="";
XSSFWorkbook workbook = null;
XSSFSheet sheetToWrite = null;
Row rowToWrite = null;
private void readIteration(XSSFWorkbook wb){
int sheetNumbers = wb.getNumberOfSheets();
for(int i=0; i < sheetNumbers; i++){
Sheet sheetToRead = wb.getSheetAt(i);
workbook = new XSSFWorkbook();
sheetToWrite = workbook.createSheet();
int z=0;
for (Iterator<Row> rit = sheetToRead.rowIterator();
rit.hasNext(); ) {
Row rowToRead = rit.next();
Row rowToWrite = sheetToWrite.createRow(z);
body +="\n";
int k =0;
for (Iterator<Cell> cit = rowToRead.cellIterator();
cit.hasNext();) {
Cell cellToRead = cit.next();
Cell cellToWrite = rowToWrite.createCell(k++);
readValue(cellToRead, cellToWrite);
}
//rows.put(++z, cells);
//TODO:Save as independient xlsx
}
System.out.print("Sheet Number:" + ++sheetNumber+"\n");
System.out.print(header);
System.out.print(body);
header="";
body="";
createOutPutSheet(sheetNumber,workbook);
}
}
public final static int CELL_TYPE_NUMERIC = 0;
public final static int CELL_TYPE_STRING = 1;
public final static int CELL_TYPE_FORMULA = 2;
public final static int CELL_TYPE_BLANK = 3;
public final static int CELL_TYPE_BOOLEAN = 4;
public final static int CELL_TYPE_ERROR = 5;
private Cell readValue(Cell cellToRead, Cell cellToWrite){
if(cellToRead.getRowIndex() == 0){
drawHeader(cellToRead,cellToWrite);
return cellToRead;
}
switch (cellToRead.getCellType()){
case CELL_TYPE_NUMERIC:
double cellNumericValue =
cellToRead.getNumericCellValue();
body = body + "|" + cellNumericValue +"";
cellNumericValue=666;
cellToWrite.setCellValue(cellNumericValue);
break;
case CELL_TYPE_STRING:
String cellStringValue = cellToRead.getStringCellValue();
body = body + "|" + cellStringValue +"";
cellStringValue +="_NEW";
cellToWrite.setCellValue(cellStringValue);
break;
case CELL_TYPE_FORMULA:
String cellFormulaValue = cellToRead.getCellFormula();
body = body + "|" + cellFormulaValue +"";
break;
case CELL_TYPE_BLANK:
//body = body + "|";
break;
case CELL_TYPE_BOOLEAN:
boolean cellBooleanValue =
cellToRead.getBooleanCellValue();
body = body + "|" + cellBooleanValue +"";
cellBooleanValue=false;
cellToWrite.setCellValue(cellBooleanValue);
break;
case CELL_TYPE_ERROR:
byte error = cellToRead.getErrorCellValue();
body = body + "|" + error +"";
break;
default:
break;
}
return cellToRead;
}
private boolean isHeader(Cell cell){
return (cell.getRowIndex() == 1);
}
private void drawHeader(Cell cellToRead, Cell cellToWrite){
try{
if(cellToRead.getCellType()== Cell.CELL_TYPE_NUMERIC){
double cellNumericValue = cellToRead.getNumericCellValue();
header = header + "|" + cellNumericValue;
cellNumericValue=666;
cellToWrite.setCellValue(cellNumericValue);
}else{
String cellStringValue = cellToRead.getStringCellValue();
header = header + "|" + cellStringValue;
cellStringValue +="_NEW";
cellToWrite.setCellValue(cellStringValue);
}
}catch(Exception e){
System.out.print("" + e);
}
}
private void createOutPutSheet(int sheetNumber, XSSFWorkbook wb){
File file = null;
try {
FileOutputStream fos = new FileOutputStream("C:\\Excel" +
sheetNumber + ".xlsx");
wb.write(fos);
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace(); //To change body of catch statement use
File | Settings | File Templates.
}
Thanks a lot
--
View this message in context:
http://apache-poi.1045710.n5.nabble.com/Writing-XSSFWorkbook-and-is-empty-tp2553790p2553790.html
Sent from the POI - Dev mailing list archive at Nabble.com.
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]