当前位置: 首页 > news >正文

C# 将doc转换为docx

清理收藏夹。

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

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,如下

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

http://www.jsqmd.com/news/1216214/

相关文章:

  • 工控机死机一般是什么原因?怎么处理?从现场故障分析找到真正原因
  • 2026年苏州工业废气治理优选:哪家服务商更值得信赖?
  • 编译原理硬核词法分析器实战——转换图、最长匹配与 Lex/Flex(九)
  • 中国无人机表演公司第一梯队空中熊猫(成都)航空服务有限公司简介 - 资讯焦点
  • 如何选择合适的数智化企业服务管理平台?企业数字化转型指南
  • EasyMarkets:面向成熟用户的综合服务评估
  • Vim离线安装NERDTree文件树插件指南
  • 紧急通知:网信办新规实施倒计时72小时!你的AI评论系统是否通过“三审一校”兼容性验证?
  • 郑州口碑好的七孔梅花管销售厂家
  • Linux 零基础速查:看完就能上手的基础命令合集4
  • 并发编程系列(二)—— store, load 与 RMW
  • 2026年7月最新大连劳力士官方售后客服服务电话及地址网点大全 - 劳力士官方服务中心
  • 宝珀中国官方售后服务中心|官方热线及网点地址权威信息公告(2026年7月更新) - 宝珀官方售后服务中心
  • 一分钟大白话 | 搞懂RAG,才知道AI什么时候不需要RAG
  • PyTorch Profiler深度使用:从timeline到memory的完整性能分析工作流
  • 1994-2025年上市公司违法行为违规事件内容处罚数据
  • 我们缺少一个词,来描述 Agent 开始替人操作业务系统这件事
  • 把 Linux 内存想象成停车场,buff和cache 终于秒懂了
  • BepInEx终极指南:跨平台Unity游戏插件开发框架深度解析
  • 【Web安全】Portswigger 业务逻辑漏洞 Lab: Inconsistent handling of exceptional input 实验:特殊输入处理不一致
  • 从AI乱编到出版级输出:一位童书编辑+AI训练师双身份者的12小时实战复盘——如何用ChatGPT批量产出获凯迪克奖风格故事脚本
  • MySQL架构揭秘:事务和读一致性问题
  • AI数字人直播实时美颜与背景替换技术原理分析
  • Python中避免GIL的性能模式:多进程、子解释器与C扩展的选型指南
  • 2026年泡菜废水治理哪家强?选对方案少走弯路
  • 题解:洛谷 B4050 [GESP202409 五级] 挑战怪物
  • 自驾游买什么车:沃尔沃XC70、问界M7、比亚迪唐DM-p续航与户外能力对比 - 信息情报站
  • 基于迅为iTOP-RK3568开发板搭建完整物联网控制平台
  • 同事说我写的代码不好看懂,封装太多层
  • 编译原理硬核——编译器核心概念的全景速览(七)