用户登录
用户注册

分享至

cword转html字符串 如何将字符串转换为RTF在C#中

  • 作者: 网友3187355
  • 来源: 51数据库
  • 2020-04-21

using System; namespace DocConvert {

class DoctoRtf{static void Main()

{ //创建一个word的实例

Word.Application newApp = new Word.Application(); // 指定源文件和目标文件

object Source="c:\\abc\\Source.doc";

object Target="c:\\abc\\Target.rtf"; object Unknown =Type.Missing; // 打开要转换的Word文件

newApp.Documents.Open(ref Source,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown ); // 指定文档的类型

object format = Word.WdSaveFormat.wdFormatRTF; //改变文档类型

newApp.ActiveDocument.SaveAs(ref Target,ref format,ref Unknown,ref Unknown,ref Unknown,

如何利用openoffice读取word文档,并输出在页面上(HTML)

将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、打开eclipse

6、喝杯热茶,等待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 = "<BODY .*</BODY>";

Pattern bodyPattern = Pattern.compile(bodyReg);

Matcher bodyMatcher = bodyPattern.matcher(htmlStr);

if (bodyMatcher.find()) {

// 获取BODY内容,并转化BODY标签为DIV

htmlStr = bodyMatcher.group().replaceFirst("<BODY", "<DIV")

.replaceAll("</BODY>", "</DIV>");

}

// 调整图片地址

htmlStr = htmlStr.replaceAll("<IMG SRC=\"", "<IMG SRC=\"" + docImgPath

+ "/");

// 把<P></P>转换成</div></div>保留样式

// content = content.replaceAll("(<P)([^>]*>.*?)(<\\/P>)",

// "<div$2</div>");

// 把<P></P>转换成</div></div>并删除样式

htmlStr = htmlStr.replaceAll("(<P)([^>]*)(>.*?)(<\\/P>)", "<p$3</p>");

// 删除不需要的标签

htmlStr = htmlStr

.replaceAll(

"<[/]?(font|FONT|span|SPAN|xml|XML|del|DEL|ins|INS|meta|META|[ovwxpOVWXP]:\\w+)[^>]*?>",

"");

// 删除不需要的属性

htmlStr = htmlStr

.replaceAll(

"<([^>]*)(?:lang|LANG|class|CLASS|style|STYLE|size|SIZE|face|FACE|[ovwxpOVWXP]:\\w+)=(?:'[^']*'|\"\"[^\"\"]*\"\"|[^>]+)([^>]*)>",

"<$1$2>");

return htmlStr;

}

}

java中的jacob将word文档转化为HTML文件问题

1、到官网下载Jacob,

2、将压缩包解压后,Jacob.jar添加到Libraries中(先复制到项目目录中,右键单击jar包选择Build Path—>Add to Build Path);

3、将Jacob.dll放至当前项目所用到的“jre\bin”下面(比如Eclipse正在用的Jre路径是C:\Java\jdk1.7.0_17\jre\bin)。

Ps:按照上面的步骤配置的,基本没有问题,但是有些电脑可能还会报错,比如:java.lang.UnsatisfiedLinkError: no jacob in java.library.path,这是系统没有加载到jacob.dll,网上解决方法是将Jacob.dll放至“WINDOWS\SYSTEM32”下面。

Java代码:

public class JacobUtil {

// 8 代表word保存成html

public static final int WORD_HTML = 8;

public static void main(String[] args) {

String docfile = "C:\\Users\\无名\\Desktop\\xxx.doc";

String htmlfile = "C:\\Users\\无名\\Desktop\\xxx.html";

JacobUtil.wordToHtml(docfile, htmlfile);

}

/**

* WORD转HTML

* @param docfile WORD文件全路径

* @param htmlfile 转换后HTML存放路径

*/

public static void wordToHtml(String docfile, String htmlfile)

{

// 启动word应用程序(Microsoft Office Word 2003)

ActiveXComponent app = new ActiveXComponent("Word.Application");

System.out.println("*****正在转换...*****");

try

{

// 设置word应用程序不可见

app.setProperty("Visible", new Variant(false));

// documents表示word程序的所有文档窗口,(word是多文档应用程序)

Dispatch docs = app.getProperty("Documents").toDispatch();

// 打开要转换的word文件

Dispatch doc = Dispatch.invoke(

docs,

"Open",

Dispatch.Method,

new Object[] { docfile, new Variant(false),

new Variant(true) }, new int[1]).toDispatch();

// 作为html格式保存到临时文件

Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {

htmlfile, new Variant(WORD_HTML) }, new int[1]);

// 关闭word文件

Dispatch.call(doc, "Close", new Variant(false));

}catch (Exception e){

e.printStackTrace();

}finally{

//关闭word应用程序

app.invoke("Quit", new Variant[] {});

}

System.out.println("*****转换完毕********");

}

}

一道C语言中字符串的简单问题!

#include<stdio.h>

#include<string.h>

#include<malloc.h>

#define STRINGSIZE 100

#define WORDSIZE 100

typedef struct word

{

char data[20];

struct word * next;

}word;

word * Find(word * head,char str1[])

{

word * p;

p=head;

while(strcmp(str1,p->data)!=0&&p->next)

{

p=p->next;

}

if(strcmp(str1,p->data)==0)

return p;

return NULL;

}

void PrintS(word * head)

{

word * p=head;

while(p)

{

if(p->next)

printf("%s ",p->data);

else

printf("%s\n",p->data);

p=p->next;

}

}

void main()

{

word head;

head.next=NULL;//头

char str[STRINGSIZE];

gets(str);

char str1[WORDSIZE],str2[WORDSIZE];

scanf("%s",str1);

scanf("%s",str2);

int n=strlen(str);

int i,j=0;

word * t,*p;

p=&head;

for(i=0;i<n;i++)

{

if(str[i]!=' ')

{

p->data[j]=str[i];

j++;

}

else if(str[i]==' ')

{

p->data[j]='\0';

j=0;

t=(word *)malloc(sizeof(word));

t->next=NULL;

p->next=t;

p=t;

}

}

p->data[j]='\0';

t=Find(&head,str1);

if(t) strcpy(t->data,str2);

PrintS(&head);

}

参考资料:http://hi.baidu.com/ttcc2009/blog/item/c73b51876561723566096e12.html

C语言中怎么打开一个文件然后写入一些数据,然后再把数据取出来,最好有代码。

有两种方法:

一是标准的输出输入方式

比如新建一个磁盘文件c:\a.txt,

将键盘输入的一字符串写到文件中:

FILE *ft;

char str[50];

ft=fopen("c:\\a.txt","w+");

printf("输入一个字符串:");

scanf("%s",str);

fputs(str,ft);

fclose(ft);

//重新打开这个文件并读出字符串,显示在屏幕上

ft=fopen("c:\\a.txt","rt");

fgets(str,50,ft);

fclose(ft);

printf("%s",str);

二是低级输入输出方式

仍如上例:

int hd;

char str[50];

printf("输入一个字符串:");

scanf("%s",str);

hd=open("c:\\a.txt",O_CREAT|O_TEXT|O_WRONLY);

write(hd,str,strlen(str));

close(hd);

//重新打开这个文件并读出字符串,显示在屏幕上

hd=open("c:\\a.txt",O_TEXT|O_RDONLY);

read(hd,str,50);

close(hd);

printf("%s",str);

word如何设置域?

1、首先,我们打开我们电脑上面的word,然后我们点击插入。

2、然后我们点击工具栏中的文档部件,弹出的界面,我们点击域。

3、弹出的界面,我们点击选择AutoNum,然后我们点击选择1,2,3···,然后我们勾选上分隔符,之后点击确定。

4、结果如图所示,这样我们就插入了一个域了。

怎样在Word2010中查找和替换字符格式

在Word2010中查找和替换字符格式的方法:

准备资料:word2010

  1. 打开Word2010文档窗口,在“开始”功能区的“编辑”分组中依次单击“查找”→“高级查找”按钮,如图所示。 单击“高级查找”按钮

  2. 在打开的“查找和替换”对话框中单击“更多”按钮,以显示更多的查找选项,如图所示。 单击“更多”按钮

  3. 在“查找内容”编辑框中单击鼠标左键,使光标位于编辑框中。然后单击“查找”区域的“格式”按钮,如图所示。单击“格式”按钮

  4. 在打开的格式菜单中单击相应的格式类型(例如“字体”、“段落”等),本实例单击“字体”命令,如图所示。单击“字体”按钮

  5. 打开“查找字体”对话框,可以选择要查找的字体、字号、颜色、加粗、倾斜等选项。本例选择“加粗”选项,并单击“确定”按钮,如图所示。“查找字体”对话框

  6. 返回“查找和替换”对话框,单击“查找下一处”按钮查找格式,如图所示。 单击“查找下一处”按钮

    小提示:如果需要将原有格式替换为指定的格式,可以切换到“替换”选项卡。然后指定想要替换成的格式,并单击“全部替换”按钮。

什么是字符串

"hello" 这就是字符串

'h' 这个就是字符

"h" 这也是字符串

在我们看来有""包含的是字符串 而''包含的是字符

转载请注明出处51数据库 » cword转html字符串 如何将字符串转换为RTF在C#中

软件
前端设计
程序设计
Java相关