Populate an Excel xlsx xls spread sheet with Apache POI Library
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
<br />
</p>
</p>
<p>
<p><br />
/*<br /><br />
* To change this template, choose Tools | Templates<br /><br />
* and open the template in the editor.<br /><br />
*/<br /><br />
package excelpoi;<br />
</p>
</p>
<p>
<p><br />
import java.io.FileOutputStream;<br /><br />
import java.io.FileInputStream;<br /><br />
import java.io.FileNotFoundException;<br /><br />
import java.io.IOException;<br />
</p>
</p>
<p>
<p><br />
import org.apache.poi.ss.usermodel.Sheet;<br /><br />
import org.apache.poi.ss.usermodel.Row;<br /><br />
import org.apache.poi.ss.usermodel.Cell;<br /><br />
import org.apache.poi.ss.usermodel.WorkbookFactory; // This is included in poi-ooxml-3.6-20091214.jar<br /><br />
import org.apache.poi.ss.usermodel.Workbook;<br /><br />
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;<br />
</p>
</p>
<p>
<p><br />
import org.apache.poi.xssf.usermodel.XSSFWorkbook;<br />
</p>
</p>
<p>
<p><br />
/**<br /><br />
*<br /><br />
* @author Freemon Sandlewould<br /><br />
*/<br /><br />
public class ExcelPOI {<br />
</p>
</p>
<p>
<p><br />
public static void main(String[] args)<br /><br />
{<br /><br />
try<br /><br />
{<br /><br />
Workbook wbook = new XSSFWorkbook();<br /><br />
wbook = WorkbookFactory.create(new FileInputStream(&quot;Input.xlsx&quot;) );<br /><br />
Sheet wsheet = wbook.getSheetAt(0); // Does not work with getSheetAt0) -&gt; works with 1! strange<br />
</p>
</p>
<p>
<p><br />
//Row row = sheet.getRow(10); // Set value of the first cell.<br /><br />
//Cell cell = row.getCell(10);<br /><br />
//Cell cell=row.getCell(10, org.apache.poi.ss.usermodel.Row.CREATE_NULL_AS_BLANK );<br />
</p>
</p>
<p>
<p><br />
//Row row = sheet.createRow((short)10);<br /><br />
//Cell cell = row.createCell((short)15);<br /><br />
//cell.setCellValue(&quot;fudge&quot;);<br />
</p>
</p>
<p>
<p><br />
Row row = wsheet.getRow(7);<br /><br />
//if (row == null) {row=wsheet.createRow(10);}<br /><br />
Cell cell = row.getCell(7);<br /><br />
//if (cell == null){cell = row.createCell(10);}<br />
</p>
</p>
<p>
<p><br />
System.out.println( &quot;before set cellvalue= &quot; + cell.getNumericCellValue());<br /><br />
//System.out.println( &quot;before set cellvalue= &quot; + cell.getStringCellValue());<br />
</p>
</p>
<p>
<p><br />
//cell.setCellType(Cell.CELL_TYPE_NUMERIC);<br /><br />
//cell.setCellValue(7);<br /><br />
cell.setCellValue(13);<br /><br />
System.out.println( &quot;after set cellvalue= &quot; + cell.getNumericCellValue());<br /><br />
//System.out.println( &quot;after set cellvalue= &quot; + cell.getStringCellValue());<br />
</p>
</p>
<p>
<p><br />
//FileOutputStream fileOut = new FileOutputStream(&quot;OutputFile.xls&quot;); // Write newly modified workbook to a file.<br /><br />
FileOutputStream fileOut = new FileOutputStream(&quot;OutputFile.xlsx&quot;);<br /><br />
wbook.write(fileOut);<br /><br />
fileOut.close();<br /><br />
System.out.println(&quot;DONE!&quot;);<br /><br />
}<br /><br />
catch(FileNotFoundException e)<br /><br />
{<br /><br />
System.out.println(e);<br /><br />
}<br /><br />
catch(IOException e)<br /><br />
{<br /><br />
System.out.println(e);<br /><br />
}<br /><br />
catch(InvalidFormatException e)<br /><br />
{<br /><br />
System.out.println(e);<br /><br />
}<br />
</p>
</p>
<p>
<p><br />
}<br />
</p>
</p>
<p>
<p><br />
/**<br /><br />
* @param args the command line arguments<br /><br />
*/<br />
</p>
</p>
<p>
<p><br />
}<br />
</p>
</p>
<p>
<p><br />
Java Programming Language Table of Contents
- Java POI Poorly Obfuscated Interface Project to Load a SpreadSheet with Data
- Java File Operations with Text Box – Load and Save Text you type in
- Java in Eclipse Integrated Development Environment IDE
- Java Software Internationalization
- Java Multilistener – Event Demo that shows how you can listen to one or to all
- Java Serial Port
- Java – Set the Size of a FrameView programmatically
- Populate an Excel xlsx xls spread sheet with Apache POI Library
End TOC

1 Comment
Freemon SandleWould · July 8, 2014 at 5:18 am
The best is CKEditor