C# 将doc转换为docx

清理收藏夹。

介绍

虽然现在office 的版本已经很高了,而且新的word都是使用的docx格式了,但是仍然还是存在doc格式的使用情况。尤其是对于一些旧的系统,这些系统的导出格式仍然是doc格式。而如果你需要进行word文档的相关操作------如导入,那么doc格式就不是那么的友好了。曾在度娘中找了很多资料,最后还是曲线救国,使用doc转docx后在导入word内容。

不多说了,直接上代码才最实在

cs 复制代码
 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);
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;
            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);
 }

以上代码中,第一个参数pathinfo表示doc文档的文件夹路径,第二个参数file表示word文件的名称。从方法的调用上说,需要选择doc文档所在的文件夹,然后可以在此文件夹下创建(如果没有)一个新的docx文件夹,用以保存转换后的word文件。

而对于调用的方式,这里给一个dome,如下

cs 复制代码
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());
    }
}

doc转docx的核心方法,大家也可根据实际项目中的需要进行调整!


https://blog.csdn.net/mzl87/article/details/102625239

相关推荐
贾宝玉的玉宝贾12 分钟前
FreeSWITCH 简单图形化界面 70 - 监听/密语的lua脚本
开发语言·junit·lua·voip·freeswitch·sip·ippbx
Wang's Blog13 分钟前
Java 项目部署之 Docker工具快速入门: 镜像命令从 pull 到 load 的完整实操
java·开发语言·docker
北京盛世宏博18 分钟前
异地机房采集踩坑笔记:以太网温湿度记录仪跨网段通信调试实战
开发语言·笔记·单片机·php·传感器·温湿度
Wang's Blog21 分钟前
Java 项目部署之 Docker工具快速入门: Docker 数据卷命令与挂载实操
java·开发语言·docker
霸道流氓气质31 分钟前
AI 应用异常检测与根因分析:语义漂移检测、因果图推理与LLM-as-Judge的 Java 生产级实现
java·开发语言·人工智能
棉猴37 分钟前
玩游戏学Python2-IDLE与pgzero入门
开发语言·python·游戏·玩游戏·pgzero·pgzrun
xuxigifxfh39 分钟前
牛客:BISHI2 【模板】栈的操作
java·开发语言·windows·算法
(Charon)1 小时前
【C++面试】线程并发与异步:区别、实现原理及使用场景
开发语言·c++
AIFQuant1 小时前
Python A股实时行情API接入:沪深股票数据与WebSocket推送实战
开发语言·python·websocket·金融·a股行情
字节暗面1 小时前
客户端上报不可信:游戏服务端数据校验与对局重演设计
c#·游戏开发·服务端校验·反作弊