C# 将doc转换为docx(需Office的Word包支持)
当前位置:点晴教程→知识管理交流
→『 技术文档交流 』
因为从事行业原因长期跟远古OA平台打交道,导出来的文档都是DOC格式,远古格式操作起来很多类不兼容,非常折磨。所以想研究一个量化的转化工具。本人没从事本专业,学艺不精,虽然也能通过PY写简单的转换器,但还是C#对Windows兼容性更好,而且性能非常不错,至少我是非常喜欢,现在市面上的代码存在转化对象错误,一个大哥写了个就错了一个对象名的转化工具被营销号各种抄袭导致根本查不到正确的相关代码,以下我的代码借鉴于这位老哥的项目,由于这位老哥被抄了太多不知道到底是谁原创,所以不在此列出。 直接上代码: // 转化类 // 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); //以下参数控制转换类型,网上查到的全是错误的,是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);
} //调用实例 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 编辑过 |
关键字查询
相关文章
正在查询... |