C# 將doc轉(zhuǎn)換為docx(需Office的Word包支持)
當前位置:點晴教程→知識管理交流
→『 技術(shù)文檔交流 』
因為從事行業(yè)原因長期跟遠古OA平臺打交道,導(dǎo)出來的文檔都是DOC格式,遠古格式操作起來很多類不兼容,非常折磨。所以想研究一個量化的轉(zhuǎn)化工具。本人沒從事本專業(yè),學藝不精,雖然也能通過PY寫簡單的轉(zhuǎn)換器,但還是C#對Windows兼容性更好,而且性能非常不錯,至少我是非常喜歡,現(xiàn)在市面上的代碼存在轉(zhuǎn)化對象錯誤,一個大哥寫了個就錯了一個對象名的轉(zhuǎn)化工具被營銷號各種抄襲導(dǎo)致根本查不到正確的相關(guān)代碼,以下我的代碼借鑒于這位老哥的項目,由于這位老哥被抄了太多不知道到底是誰原創(chuàng),所以不在此列出。 直接上代碼: // 轉(zhuǎn)化類 // pathinfo 選擇文件夾位置 // file 對象名 public void TranWordDocToDocx(string pathinfo, string file) { Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application(); Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document(); object oMissing = System.Reflection.Missing.Value;
Object ConfirmConversions = false; Object ReadOnly = false; Object AddToRecentFiles = false;
Object PasswordDocument = ""; Object PasswordTemplate = System.Type.Missing; Object Revert = System.Type.Missing; Object WritePasswordDocument = System.Type.Missing; Object WritePasswordTemplate = System.Type.Missing; Object Format = System.Type.Missing; Object Encoding = System.Type.Missing; Object Visible = System.Type.Missing; Object OpenAndRepair = System.Type.Missing; Object DocumentDirection = System.Type.Missing; Object NoEncodingDialog = System.Type.Missing; Object XMLTransform = System.Type.Missing;
Object fileName = pathinfo + "\\" + file; doc = word.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate, ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);
DirectoryInfo directory = new DirectoryInfo(pathinfo + "\\docx"); if (!directory.Exists)//不存在 { directory.Create(); }
object path = pathinfo + "\\docx\\" + file.Substring(0, file.Length - 4); //以下參數(shù)控制轉(zhuǎn)換類型,網(wǎng)上查到的全是錯誤的,是doc類型 object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatXMLDocument; doc.SaveAs(ref path, ref format, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); doc.Close(ref oMissing, ref oMissing, ref oMissing); word.Quit(ref oMissing, ref oMissing, ref oMissing);
} //調(diào)用實例 private void button1_Click(object sender, EventArgs e) { if (folderBrowserDialog1.ShowDialog() == DialogResult.OK) {
this.txtDocPaths.Text = folderBrowserDialog1.SelectedPath;
}
DirectoryInfo folder = new DirectoryInfo(this.txtDocPaths.Text); foreach (FileInfo file in folder.GetFiles("*.doc")) { if (file.ToString().IndexOf("$") == -1) { this.lblMsg.Text = string.Format("當前處理的文件:{0}", file.FullName); this.lblMsg.Refresh(); TranWordDocToDocx(this.txtDocPaths.Text, file.ToString()); } } } //using using Microsoft.CodeAnalysis; using Microsoft.Office.Interop.Word; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; 電腦里沒有原生office的在NuGet里安裝word包 有原生word的在引用里引用 該文章在 2023/12/30 0:25:25 編輯過 |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |