HTML怎么导出生成word文档
将Word转Html的原理是这样的:1、客户上传Word文档到服务器2、服务器调用OpenOffice程序打开上传的Word文档3、OpenOffice将Word文档另存为Html格式4、Over至此可见,这要求服务器端安装OpenOffice软件,其实也可以是MS Office,不过OpenOffice的优势是跨平台,你懂的。
恩,说明一下,本文的测试基于 MS Win7 Ultimate X64 系统。
下面就是规规矩矩的实现。
1、下载OpenOffice,2、下载Jodconverter 这是一个开启OpenOffice进行格式转化的第三方jar包。
3、泡杯热茶,等待下载。
4、安装OpenOffice,安装结束后,调用cmd,启动OpenOffice的一项服务:C:\Program Files (x86)\OpenOffice.org 3\program>soffice -headless -accept="socket,port=8100;urp;"5、打开eclipse6、喝杯热茶,等待eclipse打开。
7、新建eclipse项目,导入Jodconverter/lib 下得jar包。
* commons-io * jodconverter * juh * jurt * ridl * slf4j-api * slf4j-jdk14 * unoil * xstream 8、Coding...查看代码package com.mzule.doc2html.util;import java.io.BufferedReader;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStreamReader;import java.net.ConnectException;import java.util.Date;import java.util.regex.Matcher;import java.util.regex.Pattern;import com.artofsolving.jodconverter.DocumentConverter;import com.artofsolving.jodconverter.openoffice.connection.OpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.connection.SocketOpenOfficeConnection;import com.artofsolving.jodconverter.openoffice.converter.OpenOfficeDocumentConverter;/** * 将Word文档转换成html字符串的工具类 * * @author MZULE * */public class Doc2Html { public static void main(String[] args) { System.out .println(toHtmlString(new File("C:/test/test.doc"), "C:/test")); } /** * 将word文档转换成html文档 * * @param docFile * 需要转换的word文档 * @param filepath * 转换之后html的存放路径 * @return 转换之后的html文件 */ public static File convert(File docFile, String filepath) { // 创建保存html的文件 File htmlFile = new File(filepath + "/" + new Date().getTime() + ".html"); // 创建Openoffice连接 OpenOfficeConnection con = new SocketOpenOfficeConnection(8100); try { // 连接 con.connect(); } catch (ConnectException e) { System.out.println("获取OpenOffice连接失败..."); e.printStackTrace(); } // 创建转换器 DocumentConverter converter = new OpenOfficeDocumentConverter(con); // 转换文档问html converter.convert(docFile, htmlFile); // 关闭openoffice连接 con.disconnect(); return htmlFile; } /** * 将word转换成html文件,并且获取html文件代码。
* * @param docFile * 需要转换的文档 * @param filepath * 文档中图片的保存位置 * @return 转换成功的html代码 */ public static String toHtmlString(File docFile, String filepath) { // 转换word文档 File htmlFile = convert(docFile, filepath); // 获取html文件流 StringBuffer htmlSb = new StringBuffer(); try { BufferedReader br = new BufferedReader(new InputStreamReader( new FileInputStream(htmlFile))); while (br.ready()) { htmlSb.append(br.readLine()); } br.close(); // 删除临时文件 htmlFile.delete(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // HTML文件字符串 String htmlStr = htmlSb.toString(); // 返回经过清洁的html文本 return clearFormat(htmlStr, filepath); } /** * 清除一些不需要的html标记 * * @param htmlStr * 带有复杂html标记的html语句 * @return 去除了不需要html标记的语句 */ protected static String clearFormat(String htmlStr, String docImgPath) { // 获取body内容的正则 String bodyReg = ""; Pattern bodyPattern = Pattern.compile(bodyReg); Matcher bodyMatcher = bodyPattern.matcher(htmlStr); if (bodyMatcher.find()) { // 获取BODY内容,并转化BODY标签为DIV htmlStr = bodyMatcher.group().replaceFirst("", ""); } // 调整图片地址 htmlStr = htmlStr.replaceAll("转换成保留样式 // content = content.replaceAll("(]*>.*?)()", // ""); // 把转换成并删除样式 htmlStr = htmlStr.replaceAll("(]*)(>.*?)()", ""); // 删除不需要的标签 htmlStr = htmlStr .replaceAll( "]*?>", ""); // 删除不需要的属性 htmlStr = htmlStr .replaceAll( "]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>", ""); return htmlStr; }}
怎么把Word转换为网页html格式
C#实现的word转html命令行工具,对情况进行了解和思考后,认为可以通过一个中间程序自动化的将word文档转换成为excel,遂决定写个程序来实现这个转换环节。
对于这种纯Windows的需求,估计也就是JAVA或者是VS系列语言更加方便一些。
最近的项目使用的语言主要是JAVA、VC++,C#还没有尝试过完成实际项目,于是打算用C#尝试实现一下。
http://blog.csdn.net/solomonlangrui/article/details/47168449...
我要用C#,NET 导出一个word文档。
文档内容只要一个表格。
楼上的 你能不能改写下这个例子啊? 我这个只能导出字符 不能导出表格 string title = "个人信息"; object titleLengh = title.Length; string first = "\n 公司最近需要利用C#对项目进行编程,其" + "中存在一个功能就是可自动生成WORD文档,但一直以来都" + "找不到什么好办法,无奈之下,只有自已学着写一个了."; object firstLengh = first.Length; string second = "\n 如果能真正生成WORD文档的好处有:"; object secondLengh = second.Length; string third = "\n1、根据数据库信息自动生成文档;"; object thirdLengh = third.Length; string forth = "\n2、免去书写文档之苦;"; object forthLengh = forth.Length; string fifth = "\n3、可以通过邮件方式传出文档。
"; object fifthLengh = fifth.Length; object missing; object zero = 0; object c = 10; object one = 1; object two = 2; object tree = 3; object four = 4; object five = 5; object six = 6; object seven = 7; object eight = 8; object nine = 9; object ten = 10; Object Nothing = System.Reflection.Missing.Value; Microsoft.Office.Interop.Word.ApplicationClass wa = new Microsoft.Office.Interop.Word.ApplicationClass(); Microsoft.Office.Interop.Word.Document WordDoc = wa.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); missing = System.Reflection.Missing.Value; wa.Visible = true; wa.Documents.Add(ref missing, ref missing, ref missing, ref missing); Microsoft.Office.Interop.Word.Range myRange = wa.ActiveDocument.Range(ref zero, ref zero); object r = myRange; Microsoft.Office.Interop.Word.Paragraph p = wa.ActiveDocument.Paragraphs.Add(ref r); p.Range.InsertBefore(title); //p.Range.Font.Size = 1; Microsoft.Office.Interop.Word.Range titleRange = wa.ActiveDocument.Range(ref zero, ref titleLengh); //titleRange.Font.Size = 1; titleRange.Font.Name = "幼圆"; titleRange.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlue; //MessageBox.Show("NO.1"); //titleRange.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; Microsoft.Office.Interop.Word.Range firstR = wa.ActiveDocument.Paragraphs[1].Range;//.Item(2).Range; Microsoft.Office.Interop.Word.Table table = WordDoc.Tables.Add(titleRange, 3, 3, ref Nothing, ref Nothing); r = firstR; p = wa.ActiveDocument.Paragraphs.Add(ref r); firstR.Font.Size = 40; firstR.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//在表格第一单元格中添加自定义的文字内容 table.Cell(1, 1).Range.Text = "lllll"; firstR.InsertAfter(first);//firstR.InsertParagraphAfter(); //firstR=wa.ActiveDocument.Paragraphs.Item(3).Range; firstR.InsertAfter(second); firstR.InsertAfter(third); firstR.InsertAfter(forth); firstR.InsertAfter(fifth); Context.Response.Write("成功");
怎样将C语言中的输出结果转化成文本形式txt或WORD或EXCE
你的意思是把输出结果保存在文本文件吗? 有两种办法,一种是用C语言的文件操作函数。
用代码把结果存在.txt.文件中 另一种比较通用,先生成可执行文件,然后在命令行中运行。
例如生成了prog.exe,你在命令行中用 prog.exe >> result.txt可以把程序原来应该屏显的内容保存在result.txt中,result.txt是自动生成的。
word和excel实际上不是单纯的文本。
我想用c语言编程生成word文件
可以使用重定向输出比如你的程序名是abc.exe可以在控制台中输入:c:\>abc.exe > output.txt现在output.txt的内容就是abc.exe的输出了当然这种模式也可以输入,不过看不见程序的提示。
补充:在windows下可以在 开始-〉运行... 中输入cmd,点确定;然后用cd [路径名] 切换到程序所在的目录,再照上边的步骤就行了。
用tc的话可以使用file菜单吓得shell命令,好像是,记不清了。
转载请注明出处51数据库 » c html 导出word