生成word并保存
saveas方法只能用绝对路径,不能用相对路径,这个问题倒腾了好久,并且 Microsoft.Office.Interop.Word._Application 需要设置为不可见
object oMissing = System.Reflection.Missing.Value; //创建一个Word应用程序实例 Microsoft.Office.Interop.Word._Application oWord = new Microsoft.Office.Interop.Word.Application(); //设置为不可见 oWord.Visible = true; Microsoft.Office.Interop.Word._Document oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //弹出保存文件对话框,保存生成的Word SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "Word Document(*.doc)|*.doc"; sfd.DefaultExt = "Word Document(*.doc)|*.doc"; if (sfd.ShowDialog() == DialogResult.OK) { object filename = sfd.FileName; oDoc.SaveAs(ref filename, 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, ref oMissing); oDoc.Close(ref oMissing, ref oMissing, ref oMissing); //关闭word oWord.Quit(ref oMissing, ref oMissing, ref oMissing); }
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步