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();

      }

    }

  • 相关阅读:
    javascript中数据类型转换那些事
    CSS布局奇淫技巧之高度自适应
    用innerHTML插入html代码中有样式表时需注意的地方
    详解ASP.NET Core API 的Get和Post请求使用方式
    Speex 一个开源的声学回声消除器(Acoustic Echo Cancellation)(转)
    c# int byte转换
    Linux操作系统内核源码目录结构详解
    Linux/Ubuntu sudo不用输入密码的方法
    DirectX简介
    在VC工程中添加多语言支持
  • 原文地址:https://www.cnblogs.com/bwl914/p/12361705.html
Copyright © 2011-2022 走看看