Microsoft purposefully obfuscates their code. If you want to populate a .xls or .xlsx you need some help.  The Apache POI project is exactly what you need.  

Libraries required include

  • poi
  • poi-ooxml
  • poi-ooxml-schemas
  • xbean
  • dom4j

This project takes Input.xlsx and writes a value of 13 to a cell and outputs the file as Output.xlsx

Source code & libraries here


/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package excelpoi;

import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
 
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.WorkbookFactory;                                            // This is included in poi-ooxml-3.6-20091214.jar
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;

import org.apache.poi.xssf.usermodel.XSSFWorkbook;



/**
 *
 * @author <a href="http://www.amarketplaceofideas.com/freemon-sandlewould-is-my-liberal-moniker.htm"  class="alinks_links" onclick="return alinks_click(this);" title="Freemon recycles sandles if you would.  Let's get high and contemplate the difference between an orange."  style="padding-right: 13px; background: url(http://www.amarketplaceofideas.com/wp-content/plugins/alinks/images/external.png) center right no-repeat;" rel="external">Freemon Sandlewould</a>
 */
public class ExcelPOI {
    
    public static void main(String[] args)
  {
    try
    {
      Workbook wbook = new XSSFWorkbook();
      wbook = WorkbookFactory.create(new FileInputStream(&quot;Input.xlsx&quot;) );
      Sheet wsheet = wbook.getSheetAt(0);                                                    // Does not work with getSheetAt0) -&gt; works with 1! strange
 
     
      //Row row = sheet.getRow(10);                                                          // Set value of the first cell.
      //Cell cell = row.getCell(10);
      //Cell cell=row.getCell(10, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK );
      
      //Row row = sheet.createRow((short)10);
      //Cell cell = row.createCell((short)15);     
      //cell.setCellValue(&quot;fudge&quot;);
      

      Row row = wsheet.getRow(7);
      //if (row == null) {row=wsheet.createRow(10);}
      Cell cell = row.getCell(7);
      //if (cell == null){cell = row.createCell(10);}
      
      System.out.println( &quot;before set cellvalue= &quot; + cell.getNumericCellValue());
      //System.out.println( &quot;before set cellvalue= &quot; + cell.getStringCellValue());

      //cell.setCellType(Cell.CELL_TYPE_NUMERIC);
      //cell.setCellValue(7); 
      cell.setCellValue(13); 
      System.out.println( &quot;after set cellvalue= &quot; + cell.getNumericCellValue());
      //System.out.println( &quot;after set cellvalue= &quot; + cell.getStringCellValue());
      
      //FileOutputStream fileOut = new FileOutputStream(&quot;OutputFile.xls&quot;);                // Write newly modified workbook to a file.      
      FileOutputStream fileOut = new FileOutputStream(&quot;OutputFile.xlsx&quot;);
      wbook.write(fileOut);
      fileOut.close();
      System.out.println(&quot;DONE!&quot;);
    }
    catch(FileNotFoundException e)
    {
      System.out.println(e);
    }
    catch(IOException e)
    {
      System.out.println(e);
    }
    catch(InvalidFormatException e)
    {
      System.out.println(e);
    }
 
  }


    /**
     * @param args the command line arguments
     */

}



1 Comment

Freemon SandleWould MonsterID Icon Freemon SandleWould · July 8, 2014 at 5:18 am

The best is CKEditor

Leave a Reply

Your email address will not be published. Required fields are marked *