当前位置:首页教育技巧word技巧Word技巧大全

VBNET如何打开word文档,怎么在网页中打开word文档用代码实现

2025-01-17 13:19:13


内容导航:

  • VBNET如何打开word文档
  • 怎么在网页中打开word文档用代码实现
  • word文档中总是显示代码该怎么办

一、VBNET如何打开word文档


在窗体上放一个按钮,一个Webbrowser

简单代码如下,其他排错处理自己来:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim open As New OpenFileDialog

open.ShowDialog()

WebBrowser1.Navigate(open.FileName)

End Sub

效果如图:



二、怎么在网页中打开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 3program>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 = "";
Pattern bodyPattern = Patternpile(bodyReg);
Matcher bodyMatcher = bodyPattern.matcher(htmlStr);
if (bodyMatcher.find()) {
// 获取BODY内容,并转化BODY标签为DIV
htmlStr = bodyMatcher.group().replaceFirst("<body", " .replaceAll("", "");
}
// 调整图片地址
htmlStr = htmlStr.replaceAll(" + "/");
// 把转换成保留样式
// content = content.replaceAll("(

]*>.*?)(<\/P>)",
// "");
// 把转换成并删除样式
htmlStr = htmlStr.replaceAll("(

]*)(>.*?)(<\/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;
}

}


三、word文档中总是显示代码该怎么办


出现此问题的原因:设置了显示域代码而非域值导致的。

解决的方法和操作步骤如下:

1、首先,鼠标左键双击桌面上的Word2013程序图标以打开并运行。在打开的Word中启动桌面,选择“打开其他文档”选项,然后打开要编辑的Word文档,如下图所示,进入下一步。

2、其次,完成上述步骤后,在打开的Word文档编辑窗口中,单击左上角的“文件”选项,如下图所示,然后进入下一步。

3、接着,完成上述步骤后,选择“选项”并单击,如下图所示,然后进入下一步。

4、然后,完成上述步骤后,此时将打开“ Word选项”对话框窗口,如下图所示,然后进入下一步。

5、随后,完成上述步骤后,在左窗格中找到“高级”功能选项并单击,如下图所示,然后进入下一步。

6、接着,完成上述步骤后,在“显示文档内容”选项下取消勾选“显示域代码而非域值”这一项,如下图所示,然后进入下一步。

7、然后,完成上述步骤后,单击“确定”按钮以返回到Word文件编辑窗口,如下图所示,然后进入下一步。

8、最后,完成上述步骤后,文件中的页码将正常显示,如下图所示。这样,问题就解决了。


相关信息


电脑版

【免责声明】本站信息来自网友投稿及网络整理,内容仅供参考,如果有错误请反馈给我们及时更正,对文中内容的真实性和完整性本站不提供任何保证,不承但任何责任。
版权所有:学窍知识网 Copyright © 2011-2025 www.at317.com All Rights Reserved .