Word 导入导出
创始人
2024-12-18 03:37:07
0

在实际的开发过程中,也会遇到导入导出的功能,今天就简单的做一下总结。

1.需求:将下面word 数据导入到数据库并进行存储

在Controller中

 

    @RequestMapping(value = "/ImportWord")     public @RawResponseBody     Object ImportWord(HttpServletRequest request, @RequestParam("file") MultipartFile file) throws IOException,         ParseException {         return gwyjglyjtzBizc.ImportWord(request, file);     }

在Service的实现类中,

 @Override     public Object ImportWord(HttpServletRequest request, MultipartFile file) throws ParseException {          boolean flag = false;         List rowList;         String userId = null;         String userName = null;         String deptId = null;         String deptName = null;         Map userMap = DeptUserUtils.getUserDept(request);         if (userMap instanceof Map) {             userId = ((Map)userMap).get("userId").toString();             userName = ((Map)userMap).get("userName").toString();             deptId = ((Map)userMap).get("deptId").toString();             deptName = ((Map)userMap).get("deptName").toString();         }         String xqlb1 = "";         String xqdj1 = "";         String yjbt1 = "";         try {             rowList = POIUtils.readYjtzWord(file);             GwYjglYjtz gwYjglYjtz = new GwYjglYjtz();              for (int i = 0; i < rowList.size(); i++) {                 if (rowList.get(i).equals("主送单位")) {                     gwYjglYjtz.setFbdw(rowList.get(++i));                 }                  if (rowList.get(i).contains("预警通知")) {                     yjbt1 += rowList.get(i);                 }                 if (rowList.get(i).contains("电缆预警")) {                     yjbt1 += rowList.get(i);                     gwYjglYjtz.setYjbt(yjbt1);                  }                 if (rowList.get(i).equals("险情类别")) {                     xqlb1 += rowList.get(++i) + ",";                     gwYjglYjtz.setXqlb(xqlb1);                 }                 if (rowList.get(i).equals("预警来源")) {                      gwYjglYjtz.setYjly(rowList.get(++i));                 }                 if (rowList.get(i).equals("预警级别")) {                     xqdj1 += rowList.get(++i) + ",";                     gwYjglYjtz.setXqdj(xqdj1);                 }                 if (rowList.get(i).contains("事件概要")) {                     String string = rowList.get(++i);                      gwYjglYjtz.setSjgy(string);                 }                 if (rowList.get(i).equals("要求")) {                     gwYjglYjtz.setYgcsyq(rowList.get(i + 1));                 }                 if (rowList.get(i).equals("影响时间")) {                     String string = rowList.get(++i);                     if (string.length() > 0) {                         String[] split1 = string.split("~");                         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");                         // 使用SimpleDateFormat来解析日期字符串                         SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");                         // 创建一个新的SimpleDateFormat来格式化输出                         SimpleDateFormat targetFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");                         int a = 0;                         for (String s : split1) {                             try {                                 Date date = originalFormat.parse(s);                                 // 转换并打印结果                                 String formattedDate = targetFormat.format(date);                                 System.out.println(formattedDate);                                 if (a == 0) {                                     gwYjglYjtz.setXxkssj(date);                                 } else {                                     gwYjglYjtz.setXxjssj(date);                                  }                                 a++;                              } catch (Exception e) {                                 e.printStackTrace();                             }                         }                     }                     gwYjglYjtz.setDjrid(userId);                     gwYjglYjtz.setDjr(userName);                     gwYjglYjtz.setDjdwid(deptId);                     gwYjglYjtz.setDjdw(deptName);                     gwYjglYjtz.setCreatetime(new Date());                 }              }             super.add(gwYjglYjtz);             flag = true;             System.out.print("导入word成功");         } catch (IOException e) {             // TODO Auto-generated catch block             e.printStackTrace();         }         return flag;     }

 在这里需要通过输入流的方式把里面的内容读取出来,然后存储到集合中。

    public static List readYjtzWord(MultipartFile formFile) throws IOException {         // 检查文件         checkDocFile(formFile);          InputStream fis = formFile.getInputStream();          XWPFDocument document = new XWPFDocument(fis);         // 创建返回对象,把每行中的值作为一个数组,所有的行作为一个集合返回         List list = new ArrayList();         // 遍历文档中的所有段落         List list1 = new ArrayList();          List paragraphs = document.getParagraphs();         for (XWPFParagraph paragraph : paragraphs) {             // 获取并打印段落文本             String text = paragraph.getText();             String trim = text.trim();             String[] lines = trim.split("\n", 2);              for (String s : lines) {                 list1.add(s);              }             System.out.println(trim);         }          // 如果需要处理表格,可以这样获取并遍历         List tables = document.getTables();         for (XWPFTable table : tables) {             for (XWPFTableRow row : table.getRows()) {                 for (XWPFTableCell cell : row.getTableCells()) {                     for (XWPFParagraph p : cell.getParagraphs()) {                         String text = p.getText().trim();                         list1.add(text);                         System.out.println(text);                     }                 }             }         }          // 关闭输入流         fis.close();          return list1;     }

 检查是否是指定文件格式

    public static void checkDocFile(MultipartFile formFile) throws IOException {         // 判断文件是否存在         if (null == formFile) {             logger.error("文件不存在!");             throw new FileNotFoundException("文件不存在!");         }         // 获得文件名         // String fileName = formFile.getName();         String fileName = formFile.getOriginalFilename();         // 判断文件是否是word文件         if (!fileName.endsWith(DOC) && !fileName.endsWith(DOCX)) {             logger.error(fileName + "不是word文件!");             throw new IOException(fileName + "不是word文件!");         }     }

导出功能

首先我们需要在word 指定对应的模版,对应的字段需要用占位符,这里需要跟数据库的字段需要保持一致或者是和Map 里面的key 。

最终模版制作完成后,保存时请注意,文件类型。一般是xml ,由于电脑的原因我选择的是html 格式。

用编辑器打开编码一定要选择utf-8,不然将来导出的时候是乱码。

编码格式改后,如果还是出现乱码在head 标签中加入一下内容

    

 引入对应的jar包

Controller

    @RequestMapping(value = "exportMillCertificate", method = RequestMethod.GET)     @ResponseBody     public void exportMillCertificate(HttpServletRequest request, HttpServletResponse response, @RequestParam(         value = "objId") String objId) throws Exception {         gwyjglyjtzBizc.exportMillCertificate(request, response, objId);     }

Service 的实现类

 @Override     public void exportMillCertificate(HttpServletRequest request, HttpServletResponse response, String objId)         throws Exception {         SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");         SimpleDateFormat originalFormat = new SimpleDateFormat("yyyy年MM月dd日HH时mm分");          // TODO Auto-generated method stub         String sql =             "select t.yjbt,t.fbdw,t.yjly,t.xqlb,t.xqdj,t.sjgy,t.ygcsyq,to_char(t.xxkssj,'yyyy-MM-dd hh24:mi:ss') as xxkssj,to_char(t.xxjssj,'yyyy-MM-dd hh24:mi:ss') as xxjssj  from  GW_GL_T t  where t.obj_id=?";         List> list = this.hibernateDao.queryForListWithSql(sql, new String[] {objId});         Map map0 = new HashMap();         Map map = new HashMap();         if (!Tool.isEmptyList(list)) {             map0 = list.get(0);             // 应急标题             if (Tool.isEmptyStr(map0.get("yjbt"))) {                 map.put("yjbt", "");             } else {                 map.put("yjbt", map0.get("yjbt"));             }             // 发布单位             if (Tool.isEmptyStr(map0.get("fbdw"))) {                 map.put("fbdw", "");             } else {                 map.put("fbdw", map0.get("fbdw"));             }             // 预警来源             if (Tool.isEmptyStr(map0.get("yjly"))) {                 map.put("yjly", "");             } else {                 map.put("yjly", map0.get("yjly"));             }             // 险情类别             if (Tool.isEmptyStr(map0.get("xqlb"))) {                 map.put("xqlb", "");             } else {                 map.put("xqlb", map0.get("xqlb"));             }             // 预警级别             if (Tool.isEmptyStr(map0.get("xqdj"))) {                 map.put("xqdj", "");             } else {                 map.put("xqdj", map0.get("xqdj"));             }             // 影响时间             if (Tool.isEmptyStr(map0.get("xxkssj"))) {                 map.put("yxsj", "");             } else {                 String string = map0.get("xxkssj");                 String string1 = map0.get("xxjssj");                 Date parse = sdf.parse(string);                 Date parse1 = sdf.parse(string1);                 String format1 = originalFormat.format(parse);                 String format2 = originalFormat.format(parse1);                 map.put("yxsj", format1 + "~" + format2);             }             // 事件概要             if (Tool.isEmptyStr(map0.get("sjgy"))) {                 map.put("sjgy", "");             } else {                 map.put("sjgy", map0.get("sjgy"));             }             // 有关措施要求             if (Tool.isEmptyStr(map0.get("ygcsyq"))) {                 map.put("ygcsyq", "");             } else {                 map.put("ygcsyq", map0.get("ygcsyq"));             }         } else {             map.put("yjbt", "");             map.put("fbdw", "");             map.put("yjly", "");             map.put("xqlb", "");             map.put("xqdj", "");             map.put("yxsj", "");             map.put("sjgy", "");             map.put("ygcsyq", "");          }         // map.put("yxsj", "");         String yjbt = map0.get("yjbt");         WordUtils.exportWord(request, response, map, "yjglyjtzmb.ftl", yjbt);

调用工具类的方法生成Word文档

    public static void exportWord(HttpServletRequest request, HttpServletResponse response, Map map,         String templateName, String fileName) throws IOException {         String pathString = request.getSession().getServletContext().getRealPath("/WEB-INF/templete/");         logger.info("获取到的模板路径是:templetePath------->" + pathString);         configuration.setDirectoryForTemplateLoading(new File(pathString));          Template freemarkerTemplate = configuration.getTemplate(templateName);         File file = null;         InputStream fin = null;         ServletOutputStream out = null;         try {             // 调用工具类的createDoc方法生成Word文档             file = createDoc(map, freemarkerTemplate);             fin = new FileInputStream(file);              response.setCharacterEncoding("utf-8");             response.setContentType("application/msword");             // 设置浏览器以下载的方式处理该文件名             // + DateUtil.currentDateToString()             fileName = fileName + ".doc";             response.setHeader("Content-Disposition",                 "attachment;filename=".concat(String.valueOf(URLEncoder.encode(fileName, "UTF-8"))));              out = response.getOutputStream();             byte[] buffer = new byte[512]; // 缓冲区             int bytesToRead = -1;             // 通过循环将读入的Word文件的内容输出到浏览器中             while ((bytesToRead = fin.read(buffer)) != -1) {                 out.write(buffer, 0, bytesToRead);             }         } finally {             if (fin != null)                 fin.close();             if (out != null)                 out.close();             if (file != null)                 file.delete(); // 删除临时文件         }      }
    private static File createDoc(Map dataMap, Template template) {         String name = ".doc";         File f = new File(name);         Template t = template;         try {             // 这个地方不能使用FileWriter因为需要指定编码类型否则生成的Word文档会因为有无法识别的编码而无法打开             Writer w = new OutputStreamWriter(new FileOutputStream(f), "utf-8");             t.process(dataMap, w);             w.close();         } catch (Exception ex) {             ex.printStackTrace();             throw new RuntimeException(ex);         }         return f;     }

最终效果图与导入的是一致里,今天就先到这,在开发过程中不一定会遇到这些,但是觉得还不错那就点赞收藏一波哈,万一哪天用到里。

相关内容

热门资讯

科普!微信炸金花房卡链接怎么弄... 微信游戏中心:炸金花房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信炸金花房...
分享!微信里面炸金花链接房卡/... 炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享...
金花房卡购买联系方式/金花斗牛... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:15984933许多玩家在游戏中会购买房卡来享受...
重大通报,牛牛房卡制作链接人人... 人人大厅房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...
ia攻略/金花房卡制作链接新西... ia攻略/金花房卡制作链接新西部/新鸿狐大厅/微信链接房卡销售购买新西部/新鸿狐大厅是一款非常受欢迎...
终于发现!微信里面拼三张房卡哪... 微信游戏中心:拼三张房卡,添加微信【66336574】,进入游戏中心或相关小程序,搜索“微信拼三张房...
正规平台有哪些,牛牛房卡怎么购... 蛮王大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:【3329006910】或QQ:332900...
科普!微信炸金花房卡链接房卡,... 微信游戏中心:炸金花房卡,添加微信【8488009】,进入游戏中心或相关小程序,搜索“微信炸金花房卡...
IA解析/斗牛房间怎么创建的新... IA解析/斗牛房间怎么创建的新荣耀/飞鹰互娱/微信链接房间卡怎么购买Sa9Ix苹果iPhone 17...
正版授权“金花房卡微信链接怎么... 悠悠大厅是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:160470940许多玩家在游戏中会购买房卡...
IA解析/金花房卡专卖店新九天... IA解析/金花房卡专卖店新九天大厅//全网房卡低价售新九天大厅是一款非常受欢迎的游戏,咨询房/卡添加...
科普!微信里炸金花房卡卖家联系... 微信游戏中心:炸金花房卡,添加微信【55051770】,进入游戏中心或相关小程序,搜索“微信炸金花房...
终于发现!微信里玩炸金花房卡在... 微信游戏中心:炸金花房卡,添加微信【71319951】,进入游戏中心或相关小程序,搜索“微信炸金花房...
分享!微信牛牛房卡链接去哪里买... 斗牛是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:33699510许多玩家在游戏中会购买房卡来享受...
推荐一款!金花房卡出售火星大厅... 微信游戏中心:火星大厅/新道游房卡在哪里买打开微信,添加客服微信【88355042】,进入游戏中心或...
IA解析/金花房卡批发玄灵大厅... IA解析/金花房卡批发玄灵大厅/上游房卡多少钱一张Sa9Ix苹果iPhone 17手机即将进入量产阶...
科普!微信炸金花房卡找谁买,拼... 微信游戏中心:拼三张房卡,添加微信【33903369】,进入游戏中心或相关小程序,搜索“微信拼三张房...
微信炸金花链接怎样弄/在哪里买... 微信炸金花是一款非常受欢迎的棋牌游戏,咨询房/卡添加微信:44346008许多玩家在游戏中会购买房卡...
终于发现!微信里面斗牛房卡在哪... 微信游戏中心:斗牛房卡,添加微信【56001354】,进入游戏中心或相关小程序,搜索“微信斗牛房卡”...
科技实测!金花房卡出售星驰娱乐... 星驰娱乐房卡更多详情添加微:33549083、 2、在商城页面中选择房卡选项。 3、根...