Skip to content

Apache poi

the Java API for Microsoft Documents
http://poi.apache.org/

Code samples

テンプレートExcelファイルを用意してデータを埋め込んでダウンロードするサンプル

FileInputStream fis = new FileInputStream( "C:\\TemplateBook.xlsx" );
Workbook workbook = WorkbookFactory.create( fis );
Sheet sheet = workbook.getSheet( "Sheet1" );
int i = 0;
for (Row row : sheet) {
    for (Cell cell : row) {
        cell.setCellValue( "_" + i + "_" );
        i++;
    }
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write( baos );
response.setContentType( "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" );
response.setHeader( "Content-Disposition", "attachment; filename=\"YourBook.xlsx\"" );
// :
// :
response.flushBuffer();
MIME type of .xlsx - Description of the default settings for the MimeMap property and for the ScriptMaps property in IIS