C# 將doc轉(zhuǎn)換為docx(需Office的Word包支持)
當(dāng)前位置:點(diǎn)晴教程→知識(shí)管理交流
→『 技術(shù)文檔交流 』
因?yàn)閺氖滦袠I(yè)原因長(zhǎng)期跟遠(yuǎn)古OA平臺(tái)打交道,導(dǎo)出來(lái)的文檔都是DOC格式,遠(yuǎn)古格式操作起來(lái)很多類不兼容,非常折磨。所以想研究一個(gè)量化的轉(zhuǎn)化工具。本人沒從事本專業(yè),學(xué)藝不精,雖然也能通過(guò)PY寫簡(jiǎn)單的轉(zhuǎn)換器,但還是C#對(duì)Windows兼容性更好,而且性能非常不錯(cuò),至少我是非常喜歡,現(xiàn)在市面上的代碼存在轉(zhuǎn)化對(duì)象錯(cuò)誤,一個(gè)大哥寫了個(gè)就錯(cuò)了一個(gè)對(duì)象名的轉(zhuǎn)化工具被營(yíng)銷號(hào)各種抄襲導(dǎo)致根本查不到正確的相關(guān)代碼,以下我的代碼借鑒于這位老哥的項(xiàng)目,由于這位老哥被抄了太多不知道到底是誰(shuí)原創(chuàng),所以不在此列出。 直接上代碼: // 轉(zhuǎn)化類 // pathinfo 選擇文件夾位置 // file 對(duì)象名 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)上查到的全是錯(cuò)誤的,是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)用實(shí)例 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("當(dāng)前處理的文件:{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ò) |
關(guān)鍵字查詢
相關(guān)文章
正在查詢... |