Hello,
In the past, i have had no problems using FileInputStream and OutputStream to copy files. This time is different even though I have copied the code from previous programs.
public void JapanesestudiesExcel(String Japaneseword){ //creates a new workbook File f = new File("C:\\Users\\Jamal\\Desktop\\Japaneseword1.xls"); Workbook wb = new HSSFWorkbook(); if (!f.exists());{ try { Sheet sheet1 = wb.createSheet("Japaneseword"); sheet1.createRow(0); FileOutputStream fileOut = new FileOutputStream(f); wb.write(fileOut); fileOut.close(); } catch (Exception e) { throw new IllegalStateException(e.getMessage()); } } try { System.out.println(f.getAbsoluteFile()); // FileInputStream input = new FileInputStream(f); FileInputStream inp = new FileInputStream("C:\\Users\\Jamal\\Desktop\\Japaneseword1.xls"); Workbook workbook = new HSSFWorkbook(inp); FileOutputStream output = new FileOutputStream("C:\\Users\\Jamal\\Desktop\\Japaneseword2.xls"); Sheet sheet2 = workbook.getSheet("Japaneseword"); int num = sheet2.getLastRowNum(); num=++; System.out.print(num); Row row = sheet2.createRow(num); Cell cell = row.createCell(0); cell.setCellValue(Japaneseword); Cell cell2 = row.createCell(1); cell2.setCellValue(Japaneseword); Cell cell3 = row.createCell(2); cell3.setCellValue(Japaneseword); Cell cell4 = row.createCell(3); cell4.setCellValue(Japaneseword); Cell cell5 = row.createCell(4); cell5.setCellValue(Japaneseword); Cell cellwebsite = row.createCell(5); workbook.write(output); } catch(Exception e){ e.printStackTrace(); } } }
The purpose of this code is just to save the contents,add a row, and add the user's input to the excel file. I used InputStream to attempt the save the previous entries,then add new entries, and use the outputstream to print out excel file. I have done this in the past with no problems, but for some reason inputstream is not saving correctly. I am very frustrated at this problem. Would love to know why this is happening.
Thanks.
--- Update ---
Okay, update, for some reason or another, it works occasionally. See if I create the file, using the above method it won't copy correctly. But if I change the file name in the constructor of the outputstream method to a different name on the excel document to something arbitrary it starts counting the rows. It then saves the data correctly and works as it is intended. Does this make sense?
Basically, after I create the file, I then have to go back, change the code, then it saves and copys the data, and I can continue to edit the file. This solution is not practical. I want to be able to send this file to Father in Law in Japan, but it would be nice for the program to work without me sending an excel document as an attachment.
Thanks in advance.