zoukankan      html  css  js  c++  java
  • word导入功能

    //service层

    /**

     *导入word

    **/

    public JSONObject ImportWord(String fileType,String fileName, String title, ServiceRequest request) throws Exception;

    //impl层

    public JSONObject ImportWord(String fileType,String fileName, String title, ServiceRequest request) throws Exception {

      JSONObject result = new JSONObject();

      //存放wrod路径

      String property = FilePathUtil.getDirPath("xxx/xxx");

      File file0 = new File(property);

      if(!file0.exists()){

        file0.mkdir();

      }

      String[] split0 = title.split("\.",-1);//切割原文件名

      long time= new Date().getTime();

      String newPath = file0+"/"+time+"."+split[1];

      String split = fileName.split("\.",-1);

      //临时路径

      String realPath = AttachmentUtil.getUploadTempPath()+"/"+split[0]+"/"+split[1]+"."+split[2];

      FileUtils.copyFile(new File(realPath),new File(newPath));

      Map analysisWord = analysisWord(request,newPath);

      Object object = analysisWord.get("msg");

      result.put("msg",object);

      return result;

    }

    /**

     *解析Word,该方法只能解析doc(03版)格式的word文件

    * 与Excel不同,Word实现导入导出,03版和07版差异较大。实现思路是:如果03版报异常,那就是用07版的解决办法。03版的解析相对简单。07版更复杂一下。

    * 创建03版doc对象:HWPFDocument doc = new HWPFDocument(xxx)    其中,xxx可以为文件流或者File对象,根据是不是需要保存为文件来自行选择。

    * 创建07版doc对象:XWPFDocument docx = new XWPFDocument(xxx)      这里xxx同03版一样。

    **/

    public Map analysisWord (ServiceRequest request,String newPath) thows Exception{

      Map map = new HashMap();

      YoungInfoVO  vo = new YoungInfoVO ();

      File file = new File(newPath);

      String name = file.getName();

      String docContent = null;

      try {

        FileInputStream fis = new FileInputStream(file);

        if(name.toLowerCase().endsWith("doc")){

          HWPFDocument doc = new HWPFDocument(fis);

          docContent= doc.getDocumentText();

          if(docContent  ==null  || docContent.length() == 0){

            map.put("msg","word文档内无数据”):

            return map;

          }

          Range range = doc.getRange();

          TableIterator it = new TableIterator(range):

          while(it.hasNext()){

            Table tb = (Table)it.next;

            int numRows = tb.numRows();

            if(numRows<2) {

              map.put("msg","word文档内无数据”):

              return map;

            }

            for(int a=1;a<tb.numRows():a++) {

              TableCell td = tr.getCell(i);//获取 word中的a+1行的数据;

              for(int i=0;i<tr.numCells();i++) {

                TableCell td = tr.getCells(i);//获取单元格数据;

                String val = "";

                  for(int j=0;j<t.numCells();j++) {

                    Paragraph para = td.getParagraPh(j);//获取第j个字符;

                    val += para.text();

                    val = val.replaceAll(String.valueOf((char)7),"");

                    //set实体属性,存入数据库;

                    //vo.set...

                  }

              }

              int i = youngInfoDAO.insertVO(vo);

              if(i  != 0 ){

                map.put("msg","导入数据成功");

              }

            }

          }

        }

      } catch (Exception e) {

        map.put("msg","导入数据出错“);

        e.printStackTrace();

      }

    }

  • 相关阅读:
    【小白学PyTorch】1 搭建一个超简单的网络
    【小白学PyTorch】2 浅谈训练集和测试集
    【小白学AI】GBDT梯度提升详解
    【小白学AI】XGBoost推导详解与牛顿法
    【小白写论文】技术性论文结构剖析
    小白学PyTorch 动态图与静态图的浅显理解
    【小白学推荐1】 协同过滤 零基础到入门
    【小白学AI】随机森林 全解 (从bagging到variance)
    OpenCV开发笔记(七十二):红胖子8分钟带你使用opencv+dnn+tensorFlow识别物体
    【python刷题】二叉搜索树-相关题目
  • 原文地址:https://www.cnblogs.com/bwl914/p/12361705.html
Copyright © 2011-2022 走看看