基本信息
源码名称:html转word
源码大小:4.37KB
文件格式:.java
开发语言:Java
更新时间:2015-12-18
   源码介绍


package com.hyx.util;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Picture;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

public class HtmlToWord {

	/*public static void main(String[] args) {
=======
 	public static void main(String[] args) {
>>>>>>> .r6740
		// TODO Auto-generated method stub
		try {
			//"E:\\wsww.doc"
			  //String tem_content = "<html>  "   oracleJdbc()  " </html>";
			//exportWord(tem_content ,"E:\\wsww.doc");
			insertImgToWord("","E:\\upload\\demo.doc");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	} 
	
	/**
	 * 将html代码导出成word
	 * @param htmlContent html 脚本字符串 需要以<html>开头  </html>结尾
	 * @param filePath    word存放路径
	 * @throws Exception
	 */
    public boolean exportWord(String htmlContent,String filePath) throws Exception {  
    	boolean flag = true;
        try {     
                byte b[] = htmlContent.getBytes();    
                ByteArrayInputStream bais = new ByteArrayInputStream(b);    
                POIFSFileSystem poifs = new POIFSFileSystem();    
                DirectoryEntry directory = poifs.getRoot();    
                DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);   
                //输出到本地文件的话,new一个文件流  
                FileOutputStream ostream = new FileOutputStream(filePath);    
                poifs.writeFilesystem(ostream);    
                bais.close();    
                ostream.close();   
               
        } catch (IOException e) {  
        	flag = false;
            //e.printStackTrace();    
        }  
        return flag;
    }
    /**
     * 
     * @param imgFile  图片路径
     * @param filePath  word 文档路径
     * @return
     * @throws IOException 
     * @throws FileNotFoundException 
     */
    private static boolean insertImgToWord(String imgFile,String filePath) throws FileNotFoundException, IOException{
    	  
         imgFile= "E:\\test.jpg";  
        
          HWPFDocument poiDoc = new HWPFDocument(new FileInputStream(filePath));  
          List picList=poiDoc.getPicturesTable().getAllPictures();  
          Picture picture=(Picture)picList.get(0);  
               try {  
                       picture.writeImageContent(new FileOutputStream(imgFile));  
                 } catch (FileNotFoundException e) {  
                         e.printStackTrace();      
           }   
           return true;
    }
    
    
    /*
    
 private static String oracleJdbc() throws ClassNotFoundException, SQLException{
    	
        Connection con = null;// 创建一个数据库连接
        PreparedStatement pre = null;// 创建预编译语句对象,一般都是用这个而不用Statement
        ResultSet result = null;// 创建一个结果集对象
        
        Class.forName("oracle.jdbc.driver.OracleDriver");// 加载Oracle驱动程序
        System.out.println("开始尝试连接数据库!");
        String url = "jdbc:oracle:"   "thin:@127.0.0.1:1521:orcl";// 127.0.0.1是本机地址,XE是精简版Oracle的默认数据库名
        String user = "C##NEWT";// 用户名,系统默认的账户名
        String password = "123456";// 你安装时选设置的密码
        con = DriverManager.getConnection(url, user, password);// 获取连接
        System.out.println("连接成功!");
        String sql = "select tem_content from tb_templet where templet_id='1'";// 预编译语句,“?”代表参数
        pre = con.prepareStatement(sql);// 实例化预编译语句
        result = pre.executeQuery();// 执行查询,注意括号中不需要再加参数
    	TypeToType tyto = new TypeToType();
    	String temcontent = null;
	
        while (result.next())
        	try {
    			temcontent = tyto.ClobToString( result.getClob("tem_content"));
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
		return temcontent;
    }
    */

}