gpt-3.5-turbo-0301· OpenAI
WildChat-4.8M (AI2)
C# 批量导出word文档
- User
C# 批量导出word文档
- Assistant
以下是C#批量导出word文档的示例代码:
using System; using System.IO; using Microsoft.Office.Interop.Word; namespace WordExport { class Program { static void Main(string[] args) { string sourcePath = @"C:\source\"; string targetPath = @"C:\target\"; string[] files = Directory.GetFiles(sourcePath, "*.docx"); Application word = new Application(); word.Visible = false; foreach (string file in files) { Document doc = word.Documents.Open(file); string filename = Path.GetFileNameWithoutExtension(file); string targetFile = targetPath + filename + ".pdf"; doc.ExportAsFixedFormat(targetFile, WdExportFormat.wdExportFormatPDF); doc.Close(); } word.Quit(); } } }上述代码将批量导出源目录(sourcePath)中所有扩展名为.docx的word文档,并将导出的PDF文件存储在目标目录(targetPath)中。代码使用Microsoft Office Interop库操作Word文档,因此需要在项目中添加对该库的引用。在运行代码之前,确保电脑上已安装Microsoft Office软件。