c# html文件转换word,C#实现word转换成html文档 源码
【實例簡介】C#實現word轉換成html文檔
【實例截圖】
【核心代碼】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace WordToHtml
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static void BatchConvert(string docDir)//如果批量轉換
{
//創建數組保存文件夾下的文件名
string[] docFiles = Directory.GetFiles(docDir, "*.doc");
for (int i = 0; i < docFiles.Length; i )
{
WordToHtmlFile(docFiles[i]);
}
MessageBox.Show("轉換完畢!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
//將word轉換為html
public static void WordToHtmlFile(string WordFilePath)
{
try
{
Word.Application wApp = new Word.Application();
//指定原文件和目標文件
object docPath = WordFilePath;
string htmlPath = WordFilePath.Substring(0, WordFilePath.Length - 3) "html";
object Target = htmlPath;
//缺省參數
object Unknown = Type.Missing;
//只讀方式打開
object readOnly = true;
//打開doc文件
Word.Document document = wApp.Documents.Open(ref docPath, ref Unknown,
ref readOnly, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown);
// 指定格式
object format = Word.WdSaveFormat.wdFormatHTML;
// 轉換格式
document.SaveAs(ref Target, ref format,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref Unknown);
// 關閉文檔和Word程序
document.Close(ref Unknown, ref Unknown, ref Unknown);
wApp.Quit(ref Unknown, ref Unknown, ref Unknown);
}
catch (Exception e)
{
MessageBox.Show(e.Message);
}
}
private void button1_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
tsslInfo.Text = "";
txtWordPath.Text = folderBrowserDialog1.SelectedPath;
string[] docFiles = Directory.GetFiles(txtWordPath.Text.Trim(),"*.doc");
for (int i = 0; i < docFiles.Length; i )
{
string docPath=docFiles[i].ToString();
string docName = docPath.Substring(docPath.LastIndexOf("\\") 1, docPath.Length - docPath.LastIndexOf("\\") -1);
listView1.Items.Add(docName);
}
tsslInfo.Text = "文件數量:" listView1.Items.Count.ToString();
}
}
private void button2_Click(object sender, EventArgs e)//單個轉換
{
if (listView1.Items.Count > 0)
{
if (listView1.SelectedItems.Count > 0)
{
if (MessageBox.Show("確定將此文檔轉換為HTML文件嗎?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
string docPath = txtWordPath.Text.Trim() "\\" listView1.SelectedItems[0].Text;
WordToHtmlFile(docPath);
MessageBox.Show("轉換完畢!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
else
{
MessageBox.Show("請選擇要轉換的Word文檔!");
}
}
}
private void button4_Click(object sender, EventArgs e)//批量轉換
{
if (listView1.Items.Count > 0)
{
if (MessageBox.Show("確定將批量將文檔轉換為HTML文件嗎?", "提示信息", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
BatchConvert(txtWordPath.Text.Trim());
}
}
}
private void button3_Click(object sender, EventArgs e)
{
Application.Exit();
}
}
}
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的c# html文件转换word,C#实现word转换成html文档 源码的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 升级鸿蒙系统照片,华为鸿蒙系统照片出炉,
- 下一篇: vue router 原生html,Vu