欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

word导入与导出后端代码

程序员文章站 2024-03-20 15:29:52
...

导入

/**
 * 导入word文档数据到数据库
 *
 * @param execl
 * @return
 */
@PostMapping("/saveWord")
@ResponseBody
public Map<String, Object> saveExport(@RequestParam("file") MultipartFile file) {
    //新建一个map集合返回对象
    Map<String, Object> map = new HashMap<>();
    try {
        XWPFDocument document = new XWPFDocument(file.getInputStream());
        XWPFParagraph para;
        Iterator<XWPFTable> itTable = document.getTablesIterator();
        int ind = 0;
        //new 一个新的对象
        Zwfxyjtj zwfxyjtj = new Zwfxyjtj();
        //新建时间解析器
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月d日 HH:mm");
        while (itTable.hasNext()) {
            ind++;
            XWPFTable table = (XWPFTable) itTable.next();
            //行
            int rcount = table.getNumberOfRows();
            for (int i = 0; i < rcount; i++) {
                XWPFTableRow row = table.getRow(i);
                //列
                List<XWPFTableCell> cells = row.getTableCells();
                int len = cells.size();
                for (int j = 0; j < len; j++) {
                    XWPFTableCell xc = cells.get(j);
                    String sc = xc.getText();
                    System.out.println("第" + ind + "个表格,第" + (i) + "行,第" + (j) + "列:" + sc);
                    //停电设备
                    if (ind == 1 && i == 0 && j == 1) {
                        zwfxyjtj.setPowerequipment(sc);
                    }
                    //告警时段
                    if (ind == 1 && i == 2 && j == 1) {
                        zwfxyjtj.setWarntime(simpleDateFormat.parse(sc));
                    }
                    //风险等级
                    if (ind == 1 && i == 3 && j == 1) {
                        zwfxyjtj.setRisklevel(sc);
                    }
                    //主送部门
                    if (ind == 1 && i == 4 && j == 1) {
                        zwfxyjtj.setSenddept(sc);
                    }
                    //责任单位
                    if (ind == 1 && i == 5 && j == 1) {
                        zwfxyjtj.setDutyunit(sc);
                    }
                    //风险分析
                    if (ind == 1 && i == 6 && j == 1) {
                        zwfxyjtj.setRiskanalysis(sc);
                    }
                    //管控措施
                    if (ind == 1 && i == 7 && j == 1) {
                        zwfxyjtj.setMeasures(sc);
                    }
                }

            }
        }
        System.out.println(zwfxyjtj);
        zwfxyjtjService.insertinto(zwfxyjtj);
        map.put("message", Constant.DEFAULT_SUCCESS_MSG);
    } catch (Exception e) {
        map.put("message", Constant.DEFAULT_FAILED_MSG);
    }
    return map;
}

导出

/**
 * 将统计作为word文档导出
 *
 * @param pageNumber
 * @param pageSize
 * @return
 */
@RequestMapping("exportWord")
@ResponseBody
public Map<String, Object> exportWord(HttpServletResponse response, Integer pageNumber, Integer pageSize, String startTimeStr, String endTimeStr, String risk, String url) throws Exception {

    //分页查询
    PageInfo<Zwfxyjtj> info = zwfxyjtjService.findPageListByState(pageNumber, pageSize, startTimeStr, endTimeStr, risk);
    long count = info.getTotal();

    //判断开始时间和结束时间情况
    if (StringUtils.isEmpty(startTimeStr)) {
        startTimeStr = zwfxyjtjService.getStartWeekDay();
    }
    if (StringUtils.isEmpty(endTimeStr)) {
        endTimeStr = zwfxyjtjService.getEndWeekDay();
    }


    //导出word
    XWPFDocument document = new XWPFDocument();
    try {
        //添加标题
        XWPFParagraph titleParagraph = document.createParagraph();
        //设置段落居中
        titleParagraph.setAlignment(ParagraphAlignment.CENTER);

        // 标题
        XWPFRun titleParagraphRun = titleParagraph.createRun();
        // 然后把你查出的数据插入到document中去就可以了
        titleParagraphRun.setText("风险预警信息统计");
        // 设置字体颜色
        titleParagraphRun.setColor("000000");
        // 设置字体大小
        titleParagraphRun.setFontSize(15);

        //段落1
        XWPFParagraph firstParagraph = document.createParagraph();
        XWPFRun run = firstParagraph.createRun();
        run.setText("开始时间:" + startTimeStr);
        run.setColor("000000");
        run.setFontSize(10);
        //换行
        XWPFParagraph paragraph1 = document.createParagraph();
        XWPFRun paragraphRun1 = paragraph1.createRun();
        paragraphRun1.setText("\r");
        //段落2
        XWPFParagraph SecondParagraph = document.createParagraph();
        XWPFRun run2 = SecondParagraph.createRun();
        run2.setText("结束时间:" + endTimeStr);
        run2.setColor("000000");
        run2.setFontSize(10);
        //换行
        XWPFParagraph paragraph3 = document.createParagraph();
        XWPFRun paragraphRun3 = paragraph3.createRun();
        paragraphRun3.setText("\r");
        //段落4
        XWPFParagraph FourParagraph = document.createParagraph();
        XWPFRun run4 = FourParagraph.createRun();
        run4.setText("风险次数:" + count);
        run4.setColor("000000");
        run4.setFontSize(10);

        //当风险查询条件不为空
        if (!StringUtils.isEmpty(risk)) {
            //换行
            XWPFParagraph paragraph2 = document.createParagraph();
            XWPFRun paragraphRun2 = paragraph2.createRun();
            paragraphRun2.setText("\r");
            //段落3
            XWPFParagraph ThreeParagraph = document.createParagraph();
            XWPFRun run3 = ThreeParagraph.createRun();
            run3.setText("风险等级:" + risk);
            run3.setColor("000000");
            run3.setFontSize(10);
        }


        SimpleDateFormat sdf = new SimpleDateFormat("MMddHHmmss");
        String fileName = new String("word导出".getBytes("UTF-8"), "iso-8859-1");
        new WordUtil().exportWord(document, response, fileName + sdf.format(new Date()));
    } catch (Exception e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
    System.out.println("导出成功!!!!");


    Map<String, Object> map = new HashMap<>();
    map.put("message", Constant.DEFAULT_SUCCESS_MSG);
    return map;
}

word模板

见上传