美文网首页
2019-05-19 java使用POI将数据导出放入Exc

2019-05-19 java使用POI将数据导出放入Exc

作者: 于洋_dd44 | 来源:发表于2019-05-19 01:22 被阅读0次

本文主要是将数据库取出的数据按照自定义的行列格式导出到excel中,POI则是实现我们需求所用到的技术。

POI介绍

使用spring boot导入相关依赖

获取数据(自行处理)

完整代码实例:创建excel,将数据写入excel.

1.poi介绍

要想使用poi对Excel进行操作,我们需要了解一下Excel俩种版本:一种版本扩展名是“.xls;”XSSF:操作的是.xlsx。
不挂那种操作,基本思路一样,先要对应一个Excel文件,然后在对应文件中的某个sheet,接下来在操作某一行和这一行中的某一列。对应POI包:文件(webbook)、sheet(sheet)、行(row)、和单元格(cell).
详细操作请参照POI官网的Excel(HSSF/XSSF)操作

2.通过spring boot导入依赖

为了使用java操控excel,需要将相关的jar引入,对于HSSF只需要导入POI.jar,而XSSF则需要导入四个jar,具体导入见下面代码
将代码块的依赖放入工程的pom.xml文件中就可以了。
工程不是spring boot的需要手动将下面jar导入。

<!--poi依赖:导出excel-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>RELEASE</version>
        </dependency>

3.完整代码实例:简单的创建excel,将数据写入excel

依赖导入成功之后,就可以开始进行excel的生成。详细步骤在代码注释中有说明。

/**
 * 〈一句话功能简述〉<br> 
 * 〈Excel导出的工具类〉
 * 创建excel
 * @param
 * @author 13481
 * @create 2019/5/18
 * @since 1.0.0
 * @return
 */
