Wicket で jakarta-POI で生成したExcel2007 をWebアプリでダウンロードさせる時の ContentType は、
application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Wicket の例では、ダウンロードのボタンの onSubmit() に
org.apache.wicket.util.resource.IResourceStream 実装として
public String getContentType() の戻り値にこれを返すようにする。
拡張子xls の Excel2003 でも可能だった。
http://www.microsoft.com/japan/technet/prodtechnol/office/ork/library/f88d06fb-c9a4-413c-a1d3-40c97e340c5a.mspx?mfr=true
必要なJAR は、以下。
poi-3.6-20091214.jar
poi-ooxml-3.6-20091214.jar
poi-ooxml-schemes-3.6-20091214.jar
dom4j-1.6.1.jar
xmlbeans-2.3.0.jar
------------------------ 実装例 ------------------------------
public class ExcelTestPage extends WebPage{
public ExcelTestPage(){
Form<Void> form = new Form<Void>("downloadForm");
add(form);
final TextField<String> message = new TextField<String>("message"
,new Model<String>("サンプル"));
form.add(message);
Button download = new Button("download"){
@Override
public void onSubmit(){
IResourceStream resource = new AbstractResourceStreamWriter(){
@Override
public void write(OutputStream output){
String templateName = "template.xlsx";
InputStream input = ExcelPage.class.getResourceAsStream(templateName);
try{
Workbook workbook = WorkbookFactory.create(input);
Sheet sheet = workbook.getSheetAt(0);
this.writeCell(sheet,0,0,message.getDefaultModelObjectAsString());
}catch(InvalidFormatException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
}
@Override
public String getContentType() {
return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
private void writeCell(Sheet sheet,int rownum,int colnum,String data){
Row row = sheet.getRow(rownum);
if (row==null) row = sheet.createRow(rownum);
Cell cell = row.getCell(colnum);
if (cell==null) cell = row.createCell(colnum);
cell.setCellValue(data);
cell.setCellStyle(this.cellstyle);
}
};
String downloadName = "download.xlsx";
IRequestTarget target = new ResourceStreamRequestTarget(resource,downloadName);
getRequestCycle().setRequestTarget(target);
}
};
form.add(download);
}
}