Hello
I have an excel file that I manipulate with the following code:
public static void main(String[] args) {
try {
String path = args[0];
File excelTemplate = new File(path + "ny_resdirekt.xls");
File resultFile = new File(path + "resultat.xls");
POIFSFileSystem fs = new POIFSFileSystem(
new FileInputStream(excelTemplate));
//Not totally sure about the constructor
HSSFWorkbook wb = new HSSFWorkbook(fs,true);
HSSFSheet data = wb.getSheet("Indata");
HSSFRow row1 = data.getRow(4);
row1.removeCell(row1.getCell((short)2));
HSSFCell cell1 = row1.createCell((short)2);
cell1.setCellValue(2);
HSSFRow row2 = data.getRow(5);
row2.removeCell(row2.getCell((short)2));
HSSFCell cell2 = row2.createCell((short)2);
cell2.setCellValue(2);
FileOutputStream out = new FileOutputStream(resultFile);
//fs.writeFilesystem(out); Tried this but nothing happend
wb.write(out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
The excel file consists of three sheets, one with data, one with a diagram
built with data from the first sheet and then there is another one with no
clear purpose (haven't made the file myself). There is just text fields in
the third sheet. The data sheet is called "Indata". My idea is to modify the
data sheet with HSSF and then write the WorkBook to a new file and then I
should have a new, fine diagram. This is what the documentation says.
The problem is that when I try to open the new file in Excel it turns out
it's broken. I'm using Excel 2002 and it says the following (translated from
swedish) when I try to open the file:
1. The file cannot be read
2. The file cannot be read (same as the first alert)
3. There is errors in resultat.xls, but Excel could open the file by doing
the following repairings (and then it refers to a file that is mostly
empty).
The resulting file is opened but all formattings in the first and the third
sheets are gone and the diagram in the second sheet is gone.
What have I done wrong? Or am I trying to do something that is not
supported?
Do you need an excel file to analyze so just tell me.