How to export data from one excel file to many excel files

basel farah 1 Reputation point
2021-12-29T16:02:04.723+00:00

Hi every body I have more than 200 excel files , and i need to import specific cells from one excel file to specific cells in these 200 excel files it is highlighted in yellow in the following photo , is there an easy way to this
161241-image.png

Microsoft 365 and Office Excel For business Windows
{count} votes

1 answer

Sort by: Most helpful
  1. David 151 Reputation points
    2021-12-31T10:06:52.333+00:00

    I made an example for you using Spire.XLS for .NET. Check it out.

    using System.IO;  
    using Spire.Xls;  
      
    namespace CoypCellRangeBetweenWorkbooks  
    {  
        class Program  
        {  
            static void Main(string[] args)  
            {          
                //Source file  
                string sourceExcel = "C:\\Users\\Administrator\\Desktop\\source.xlsx";  
                //Folder that contains your 200 files  
                string copyToFolder = "C:\\Users\\Administrator\\Desktop\\ExcelDocuments";  
                string outputFolder = "C:\\Users\\Administrator\\Desktop\\Output\\";  
                //Get file paths of your 200 files  
                string[] copyToFilePaths = Directory.GetFiles(copyToFolder);  
      
                //Load source Excel file  
                Workbook sourceWb = new Workbook();  
                sourceWb.LoadFromFile(sourceExcel);  
                //Get the first worksheet  
                Worksheet sourceSheet = sourceWb.Worksheets[0];  
      
                int startRow = 1;  
                int endRow = 4;  
                int j = 0;  
      
                Workbook temporaryWb = new Workbook();  
                for (int i = 0; i < copyToFilePaths.Length; i++)  
                {  
                    temporaryWb.LoadFromFile(copyToFilePaths[i]);  
                    Worksheet sheet = temporaryWb.Worksheets[0];  
      
                    //Get source range  
                    CellRange sourceRange = sourceSheet.Range[startRow + j, 4, endRow + j, 4];  
                    //Get copy-to range  
                    CellRange copyToRange = sheet.Range[1, 1, 4, 1];  
                    //Coyp cells from source sheet to another sheet  
                    sourceSheet.Copy(sourceRange, copyToRange, true);  
                    string fileName = string.Format("output-{0}.xlsx", i + 1);  
                    temporaryWb.SaveToFile(outputFolder + fileName, ExcelVersion.Version2013);  
                    j += 3;  
                }  
      
            }  
        }  
    }  
    

    161538-2021-12-31-180231.png

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.