excel word 网页自动求和

根据excel word文件,生成网页录入,自动求和。
使用工具:jsoup、poi、summernote(前台富文本编辑器)
先写一个关闭输入输出流的方法

 /** 
     * 关闭输入流 
     * @param is 
     */  
    private void closeStream(InputStream is) {  
       if (is != null) {  
          try {  
             is.close();  
          } catch (IOException e) {  
             e.printStackTrace();  
          }  
       }  
    }  
    
    /** 
     * 关闭输出流 
     * @param os 
     */  
    private void closeStream(OutputStream os) {  
       if (os != null) {  
          try {  
             os.close();  
          } catch (IOException e) {  
             e.printStackTrace();  
          }  
       }  
    }

根据word生成html文件及字符串

    /** 
     *  
     * @param path 所在的文件夹 
     * @param inFileName 输入Word文档 
     * @return String
     * @throws Throwable 
     */  
    public String wordToHtml(String path) throws Throwable { 
        return hWPFToHtml(wordToHWPF(path));
    }
    
    /** 
     *  
     * @param path 所在的文件夹 
     * @param inFileName 输入Word文档 
     * @return HWPFDocument
     * @throws Throwable 
     */  
    public HWPFDocument wordToHWPF(String path) throws Throwable {  
        InputStream input = new FileInputStream(path);  
        HWPFDocument wordDocument = new HWPFDocument(input); 
        this.closeStream(input);
        return wordDocument;
    }
   
    /** 
     *  
     * @param wordDocument 输入Word文档 
     * @return String
     * @throws Throwable 
     */ 
    public String hWPFToHtml(HWPFDocument wordDocument) throws Throwable{ 
    	String path = "";   //想要生成html文件的物理地址
        WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());  
          
        wordToHtmlConverter.setPicturesManager(new PicturesManager() {  
            public String savePicture(byte[] content, PictureType pictureType,  
                    String suggestedName, float widthInches, float heightInches) {  
                return suggestedName;  
            }  
        });  
          
        wordToHtmlConverter.processDocument(wordDocument);  
        List<Picture> pics = wordDocument.getPicturesTable().getAllPictures();  
        if (pics != null) {  
            for (int i = 0; i < pics.size(); i++) {  
                Picture pic = (Picture) pics.get(i);  
                try {  
                    pic.writeImageContent(new FileOutputStream(path  
                            + pic.suggestFullFileName()));  
                } catch (FileNotFoundException e) {  
                    e.printStackTrace();  
                }  
            }  
        }  
          
        Document htmlDocument = wordToHtmlConverter.getDocument();  
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();  
        DOMSource domSource = new DOMSource(htmlDocument);  
        StreamResult streamResult = new StreamResult(outStream);  
  
        TransformerFactory tf = TransformerFactory.newInstance();  
        Transformer serializer = tf.newTransformer();  
        serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");  
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");  
        serializer.setOutputProperty(OutputKeys.METHOD, "html");  
        serializer.transform(domSource, streamResult);
        String content = new String(outStream.toByteArray());  
        /*FileUtils.write(new File(path, "test.html"), content, "utf-8");*/  //需要生成html文件解除注释
        this.closeStream(outStream);
        return content;
    } 



  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值