1
I need to edit an XLS file already created, but I am only able to create a new file and delete the old one, so far it is working but I wonder if it is possible to edit the old file and not have to delete the old one and create a new one with the same name.
String autEdit = request.getParameter("aut");
response.setContentType("text/html;charset=UTF-8");
File file = new File("C:\\Tratados\\4-600dpi.xls");
Workbook workbook = null;
try {
workbook = Workbook.getWorkbook(new File("C:\\teste.xls"));
} catch (BiffException ex) {
Logger.getLogger(autorizacao.class.getName()).log(Level.SEVERE, null, ex);
}
WritableWorkbook copy = Workbook.createWorkbook(new File("C:\\Teste.xls"), workbook);
Sheet sheet = workbook.getSheet(0);
int linhas = sheet.getRows();
int i = 0, x = 0;
WritableSheet sheet2 = copy.getSheet(0);
for (i = x; i < linhas; i++) {
Cell a1 = sheet.getCell(0, i);
WritableCell a2 = sheet2.getWritableCell(1, i);
WritableCell a3 = sheet2.getWritableCell(2, i);
String as1 = a1.getContents();
String as2 = a2.getContents();
String as3 = a3.getContents();
if ("-".equals(as2)){
Label l = (Label) a2;
l.setString(autEdit);
Label ll = (Label) a3;
ll.setString("Editado");
linhas = i - 1;
}
}
copy.write();
file.delete();
try {
copy.close();
} catch (WriteException ex) {
Logger.getLogger(autorizacao.class.getName()).log(Level.SEVERE, null, ex);
}
workbook.close();
WorkbookFactory
is giving an error saying that would need to create a class in the package, would not only have to place the import or theWorkbookFactory
not included in JXL library which is the one I am using– R.Santos
So, the solution I posted is using Apache POI, which I consider a better and more up-to-date library to work with excel in Java. You are using Jexcelapi probably why you are giving error.
– hebertrfreitas
Ah right I will try to download this library then and test your solution? You would happen to have a download link from that library?
– R.Santos
Browse this page https://poi.apache.org/download.html It’s pretty similar to the one you’re using, find examples of it on this link: https://poi.apache.org/spreadsheet/quick-guide.html
– hebertrfreitas