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

  }

}

 

posted @ 2020-02-25 14:56  790021521  阅读(478)  评论(0编辑  收藏  举报