实例介绍
【实例简介】
【实例截图】
【实例截图】
【核心代码】
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; } */ }
好例子网口号:伸出你的我的手 — 分享!
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论