- 浏览: 185026 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
xiaoqiangcn:
-->使用Spring测试套件,Spring容器只会初始 ...
spring junit完美解决方案 -
huomiam:
wei5201 写道ynyee 写道@ContextConfi ...
spring junit完美解决方案 -
ae6623:
@Test
//@Ignore
public void ...
集合操作工具类CollectionUtils -
hejun5918:
java.lang.NoSuchMethodError: or ...
spring junit完美解决方案 -
u010100704:
垃圾垃圾垃圾垃圾垃圾垃圾垃圾垃圾垃圾
实用的json工具类gson
把html导出为excel或者word文档
1.jsp页面简单导出
导出为excel:
导出为word:
2.pdf 导出
步骤1: 写出html模板 muban.html
步骤2: 写出 Pdf 类,这个类用于生成你想要的pdf文件,包括往模板内填充数据,以及导出!
步骤3:测试类PdfTest
最后,jar包,放出供各位下载!
3.由于最近的项目只有导出word功能,没有导出pdf功能,而又不想再做导出pdf的工作,于是使用word直接转换成pdf的方式获得pdf文件
提供2种方案:
3.1 使用jacob.不过有使用前提,必须先把jar包中的dll文件放到jre/bin下.同时安装adobe acrobat7.0以上版本的软件.再配置下adobe的虚拟打印机,即可.之后运行java类,代码很简单,如下(拷贝自别人):
3.2 但是由于公司服务器在linux上跑,jacob不适用,于是使用openOffice+jodconverter,实现很不错哦!不想再写字了,直接上工程吧,呵呵!
有些前提限制,原版说明如下:
附件中有的下载!提供io流的方式转换pdf!自创!
1.jsp页面简单导出
导出为excel:
<% response.setContentType("application/vnd.ms-excel;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition","attachment;filename=test.xls"); //这里的filename 如果是中文的话,需要先把中文转成utf8编码 %>
导出为word:
<% response.setContentType("application/msword;charset=UTF-8"); response.setHeader("Content-Disposition","attachment;filename=test.doc"); %>
2.pdf 导出
步骤1: 写出html模板 muban.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <title>无标题文档</title> <style type="text/css"> div *{font-size:10px;} </style> </head> <body style="width:900px; margin:auto ;padding-top:15px;"> <table cellspacing="0" border="1" cellpadding="0" width="80%"> <tr> <td width="45%"> <p id="post">#邮政编码</p> <p id="addr">#地址</p> <p id="addr2">#具体地址(几号楼几单元几室)</p> <p userName="userName">#userName 亲启</p> </td> <td > <img src="ad.jpg" /> </td> </tr> </table> <h3 style="padding-left:280px;">债权转让及受让协议</h3> <div> <p userName="userName">尊敬的 #userName 先生 ,您好!</p> <p id="date">通过宜信公司信用评估与筛选,推荐您 #date 通过受让他人既有的个人间借贷合同的方式,出借资金给如下借款人,</p> <p>详见《债权列表》,在您接受该批债权转让并按时支付对价的情况下,预期您的出借获益情况如下: 货币单位:人民币(元)</p> <table cellspacing="0" border="1" cellpadding="0" > <tr id="lenderAcountInfo"> <th style='width:100px;align:center;'>出借编号</th> <th style='width:100px;align:center;'>资金出借及<br/>回收方式</th> <th style='width:100px;align:center;'>初始出借日期</th> <th style='width:100px;align:center;'>初始出借金额</th> <th style='width:100px;align:center;'>下一个报告日</th> <th style='width:100px;align:center;'>下一个报告期借款人<br/>应还款额</th> <th style='width:100px;align:center;'>账户管理费</th> <th style='width:100px;align:center;'>预计下一个报告日您的<br/>资产总额</th> </tr> </table> </div> </body> </html>
步骤2: 写出 Pdf 类,这个类用于生成你想要的pdf文件,包括往模板内填充数据,以及导出!
import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.OutputStream; import org.jsoup.Jsoup; import org.jsoup.nodes.Document; import org.jsoup.nodes.Element; import org.jsoup.select.Elements; import org.xhtmlrenderer.pdf.ITextRenderer; public class Pdf { private Document document; private String fileTemplete; public Pdf() { super(); } /** * 通过构造方法获取pdf类的基本模板样式 * @param fileTemplete * @throws Exception */ public Pdf(String fileTemplete) throws Exception { super(); //初始化document document = this.getHTMLTemplete(fileTemplete); } public void createPdf() throws Exception { String outputFile = "test.pdf"; OutputStream os = new FileOutputStream(outputFile); ITextRenderer renderer = new ITextRenderer(); //得到最终html对象 String html = document.html(); //设置pdf内容 renderer.setDocumentFromString(html); // 解决图片的相对路径问题 renderer.getSharedContext().setBaseURL("file:/E:/"); //设置pdf样式 renderer.layout(); //最终生成pdf renderer.createPDF(os); os.close(); System.out.println("生成成功!"); } /** * 获取模板 * @param templeteName * @return * @throws Exception */ public Document getHTMLTemplete(String templeteName) throws Exception { String path = Test.class.getResource("/").getPath(); File file = new File(path + templeteName); Document document = Jsoup.parse(file, "GBK"); return document; } /** * 设置用户名 * @param userName */ public void setUserName(String userName) { Elements es = document.getElementsByAttribute("userName"); for(Element e:es){ String s=e.html(); String[] text=s.split("#userName"); if(text.length==2){ s=text[0]+userName+text[1]; }else if(text.length==1) s=userName+text[0]; e.html(s); } } /** * 设置邮编 * @param post */ public void setPost(String post) { Element e = document.getElementById("post"); e.html(post); } /** * 设置地址 * @param addr */ public void setAddr( String addr) { Element e = document.getElementById("addr"); e.html(addr); } /** * 设置详细地址 * @param addr2 */ public void setAddr2( String addr2) { Element e = document.getElementById("addr2"); e.html(addr2); } /** * 设置日期 * @param date */ public void setDate( String date) { Element e = document.getElementById("date"); String s=e.html(); String[] text=s.split("#date"); if(text.length==2){ s=text[0]+date+text[1]; }else if(text.length==1) s=date+text[0]; e.html(s); } /** * 设置出借人账户收益信息 * @param info */ public void setLenderAccountInfo(String info){ Element e = document.getElementById("lenderAcountInfo"); e.after(info); } public Document getDocument() { return document; } public void setDocument(Document document) { this.document = document; } public String getFileTemplete() { return fileTemplete; } public void setFileTemplete(String fileTemplete) { this.fileTemplete = fileTemplete; } }
步骤3:测试类PdfTest
import junit.framework.TestCase; public class PdfTest extends TestCase { @Override protected void setUp() throws Exception { super.setUp(); } @Override protected void tearDown() throws Exception { super.tearDown(); } public void testPdf() throws Exception{ Pdf pdf=new Pdf("/muban.html"); //使用哪个模板 //以下部分为往模板内填数据 pdf.setPost("100000"); pdf.setUserName("张伟");//填用户名 pdf.setAddr("北京市朝阳区红领巾南路朝阳园");//设置地址 pdf.setAddr2("9号楼1单元801室");//设置具体地址 pdf.setDate("2010-08-10");//设置日期参数 String info="<tr>" + "<td>8808010100029781-005</td>" + "<td >宜信宝</td>" + "<td>宜信宝2010-08-12</td>" + "<td>1,200,000.00</td>" + "<td>2010-08-30</td>" + "<td>64,368.82</td>" + "<td>0.00</td>" + "<td>1,207,499.14</td>" + "</tr>"; pdf.setLenderAccountInfo(info); //生成pdf pdf.createPdf(); } }
最后,jar包,放出供各位下载!
3.由于最近的项目只有导出word功能,没有导出pdf功能,而又不想再做导出pdf的工作,于是使用word直接转换成pdf的方式获得pdf文件
提供2种方案:
3.1 使用jacob.不过有使用前提,必须先把jar包中的dll文件放到jre/bin下.同时安装adobe acrobat7.0以上版本的软件.再配置下adobe的虚拟打印机,即可.之后运行java类,代码很简单,如下(拷贝自别人):
/** *//** * * @version 1.00, 2007-4-9 * */ import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; /* * 注意word转pdf要安装虚拟打印机,且要配置 * 使用jacob框架,把dll文件放到jre/bin目录下 */ public class WordToPdf { private ActiveXComponent wordCom = null; private Object wordDoc = null; private final Variant False = new Variant(false); private final Variant True = new Variant(true); /** * 打开word文档 * * @param filePath word文档 * @return 返回word文档对象 */ public boolean openWord(String filePath) { //建立ActiveX部件 wordCom = new ActiveXComponent("Word.Application"); try { //返回wrdCom.Documents的Dispatch Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch(); //调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method, new Object[] { filePath }, new int[1]).toDispatch(); return true; } catch (Exception ex) { ex.printStackTrace(); } return false; } /** * 关闭word文档 */ public void closeWord(boolean saveOnExit) { if (wordCom!=null) { //关闭word文件 //Dispatch.call(wordDoc, "Close", new Variant(saveOnExit)); wordCom.invoke("Quit",new Variant[]{}); //wordCom.invoke("Quit",new Variant[0]); wordCom=null; //释放在程序线程中引用的其它com,比如Adobe PDFDistiller ComThread.Release(); } } /** * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件 * * @param sourceFilePath * 源文件路径 * @param destinPSFilePath * 首先生成的PS文件路径 * @param destinPDFFilePath * 生成PDF文件路径 */ public void docToPDF(String sourceFilePath, String destinPSFilePath, String destinPDFFilePath) { if (!openWord(sourceFilePath)) { closeWord(true); return; } //建立Adobe Distiller的com对象 ActiveXComponent distiller = new ActiveXComponent("PDFDistiller.PDFDistiller.1"); try { //设置当前使用的打印机,我的Adobe Distiller打印机名字为 "Adobe PDF" wordCom.setProperty("ActivePrinter", new Variant("Adobe PDF")); //设置printout的参数,将word文档打印为postscript文档。现在只使用了前5个参数,假如要使用更多的话可以参考MSDN的office开发相关api //是否在后台运行 Variant Background = False; //是否追加打印 Variant Append = False; //打印所有文档 int wdPrintAllDocument = 0; Variant Range = new Variant(wdPrintAllDocument); //输出的postscript文件的路径 Variant OutputFileName = new Variant(destinPSFilePath); Dispatch.callN((Dispatch) wordDoc, "PrintOut", new Variant[] { Background, Append, Range, OutputFileName }); System.out.println("由word文档打印为ps文档成功!"); //调用Distiller对象的FileToPDF方法所用的参数,具体内容参考Distiller Api手册 //作为输入的ps文档路径 Variant inputPostScriptFilePath = new Variant(destinPSFilePath); //作为输出的pdf文档的路径 Variant outputPDFFilePath = new Variant(destinPDFFilePath); //定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件 Variant PDFOption = new Variant(""); //调用FileToPDF方法将ps文档转换为pdf文档 Dispatch.callN(distiller, "FileToPDF", new Variant[] { inputPostScriptFilePath, outputPDFFilePath, PDFOption }); System.out.println("由ps文档转换为pdf文档成功!"); } catch (Exception ex) { ex.printStackTrace(); } finally { closeWord(true); } } public static void main(String[] argv) { WordToPdf d2p = new WordToPdf(); d2p.docToPDF("f:\\1043_王燕春_lender账单_2010-06-15_1.doc", "f:\\1043_王燕春_lender账单_2010-06-15_1.ps", "f:\\1043_王燕春_lender账单_2010-06-15_1.pdf"); // boolean success = (new File("D:\\test.ps")).delete(); // if(success){ // System.out.println("删除打印机文件成功"); // } } }
3.2 但是由于公司服务器在linux上跑,jacob不适用,于是使用openOffice+jodconverter,实现很不错哦!不想再写字了,直接上工程吧,呵呵!
有些前提限制,原版说明如下:
JODConverter needs to connect to a running OpenOffice.org instance in order to perform the document conversions. This is different from starting the OpenOffice.org program as you would normally do. OpenOffice.org can be configured to run as a service and listen for commands on a TCP port; there are a few ways to accomplish this but the simplest one is to start it from the command line with the following options: soffice -headless -accept="socket,host=127.0.0.1,port=8100;urp;" -nofirststartwizard
附件中有的下载!提供io流的方式转换pdf!自创!
- jar.rar (2.6 MB)
- 描述: 根据html模板导出pdf所需jar包
- 下载次数: 5
- JODConverterDoc2PDF.rar (3.3 MB)
- 描述: linux下word转pdf
- 下载次数: 4
发表评论
-
关于跨应用事务
2011-12-23 14:01 1327在项目中对于事务控制有如下3种: 1.简单的单项目,单服务,单 ... -
浅谈电信三户模型
2011-12-23 13:48 3118三户模型是一个成熟的经典模型,客户、账户和用户的模型关系应用在 ... -
网页性能优化心得
2011-10-20 16:14 2227页面性能优化的的几大 ... -
jquery 插件ajaxupload 的简单应用
2011-08-26 14:27 11783同事 一次需要ajax 上传某文件, 正好以前用过一种,叫aj ... -
web开发中远程接口的改观用法
2011-08-15 20:46 1860工作中,使用webService 以及 rmi 进行远程接口调 ... -
eclipse插件安装,刷新命令等
2011-02-24 10:39 27601、把插件解压到插件安 ... -
eclipse property插件,适合于eclispe3.1版本
2010-12-30 15:38 1599在做国际化编辑一些简体中文、繁体中文等Unicode资源文件时 ... -
eclipse中的代码注释格式
2010-12-21 13:42 1410设置注释模板的入口: ... -
生成压缩文件(把文件加到压缩包中的简便方法)
2010-08-27 13:41 1194import org.apache.tools.zip ... -
通过request获取真实客户ip
2010-08-12 09:54 2200项目中需要和第三方平台接口,加了来源IP鉴权功能,测试时发现没 ...
相关推荐
用其它机器上的与试验机不同的系统的同名同类型文件替换,结果在系统进入到用户切换界面时,出现提示如下:一个问题阻止Windows正确检查此机器的许可证,错误代码:0X80070002;换成原文件后,正常起动。 11、删除...
这里有一个文件类型,我们就选择HTML,然后在EXPORT FORMAT这里我们就暂时先不管,先选择short,然后我们点击OK,就可以生成一份非常漂亮的根据这个REPORT转化的HTML文档,但是如果每个脚本都这样导出会显得十分麻烦...
在对象创建的过程中将被容器调用,onMessage函数方法接收消息参数,将其强制转型为合适的消息类型,同时打印出消息的内容。同时一个mail note将被发送给消息发送者,发送一个e-mail通知给由recipient参数确定的e-...
2、添加自定义调用外部编辑器编辑功能(文本类型文件); 3、修正修改节点(文件夹)备注会损坏节点图标问题; 4、添加记忆悬浮窗位置功能; 5、修正收集图片时收集窗体无法最前端功能(IE7); 6、加快调用QQ抓图速度; 7...
Java从网络取得文件 1个目标文件 简单 Java从压缩包中提取文件 1个目标文件 简单 Java存储与读取对象 1个目标文件 如题 Java调色板面板源代码 1个目标文件 摘要:Java源码,窗体界面,调色板 使用Java语言编写的一款...
2)对于独立安装包:(相对于单个程序文件,还包含了先前的各种自定义配置文件) 第一步:双击运行安装程序Section2010.exe,打开安装向导页面开始安装Section软件; 第二步:仔细阅读“许可协议”,点击“同意”,...
该工具目前涉及到的功能个个都比Spy++更好些,所以暂时就不写冗长的帮助文档了,几个地方简单说明一下。 1. 关于 [文件] 菜单下 "保存窗体内部文本" 功能。 该功能需要根据窗体类型来判断如何读取文本,目前仅...
对未知类型的文件也可以选择以TXT的方式打开了 修正三星用户时间不准的BUG 修正SE/S40用户背景灯关闭时闪屏的BUG 修正阅读TXT时潜在的致命BUG(感谢Sword-Light的指正) 用户可配置4种偏好,以分别在白天/晚上/室内/...
13.4.2 使用mount加载一个导出的文 件系统 238 13.4.3 卸载文件系统 238 13.4.4 配置/etc/fstab文件以自动加载 文件系统 239 13.5 完成样本配置文件 240 13.6 小结 241 第14章 Samba 242 14.1 安装Samba 242 14.2 ...
本书共分23章,内容涵盖了Windows操作系统的基本原理、NT驱动程序与WDM驱动程序的构造、驱动程序中的同步异步处理方法、驱动程序中即插即用功能、驱动程序的各种调试技巧等。同时,还针对流行的PCI驱动程序、USB驱动...
3.7.2 简单的INF文件剖析 3.8 WDM设备安装在注册表中的变化 3.8.1 硬件子键 3.8.2 类子键 3.8.3 服务子键 3.9 小结 第4章 驱动程序的基本结构 本章首先对Windows驱动程序的两个重要数据结构...
1.暂时取消试题导出功能(目前的试题导出存在部分问题,后续会重新增加) 2.主观题评分增加js段判断分值是否合法 3.试题库管理增加题库题量统计显示 4.考试项目增加及格分数选项 5.考试情况页面增加及格率显示 6....
6-3-2 用简单查询向导产生查询 6-3-3 用设计视图建立查询 6-3-4 修改查询条件 6-3-5 在查询中建立计算字段 6-3-6 限定查询的数据量 6-3-7 建立多表的查询 6-3-8 表的查询与暂时性关联 6-4 高级查询技巧 6-4-...
6-3-2 用简单查询向导产生查询 6-3-3 用设计视图建立查询 6-3-4 修改查询条件 6-3-5 在查询中建立计算字段 6-3-6 限定查询的数据量 6-3-7 建立多表的查询 6-3-8 表的查询与暂时性关联 6-4 高级查询技巧 6-4-...
做个一个库定义数据类型的方法,每个方法的第一个参数为指向该对象的结构指针 ========== 3.简单说下易源码 ========== !!!易保存源码时没有保存各信息名称(比如命令名、数据类型名),而是保存了索引 !!!...
导出cvs格式的表格,通过excel打开时,中文会显示乱码,因为bugzill是utf8的编码,在excel中不支持,就是把csv文件转成ascii码就能正常显示的,具体是将cvs文档用记事本或utraledit打开,另存为,格式中选择"ANSI/...