public static InputStream ExportExcel(Map<String,Object> map){
List<String[]> exportList=(List<String[]>)map.get("context");
String[] titleArray=(String[])map.get("title");
InputStream inputStream=null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
HSSFWorkbook workbook = new HSSFWorkbook();
try {
HSSFSheet sheet = workbook.createSheet("sheet1");
HSSFRow row = sheet.createRow((short)0);
//写入title
for(int c=0;c<titleArray.length;c++){
row .createCell((short)c).setCellValue(titleArray[c]);
}
for (int i = 0; i <exportList.size(); i++){
HSSFRow beanRow = sheet.createRow((short)(i+1));
String[] export = exportList.get(i);
for(int c=0;c<export.length;c++){
beanRow.createCell((short)c).setCellValue(export[c]);
}
}
workbook.write(baos);
baos.flush();
inputStream = new ByteArrayInputStream(baos.toByteArray());
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
baos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return inputStream;
}
配置
<action name="" class="" method="">
<result name="success" type="stream">
<param name="contentType">application/vnd.ms-excel</param>
<param name="inputName">inputStream</param>
<param name="contentDisposition">attachment;filename=${outputName}</param>
<param name="bufferSize">10000</param>
</result>
</action>