public class ExportToExcel {
    @Autowired
    PublicPropertyDao publicPropertyDao;
    public XSSFWorkbook exportExcel(List<DefectMessage> listresult){
        //1.创建XSSFWorkbook,一个XSSFWorkbook对应的Excel文件
        XSSFWorkbook web=new XSSFWorkbook();
        //2.在work中添加一个sheet,对应excel文件中的sheet
        XSSFSheet sheet=web.createSheet("sheet1");
        //3.设置表头,即每个列的列名
        //创建第一行
        XSSFRow row=sheet.createRow(0);
        //此处创建一个序列号
        row.createCell(0).setCellValue("序号");
        //将列名写入
        String[] title={"缺陷编码","缺陷摘要","所属模块","所属系统","缺陷归属系统","所属功能","所属案例编号"
        ,"所属案例名称","缺陷类型","严重程度","优先级","可重现","修复次数","重打开次数","缺陷状态","缺陷类型再确定","所属计划"
        ,"创建时间","预计修复时间","创建人","分派至","缺陷描述","注释","确认时间","确认人","分派时间","分派人","修复时间"
        ,"修复人","关闭时间","关闭人","修改时间","修改人","测试环境","所属机构","所属任务","所属组"};
        for(int i=0;i<title.length;i++){
            row.createCell(i+1).setCellValue(title[i]);
        }
        //写入正式数据
        for(int i=0;i<listresult.size();i++){
            //创建行
            row=sheet.createRow(i+1);
            //创建序号
            row.createCell(0).setCellValue(i+1);
            //缺陷编码
            row.createCell(1).setCellValue(listresult.get(i).getCode());
            //缺陷摘要
            row.createCell(2).setCellValue(listresult.get(i).getSummary());
            //所属模块
            row.createCell(3).setCellValue(listresult.get(i).getBelongitem());
            //所属系统
            row.createCell(4).setCellValue(listresult.get(i).getSystem());
            //缺陷归属系统
            row.createCell(5).setCellValue(listresult.get(i).getBelongitem());
            /*//
            row.createCell(7).setCellValue(listresult.get(i).getBelongitemcode());*/
            //所属功能
            row.createCell(6).setCellValue(listresult.get(i).getBelongtradename());
            //所属案例编号
            row.createCell(7).setCellValue(listresult.get(i).getBelongitemcode());
            //所属案例名称
            row.createCell(8).setCellValue(listresult.get(i).getCasename());
            //缺陷类型
            row.createCell(9).setCellValue(listresult.get(i).getDefecttype());
            //严重程度
            row.createCell(10).setCellValue(listresult.get(i).getSeverity());
            //优先级
            row.createCell(11).setCellValue(listresult.get(i).getPriority());
            //可重现
            row.createCell(12).setCellValue(listresult.get(i).getReproducible());
            //修复次数
            row.createCell(13).setCellValue(listresult.get(i).getRepairtime());
            //重打开次数
            row.createCell(14).setCellValue(listresult.get(i).getRepairtimes());
            //缺陷状态
            row.createCell(15).setCellValue(listresult.get(i).getDefectstatus());
            //缺陷类型在确定
            row.createCell(16).setCellValue(listresult.get(i).getRecertaintype());
            //所属计划
            row.createCell(17).setCellValue(listresult.get(i).getTeststatus());
            //创建时间
            row.createCell(18).setCellValue(listresult.get(i).getCreatetime());
            //预计修复时间
            row.createCell(19).setCellValue(listresult.get(i).getPrerepairtime());
            //创建人
            row.createCell(20).setCellValue(listresult.get(i).getCreateperson());
            //分派至
            row.createCell(21).setCellValue(listresult.get(i).getAssignto());
            //缺陷描述
            row.createCell(22).setCellValue(listresult.get(i).getDefectdescribe());
            //注释
            row.createCell(23).setCellValue(listresult.get(i).getDefectnote());
            //确认时间
            row.createCell(24).setCellValue(listresult.get(i).getConfirmtime());
            //确认人
            row.createCell(25).setCellValue(listresult.get(i).getConfirmperson());
            //分派时间
            row.createCell(26).setCellValue(listresult.get(i).getDividetime());
            //分派人
            row.createCell(27).setCellValue(listresult.get(i).getDivideperson());
            //修复时间
            row.createCell(28).setCellValue(listresult.get(i).getRepairtime());
            //修复人
            row.createCell(29).setCellValue(listresult.get(i).getRepairperson());
            //关闭时间
            row.createCell(30).setCellValue(listresult.get(i).getClosetime());
            //关闭人
            row.createCell(31).setCellValue(listresult.get(i).getCloseperson());
            //修改时间
            row.createCell(32).setCellValue(listresult.get(i).getModifytime());
            //修改人
            row.createCell(33).setCellValue(listresult.get(i).getModifyperson());
            //测试环境
            row.createCell(34).setCellValue(listresult.get(i).getVersion());
            //所属机构
            row.createCell(35).setCellValue(listresult.get(i).getDefect());
            //所属任务
            row.createCell(36).setCellValue(listresult.get(i).getBelonginstitutionname());
            //所属组
            row.createCell(37).setCellValue(listresult.get(i).getUsergroup());
        }
/**
 * 上面的操作已经是生成一个完整的文件了,只需要将生成的流转换成文件即可;
 * 下面的设置宽度可有可无,对整体影响不大
 */
        // 设置单元格宽度
        int curColWidth = 0;
        for (int i = 0; i <= title.length; i++) {
            // 列自适应宽度,对于中文半角不友好,如果列内包含中文需要对包含中文的重新设置。
            sheet.autoSizeColumn(i, true);
            // 为每一列设置一个最小值,方便中文显示
            curColWidth = sheet.getColumnWidth(i);
            if(curColWidth<2500){
                sheet.setColumnWidth(i, 2500);
            }
            // 第3列文字较多,设置较大点。
            sheet.setColumnWidth(3, 8000);
        }
        return web;

    }
}

接下来完成控制层数据的生成,然后调用工具类,就OK了。

public Map ExporttToCsv(String format,String rows,HttpServletRequest request) throws IOException {
 Map resultMap = new HashMap();
        ExportToExcel exportToExcel = new ExportToExcel();
    List<DefectMessage> list=JSONArray.parseArray(rows,DefectMessage.class);
            //1生成Excel
            XSSFWorkbook defectListExcel=exportToExcel.exportExcel(list);
            try{
                // 输出成文件
                File file=new File(".");
                String path=file.getCanonicalPath();
                String ys=path+"/DefectMessage.xlsx";
                // TODO 生成的wb对象传输
                FileOutputStream outputStream = new FileOutputStream(new File(ys));
                defectListExcel.write(outputStream);
                outputStream.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            resultMap.put("success", true);
}

相关文章

网友评论

      本文标题:2019-05-19 java使用POI将数据导出放入Exc

      本文链接:https://www.haomeiwen.com/subject/klxfzqtx.html