java using poi read word document stored in the database

poi jar package need to download their own use

read word document contains multiple pictures, so it is divided into two parts, one to read the contents of each table, one interception all pictures:

   / ** * traverse the contents of paragraphs 
     * docxReadPath document address 
     * uploadPic image upload address 
     * picFile save the picture after the address 
     * @param the Document 
     * @return XWPFDocument 
     * @throws IOException 
      * / 
    public  static String readPar (XWPFDocument the Document, docxReadPath String, String uploadPic, picFile String) { 
         String Fail = "Sucess" ; 
     the Iterator <XWPFParagraph> itPara document.getParagraphsIterator = ();
the try {
  // read the contents of the word in all
        the while (itPara.hasNext ()) {
      
        XWPFParagraph = paragraph (XWPFParagraph) itPara. next ();

          // run represents the same area of the same character attributes, results ',' separated;
          List <XWPFRun> = paragraph.getRuns the runs (); // paragraph.getRuns ();
          String fileName = "";
          for (int I = 0 ; i <runs.size (); i ++) {

            String oneparaString = runs.get(i).getText(runs.get(i).getTextPosition());

             System.out.println(oneparaString);

          }

       }
              List<XWPFPictureData> picList = document.getAllPictures();for (XWPFPictureData pic : picList) {
              byte[] bytev = pic.getData();
              String imgName=pic.getFileName();                           
        System.out.println("=====图片生成中========"+imgName); if(!"image1.jpeg".equals(imgName)){ FileOutputStream fos = new FileOutputStream(uploadPic+"/"+imgName); fos.write(bytev); } } } catch (Exception e) { e.printStackTrace(); System.out.println("=====错误信息===="+e.getMessage()); fail="false"; } return fail; }
 /**
     * 遍历所有表格的内容
     * @param document
     * @throws FileNotFoundException 
     */
    public static void readTableContent(XWPFDocument document) {
        Iterator<XWPFTable> itTable = document.getTablesIterator();
        int ind = 0;
        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+1) +"行,第"+ (j+1) +"列:" +sc);
                }
            }
        }
    }
 /**
     * 读取文件
     * @param srcPath
     * @return XWPFDocument
     */
    public static XWPFDocument read_file(String srcPath)
    {
        String[] sp = srcPath.split("\\.");
        if ((sp.length > 0) && sp[sp.length - 1].equalsIgnoreCase("docx"))
        {
            try {
                 FileInputStream fis = new FileInputStream(srcPath);
                  XWPFDocument xdoc = new XWPFDocument(fis);
                  XWPFWordExtractor extractor = new XWPFWordExtractor(xdoc);
                
               // OPCPackage pack = POIXMLDocument.openPackage(srcPath);
               // XWPFDocument doc = new XWPFDocument(pack);
                return xdoc;
            } catch (IOException e) {
                System.out.println("读取文件出错!");
                e.printStackTrace();
                return null;
            }
        }
        return null;
    }
 
  
public static void main(String[] args) throws IOException{
        String  docx = "F:\\bb.docx";
        XWPFDocument document = read_file(docx);
        readPar(document);
        readTableContent(document);
    }

 

 
 
 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/xl1314666/p/11898273.html