Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Okay I'm not sure exactly why you're having a problem but I think it's due to the way in which you are accessing the Workbook. Let's assume that the file you send down <code>file</code> is the location of the XLS workbook that you want to work with. The first thing you need to check is if that workbook exists because the POI's handling of existing v nonexistent workbooks is differnt. That is accomplished as such:</p> <pre><code>HSSFWorkbook wb; HSSFSheet sheet; if(file.exists()) {//The workbook has been created already wb = (HSSFWorkbook) WorkbookFactory.create(new FileInputStream(file));//Line 1 sheet = wb.getSheetAt(0); } else {//No workbook exists at the location the "file" specifies wb = new HSSFWorkbook(); sheet = wb.createSheet(); } </code></pre> <p>A few notes: <code>Line 1</code> throws 2 exceptions <code>IOException</code> from <code>java.io.IOException</code> and <code>InvalidFormatException</code> from <code>org.apache.poi.openxml4j.exceptions.InvalidFormatException</code>. So either throw the exceptions or surround with try-catch as you prefer.</p> <p>Now if file always exists, than the if-else statement isn't really needed. However, to properly open the desired workbook I would use the <code>WorkbookFactory</code>.</p> <p>As a final word, you can simplify your file saving by simply putting:</p> <pre><code>wb.write(new FileOutputStream("C:/juni1.xls"); </code></pre> <p>Notice that you are also writing the saved file to a different location. Therefore your original workbook is untouched while the corrected one is in a different place.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload