C# で Office Word フォーマットでドキュメントをHTMLへ変換します。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
方法1:
private void ConvertFileWordToHtml(string WordFilePath)
{
try
{
//Word ファイル・HTMLの生成先のファイルを指定
object Source = WordFilePath;
string SaveHtmlPath = WordFilePath.Substring(0, WordFilePath.Length - 4) + "html";
object Target = SaveHtmlPath;
//ReadonlyでWordを開く
object Unknown = Type.Missing;
object readOnly = true;
object visible = false;
Microsoft.Office.Interop.Word.Application newApp = new Microsoft.Office.Interop.Word.Application();
Microsoft.Office.Interop.Word.Document doc = newApp.Documents.Open(ref Source, ref Unknown,
ref readOnly, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown,
ref Unknown, ref Unknown, ref visible, ref Unknown, ref Unknown, ref Unknown, ref Unknown);
// HTMLへ保存
Type docType = doc.GetType();
object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatHTML;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
null, doc, new object[] { Target, format,Unknown,Unknown,Unknown,Unknown,Unknown,
Unknown,Unknown,Unknown,Unknown,Unknown,Unknown,Unknown,Unknown,Unknown });
// doc.SaveAs(ref Target, ref format,
// ref Unknown, ref Unknown, ref Unknown,
// ref Unknown, ref Unknown, ref Unknown,
// ref Unknown, ref Unknown, ref Unknown,
// ref Unknown, ref Unknown, ref Unknown,
// ref Unknown, ref Unknown);
// クローズ
doc.Close(ref Unknown, ref Unknown, ref Unknown);
newApp.Quit(ref Unknown, ref Unknown, ref Unknown);
}
catch (Exception e)
{
System.Windows.Forms.MessageBox.Show(e.Message);
}
}
方法2:
protected void ConvertToHtml(string docPath,string htmlPath)
{
Word.Application app=new Word.Application();
app.Visible=false;
Object o=Missing.Value;
object docFile=docPath;
_Document doc=app.Documents.Open(ref docFile,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
object fileName=htmlPath;
object format=8;//Html
doc.SaveAs(ref fileName,ref format,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
object t=true;
app.Quit(ref t,ref o,ref o);
}
0 件のコメント:
コメントを投稿