
1.java怎么由html生成word,保留html样式
@RequestMapping("download")
public void exportWord( HttpServletRequest request, HttpServletResponse response)
throws Exception {
User user = AppContext.getLoginUser();
Student student = studentSvc.findByUserId(user.getId());
try {
//word内容
String content="<html><body></body></html>";
byte b[] = content.getBytes("utf-8"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
/*
* 关键地方
* 生成word格式
*/
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
//输出文件
String fileName="实习考核鉴定表";
request.setCharacterEncoding("utf-8");
response.setContentType("application/msword");//导出word格式
response.addHeader("Content-Disposition", "attachment;filename=" +
new String( (fileName + ".doc").getBytes(),
"iso-8859-1"));
OutputStream ostream = response.getOutputStream();
poifs.writeFilesystem(ostream);
bais.close();
ostream.close();
}catch(Exception e){
AppUtils.logError("导出出错:%s", e.getMessage());
}
}
2.请教java html导出word如何实现
java将html导出word不用忘记这对标签 //换页//换行
3.Java程序把Word文档直接换成Html文件方法是什么呢
acob是Java和Windows下的Com桥,通过它我们可以在Java程序中调用COM组件。
如果你的JDK是1。4,那你需要下载Jacob 1。
9的jni库才能正常运行,早期版本在JDK 1。4下有些问题。
以下是引用片段:package com;/*** 〈p〉Title:Word文档转html类〈/p〉* 〈p〉Description: 〈/p〉* 〈p〉Copyright:() 2002〈/p〉* @author 舵手* @version 1。 0*/import com。
jacob。com。
*;import com。jacob。
activeX。*;public class WordtoHtml {/***文档转换函数*@param docfile word文档的绝对路径加文件名(包含扩展名)*@param htmlfile 转换后的html文件绝对路径和文件名(不含扩展名)*/public static void change(String docfile, String htmlfile) {ActiveXComponent app = new ActiveXComponent("Word。
Application"); // 启动wordtry {app。setProperty("Visible", new Variant(false));//设置word不可见Object docs = app。
getProperty("Documents")。 toDispatch();Object doc = Dispatch。
invoke(docs,"Open",Dispatch。Method,new Object[]{ docfile, new Variant(false),new Variant(true) }, new int[1])。
toDispatch();// 打开word文件Dispatch。invoke(doc, "SaveAs", Dispatch。
Method, new Object[] {htmlfile,new Variant (8) }, new int[1]);// 作为html格式保存到临时文件Variant f = new Variant(false);Dispatch。 call(doc, "Close", f);} catch (Exception e) {e。
printStackTrace();} finally {app。invoke("Quit", new Variant[]{});}}public static void main(String[] strs){WordtoHtml。
change("c:\\a\\运输管理调度系统总体方案。doc", "c:\\a\\t");}}。
4.怎样用Java把word文档转换为html文档
在线学习的话应该是B/S模式吧,如果楼主是想将我word内容连同样式一起转换成html有两种方法
一种是手动将要上传的word文件另存为html文件,并将html文件传进服务器,由浏览器打开就行
另一种是使用控件将要上传的word文件内容转成html代码,现在较好的控件有FCKeditor,eWebEditor,前者免费,后者精简版免费,商业版支持直接上传word文件转成html代码,不过是收费的
源码的话真的没有,本人也是最近要做类似的项目,现学现卖的~
eWebEditor主页:
FCKeditor主页:
5.怎么用java导出word
java导出word代码如下:package com.bank.util;import java.awt.Color;import java.io.FileOutputStream;import java.io.IOException;import com.lowagie.text.Cell;import com.lowagie.text.Document;import com.lowagie.text.DocumentException;import com.lowagie.text.Element;import com.lowagie.text.Font;import com.lowagie.text.FontFactory;import com.lowagie.text.Image;import com.lowagie.text.PageSize;import com.lowagie.text.Paragraph;import com.lowagie.text.Phrase;import com.lowagie.text.Table;import com.lowagie.text.pdf.BaseFont;import com.lowagie.text.rtf.RtfWriter2;public class WordTools { public void createDocContext(String file) throws DocumentException, IOException { // 设置纸张大小 Document document = new Document(PageSize.A4); // 建立一个书写器(Writer)与document对象关联,通过书写器(Writer)可以将文档写入到磁盘中 RtfWriter2.getInstance(document, new FileOutputStream(file)); document.open(); // 设置中文字体 BaseFont bfChinese = BaseFont.createFont("STSongStd-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED); // 标题字体风格 Font titleFont = new Font(bfChinese, 12, Font.BOLD); // 正文字体风格 Font contextFont = new Font(bfChinese, 10, Font.NORMAL); Paragraph title = new Paragraph("标题"); // 设置标题格式对齐方式 title.setAlignment(Element.ALIGN_CENTER); title.setFont(titleFont); document.add(title); String contextString = "iText是一个能够快速产生PDF文件的java类库。
" + " \n"// 换行 + "iText的java类对于那些要产生包含文本," + "表格,图形的只读文档是很有用的。它的类库尤其与java Servlet有很好的给合。
" + "使用iText与PDF能够使你正确的控制Servlet的输出。"; Paragraph context = new Paragraph(contextString); // 正文格式左对齐 context.setAlignment(Element.ALIGN_LEFT); context.setFont(contextFont); // 离上一段落(标题)空的行数 context.setSpacingBefore(5); // 设置第一行空的列数 context.setFirstLineIndent(20); document.add(context); //利用类FontFactory结合Font和Color可以设置各种各样字体样式 /** * Font.UNDERLINE 下划线,Font.BOLD 粗体 */ Paragraph underline = new Paragraph("下划线的实现", FontFactory.getFont( FontFactory.HELVETICA_BOLDOBLIQUE, 18, Font.UNDERLINE, new Color(0, 0, 255))); document.add(underline); // 设置 Table 表格 Table aTable = new Table(3); int width[] = {25,25,50}; aTable.setWidths(width);//设置每列所占比例 aTable.setWidth(90); // 占页面宽度 90% aTable.setAlignment(Element.ALIGN_CENTER);//居中显示 aTable.setAlignment(Element.ALIGN_MIDDLE);//纵向居中显示 aTable.setAutoFillEmptyCells(true); //自动填满 aTable.setBorderWidth(1); //边框宽度 aTable.setBorderColor(new Color(0, 125, 255)); //边框颜色 aTable.setPadding(0);//衬距,看效果就知道什么意思了 aTable.setSpacing(0);//即单元格之间的间距 aTable.setBorder(2);//边框 //设置表头 /** * cell.setHeader(true);是将该单元格作为表头信息显示; * cell.setColspan(3);指定了该单元格占3列; * 为表格添加表头信息时,要注意的是一旦表头信息添加完了之后, \ * 必须调用 endHeaders()方法,否则当表格跨页后,表头信息不会再显示 */ Cell haderCell = new Cell("表格表头"); haderCell.setHeader(true); haderCell.setColspan(3); aTable.addCell(haderCell); aTable.endHeaders(); Font fontChinese = new Font(bfChinese, 12, Font.NORMAL, Color.GREEN); Cell cell = new Cell(new Phrase("这是一个测试的 3*3 Table 数据", fontChinese )); cell.setVerticalAlignment(Element.ALIGN_TOP); cell.setBorderColor(new Color(255, 0, 0)); cell.setRowspan(2); aTable.addCell(cell); aTable.addCell(new Cell("#1")); aTable.addCell(new Cell("#2")); aTable.addCell(new Cell("#3")); aTable.addCell(new Cell("#4")); Cell cell3 = new Cell(new Phrase("一行三列数据", fontChinese )); cell3.setColspan(3); cell3.setVerticalAlignment(Element.ALIGN_CENTER); aTable.addCell(cell3); document.add(aTable); document.add(new Paragraph("\n")); //添加图片// Image img=Image.getInstance("http://127.0.0.1:8080/testSystem/images/1_r1_c1.png");// img.setAbsolutePosition(0, 0);// img.setAlignment(Image.RIGHT);//设置图片显示位置// img.scaleAbsolute(12,35);//直接设定显示尺寸// img.scalePercent(50);//表示显示的大小为原尺寸的50%// img.scalePercent(25, 12);//图像高宽的显示比例// img.setRotation(30);//图像旋转一定角度// document.add(img); document.close(); } public static void main(String[] args){ WordTools b=new WordTools(); try { b.createDocContext("d:/demo.doc"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOExceptio。
6.怎么用java导出word文档
java导出word大致有6种解决方案:1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁。
使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用。DLL动态链接库的生成需要windows平台的支持。
该方案只能在windows平台实现,是其局限性。2:Apache POI包括一系列的API,它们可以操作基于MicroSoft OLE 2 Compound Document Format的各种格式文件,可以通过这些API在Java中读写Excel、Word等文件。
他的excel处理很强大,对于word还局限于读取,目前只能实现一些简单文件的操作,不能设置样式。3:Java2word是一个在java程序中调用 MS Office Word 文档的组件(类库)。
该组件提供了一组简单的接口,以便java程序调用他的服务操作Word 文档。 这些服务包括: 打开文档、新建文档、查找文字、替换文字,插入文字、插入图片、插入表格,在书签处插入文字、插入图片、插入表格等。
填充数据到表格中读取表格数据 ,1.1版增强的功能: 指定文本样式,指定表格样式。如此,则可动态排版word文档。
是一种不错的解决方案。4:iText是著名的开放源码的站点sourceforge一个项目,是用于生成PDF文档的一个java类库。
通过iText不仅可以生成PDF或rtf的文档,而且可以将XML、Html文件转化为PDF文件。功能强大。
5:JSP输出样式,该方案实现简单,但是处理样式有点缺陷,简单的导出可以使用。6:用XML做就很简单了。
Word从2003开始支持XML格式,大致的思路是先用office2003或者2007编辑好word的样式,然后另存为xml,将xml翻译为FreeMarker模板,最后用java来解析FreeMarker模板并输出Doc。经测试这样方式生成的word文档完全符合office标准,样式、内容控制非常便利,打印也不会变形,生成的文档和office中编辑文档完全一样。
转载请注明出处51数据库 » javahtml导出word文档
我的逗比中带着高冷