C# Excel 文件复制服务:根据文件名创建文件夹并复制新文件
{/'title/':/'C# Excel 文件复制服务:根据文件名创建文件夹并复制新文件/',/'description/':/'使用 C# 语言编写服务,将某个文件夹下的 Excel 文件复制到另一个硬盘文件夹,并根据 Excel 文件名创建不同的文件夹,将 Excel 存储到对应的文件夹。服务只复制新产出的 Excel 文件,避免重复复制已存在的文件。/',/'keywords/':/'C#, Excel, 文件复制, 文件夹创建, 服务, 新文件, 复制, 文件名, 目标文件夹, 源文件夹, Microsoft.Office.Interop.Excel/',/'content/':/'使用 C# 语言编写服务,将某个文件夹下的 Excel 文件复制到另一个硬盘文件夹,并根据 Excel 文件名创建不同的文件夹,将 Excel 存储到对应的文件夹。服务只复制新产出的 Excel 文件,避免重复复制已存在的文件。//n//ncsharp//nusing System;//nusing System.IO;//nusing System.Linq;//nusing System.Runtime.InteropServices;//nusing Excel = Microsoft.Office.Interop.Excel;//n//nnamespace ExcelCopyService//n{//n class Program//n {//n static void Main(string[] args)//n {//n string sourceFolderPath = ///'C:////SourceFolder///';//n string destinationFolderPath = ///'D:////DestinationFolder///';//n//n // Get the list of Excel files in the source folder//n string[] excelFiles = Directory.GetFiles(sourceFolderPath, ///'*.xlsx///');//n//n foreach (string excelFile in excelFiles)//n {//n string fileName = Path.GetFileNameWithoutExtension(excelFile);//n string destinationPath = Path.Combine(destinationFolderPath, fileName);//n//n // Only copy the file if the destination folder does not exist already//n if (!Directory.Exists(destinationPath))//n {//n Directory.CreateDirectory(destinationPath);//n File.Copy(excelFile, Path.Combine(destinationPath, Path.GetFileName(excelFile)));//n Console.WriteLine($///'Copied {excelFile} to {destinationPath}///');//n }//n }//n//n Console.WriteLine(///'Press any key to exit.///');//n Console.ReadKey();//n }//n }//n}//n//n//n请确保您的项目引用了 Microsoft.Office.Interop.Excel 组件。如果未引用,请使用以下步骤://n//n1. 在 Visual Studio 中,右键单击项目,选择///'管理 NuGet 程序包///'.//n2. 在///'NuGet 程序包管理器///'窗口中,搜索 Microsoft.Office.Interop.Excel。//n3. 选择 Microsoft.Office.Interop.Excel,并点击///'安装///'按钮以引用它。//n//n此代码将遍历源文件夹中的所有 Excel 文件,并将它们复制到目标文件夹中的适当文件夹中。只有当目标文件夹不存在时才会进行复制,以避免重复复制已存在的文件。/
原文地址: https://www.cveoy.top/t/topic/pRjV 著作权归作者所有。请勿转载和采集!