LOGO OA教程 ERP教程 模切知识交流 PMS教程 CRM教程 开发文档 其他文档  
 
网站管理员

C# 将doc转换为docx(需Office的Word包支持)

admin
2023年12月30日 0:16 本文热度 470

因为从事行业原因长期跟远古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的在引用里引用
懒人版C#项目:https://download.csdn.net/download/Jiangsinan_/84995190
整个项目包我都包上了,VS19直接能打开。


该文章在 2023/12/30 0:25:25 编辑过
关键字查询
相关文章
正在查询...
点晴ERP是一款针对中小制造业的专业生产管理软件系统,系统成熟度和易用性得到了国内大量中小企业的青睐。
点晴PMS码头管理系统主要针对港口码头集装箱与散货日常运作、调度、堆场、车队、财务费用、相关报表等业务管理,结合码头的业务特点,围绕调度、堆场作业而开发的。集技术的先进性、管理的有效性于一体,是物流码头及其他港口类企业的高效ERP管理信息系统。
点晴WMS仓储管理系统提供了货物产品管理,销售管理,采购管理,仓储管理,仓库管理,保质期管理,货位管理,库位管理,生产管理,WMS管理系统,标签打印,条形码,二维码管理,批号管理软件。
点晴免费OA是一款软件和通用服务都免费,不限功能、不限时间、不限用户的免费OA协同办公管理系统。
Copyright 2010-2024 ClickSun All Rights Reserved