zoukankan      html  css  js  c++  java
  • 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交流。

  • 相关阅读:
    Bone Collector II(背包 求第k优解)
    How Many Answers Are Wrong (彻底理解带权并查集)
    System Testing(Codeforces Round #543 (Div. 2, based on Technocup 2019 Final Round))D
    Rikka with Minimum Spanning Trees徐州区域赛a题
    cin加速语句
    牛客练习赛41 b 666RPG (01背包)
    js字符串拼接优化
    nodejs中使用递归案例
    thunk的主要用法
    $in的方法总结
  • 原文地址:https://www.cnblogs.com/Hawk-cyc/p/9094535.html
Copyright © 2011-2022 走看看