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>
本文介绍了一种使用Java和Struts2框架导出数据到Excel文件的方法。通过创建包含标题和数据的列表,利用HSSFWorkbook来生成Excel工作簿,并设置流以供下载。此方法适用于需要将数据库查询结果快速导出为Excel表格的应用场景。
1924

被折叠的 条评论
为什么被折叠?



