excel 导出

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

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值