title: Jexcel - Updating an excel with data
body: <p>I've tried using <a href="http://www.andykhan.com/jexcelapi/index.html" rel="nofollow">Jexcel</a> </p>
  
  <p>to update an existing excel sheet as discussed in the Vogella <a href="http://www.vogella.com/articles/JavaExcel/" rel="nofollow">tutorial</a>.</p>
  
  <p>Issue here is the data already present in the existing excel sheet is wiped out with the newly written excel data.</p>
  
  <p>For example if I've this in the excel</p>
  
  <pre><code>&lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;3&lt;/td&gt; &lt;td&gt;4 &lt;/td&gt;
  &lt;/tr&gt;
  &lt;table&gt;
  </code></pre>
  
  <p>and I want to add data to the new cell next to 2 and 4, like</p>
  
  <pre><code>&lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt;1&lt;/td&gt; &lt;td&gt;2 &lt;/td&gt; &lt;td&gt;X &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt;3&lt;/td&gt; &lt;td&gt;4 &lt;/td&gt; &lt;td&gt;Y &lt;/td&gt;
  &lt;/tr&gt;
  &lt;table&gt;
  </code></pre>
  
  <p>after the write program executed this is what I get</p>
  
  <pre><code>&lt;table&gt;
  &lt;tr&gt;
  &lt;td&gt; &lt;/td&gt; &lt;td&gt;  &lt;/td&gt; &lt;td&gt;X &lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
  &lt;td&gt; &lt;/td&gt; &lt;td&gt;  &lt;/td&gt; &lt;td&gt;Y &lt;/td&gt;
  &lt;/tr&gt;
  &lt;table&gt;
  
  Label label;
  label = new Label(column, row, s, times);
  sheet.addCell(label);
  </code></pre>
  
  <p>This is adding the cell at the specified column and row but wiping out the rest of the excel data.</p>
  
  <p>How can I add data to the existing excel keeping the data?</p>
  
  <p>Following is the program(Reference: Vogella).  Excel sheet already has data in 20 rows and
  first 2 columns, am trying to add data on the 3rd column for 20 rows
      package excel;</p>
  
  <pre><code>import java.io.File;
  import java.io.IOException;
  import java.util.Locale;
  
  import jxl.CellView;
  import jxl.Workbook;
  import jxl.WorkbookSettings;
  import jxl.format.UnderlineStyle;
  import jxl.write.Label;
  import jxl.write.WritableCellFormat;
  import jxl.write.WritableFont;
  import jxl.write.WritableSheet;
  import jxl.write.WritableWorkbook;
  import jxl.write.WriteException;
  import jxl.write.biff.RowsExceededException;
  
  public class WriteExcel {
  
      private WritableCellFormat timesBoldUnderline;
      private WritableCellFormat times;
      private String inputFile;
  
      public void setOutputFile(String inputFile) {
          this.inputFile = inputFile;
      }
  
      public void write() throws IOException, WriteException {
          File file = new File(inputFile);
          WorkbookSettings wbSettings = new WorkbookSettings();
  
          wbSettings.setLocale(new Locale("en", "EN"));
  
          WritableWorkbook workbook = Workbook.createWorkbook(file, wbSettings);
          workbook.createSheet("Report", 0);
          WritableSheet excelSheet = workbook.getSheet(0);
          createLabel(excelSheet);
          createContent(excelSheet);
  
          workbook.write();
          workbook.close();
      }
  
      private void createLabel(WritableSheet sheet) throws WriteException {
          // Lets create a times font
          WritableFont times10pt = new WritableFont(WritableFont.TIMES, 10);
          // Define the cell format
          times = new WritableCellFormat(times10pt);
          // Lets automatically wrap the cells
          times.setWrap(true);
  
          // Create create a bold font with unterlines
          WritableFont times10ptBoldUnderline = new WritableFont(
                  WritableFont.TIMES, 10, WritableFont.BOLD, false,
                  UnderlineStyle.SINGLE);
          timesBoldUnderline = new WritableCellFormat(times10ptBoldUnderline);
          // Lets automatically wrap the cells
          timesBoldUnderline.setWrap(true);
  
          CellView cv = new CellView();
          cv.setFormat(times);
          cv.setFormat(timesBoldUnderline);
          cv.setAutosize(true);
  
      }
  
      private void createContent(WritableSheet sheet) throws WriteException,
              RowsExceededException {
  
          Integer salary = 1000;
          // Now a bit of text
          for (int i = 0; i &lt; 20; i++) {
              // third column
              addLabel(sheet, 2, i, salary.toString());
  
              // WritableCell cell = sheet.getWritableCell(2, i);
              // if (cell.getType() == CellType.LABEL) {
              // Label l = (Label) cell;
              // l.setString("modified cell");
              // }
              salary += 1000;
          }
  
      }
  
      private void addLabel(WritableSheet sheet, int column, int row, String s)
              throws WriteException, RowsExceededException {
          Label label;
          label = new Label(column, row, s, times);
          sheet.addCell(label);
      }
  
      public static void main(String[] args) throws WriteException, IOException {
          WriteExcel test = new WriteExcel();
          test.setOutputFile("c:/temp/lars.xls");
          test.write();
          System.out
                  .println("Please check the result file under c:/temp/lars.xls ");
      }
  }
  </code></pre>