SSH框架通过poi导出excel表格

struts配置文件

<result name="outputPaper" type="stream">
<param name="inputStream">excelStream</param>
<param name="ContentType">application/vnd.ms-excel</param>
<param name="contentDisposition">filename="paper.xls"</param>       //导出的excel表格文件名
</result>

action代码:

private InputStream inputStream;

public InputStream getInputStream() {
return inputStream;
}
public void setInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}

方法体代码:

/**
*
* @param achievements 导出的数据集
* @param xlsName 导出excel文件名
* @return
*/

public static InputStream paperOutput(List<Achievement> achievements, String xlsName){
HSSFWorkbook workbook = new HSSFWorkbook();
// 第二步,在webbook中添加一个sheet,对应Excel文件中的sheet
HSSFSheet sheet = workbook.createSheet("论文");
// 第三步,在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
HSSFRow row = sheet.createRow((int)0);
// 第四步,创建单元格,并设置值表头 设置表头居中
HSSFCellStyle style = workbook.createCellStyle();
style.setAlignment(HSSFCellStyle.ALIGN_CENTER); //创建一个居中样式
//写列名,视自己的需求而定
HSSFCell cell = row.createCell(0);
cell.setCellValue("编号");
cell.setCellStyle(style);
cell = row.createCell(1);
cell.setCellValue("论文题目");
cell.setCellStyle(style);
cell = row.createCell(2);
cell.setCellValue("发表时间");
cell.setCellStyle(style);
cell = row.createCell(3);
cell.setCellValue("期刊名称");
cell.setCellStyle(style);
cell = row.createCell(4);
cell.setCellValue("版次");
cell.setCellStyle(style);
cell = row.createCell(5);
cell.setCellValue("类别");
cell.setCellStyle(style);
cell = row.createCell(6);
cell.setCellValue("状态");
cell.setCellStyle(style);

// 第五步,写入实体数据
for(int i=0;i<achievements.size();i++){
row = sheet.createRow((int)i+1);
Achievement achievement = achievements.get(i);
String time = achievement.getTime().toString().substring(0, 10);
HSSFCell cell2 = row.createCell(0);
cell2.setCellValue(achievement.getId());
cell2.setCellStyle(style);
cell2 = row.createCell(1);
cell2.setCellValue(achievement.getTitle());
cell2.setCellStyle(style);
cell2 = row.createCell(2);
cell2.setCellValue(time);
cell2.setCellStyle(style);
cell2 = row.createCell(3);
cell2.setCellValue(achievement.getMagazineName());
cell2.setCellStyle(style);
cell2 = row.createCell(4);
cell2.setCellValue(achievement.getEdition());
cell2.setCellStyle(style);
cell2 = row.createCell(5);
cell2.setCellValue(achievement.getType());
cell2.setCellStyle(style);
cell2 = row.createCell(6);
cell2.setCellValue(achievement.getStatus());
cell2.setCellStyle(style);
}
//自动设置EXCEL的列宽,视自己的需求而定,也可以用sheet.setDefaultColumnWidth(13);为全部列的列宽设置默认值
sheet.setDefaultColumnWidth(13);
// sheet.autoSizeColumn((short)0);
// sheet.autoSizeColumn((short)2);
// sheet.autoSizeColumn((short)6);
// sheet.autoSizeColumn((short)7);
// sheet.autoSizeColumn((short)8);
// sheet.autoSizeColumn((short)9);
// sheet.autoSizeColumn((short)10);

//设置文件名,用格式化日期来生成一个ID
String filePath = "";
Date dt = new Date();
DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");
String date = df.format(dt).toString();
filePath = "paper" + date + ".xls";
File file = new File(filePath);
try{
OutputStream out = new FileOutputStream(file);
workbook.write(out);
out.close();
}catch(Exception e){
e.printStackTrace();
}
InputStream in = null;
try{
in = new FileInputStream(file);
}catch(Exception e){
e.printStackTrace();
}
return in;
}

public String outputPaper(){
HttpSession session=ServletActionContext.getRequest().getSession();
List<Achievement> achievements = (List<Achievement>) session.getAttribute("paperes");    //要导出的数据集
setInputStream(ModelExport.paperOutput(achievements, "paper"));
return "outputExcel";
}

 

相应poi包网盘链接https://pan.baidu.com/s/1Sq1U_VR7M4Y_cKPkZCDmJw

密码:lxws

有不懂的可加wechat:CL1050366731交流。

转载于:https://www.cnblogs.com/Hawk-cyc/p/9094535.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值