Read Excel file (.xls or .xlsx) and copy from one folder to another

By 10th December 2018C#

First you have to add reference to read excel files. you can download or add reference from nuget package manager. link is below

https://www.nuget.org/packages/Microsoft.Office.Interop.Excel/ 

After that code is below:

class Program
    {
        public static Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

        static void Main(string[] args)
        {
            string readFolderPath = @"C:\Users\Mahaveer Jangid\Desktop\files";
            DirectoryInfo dir = new DirectoryInfo(readFolderPath);
            if (dir.Exists == true)
            {
                FileInfo[] files = dir.GetFiles("*.xls");
                foreach (FileInfo file in files)
                {
                    if (!file.Attributes.HasFlag(FileAttributes.Hidden))
                    {
                        string originalFilePath = file.FullName;
                        bool IsReadble = ReadFile(originalFilePath); //Method to check file is readble or not.

                        //If file successfully readble
                        if (IsReadble)
                        {
                            string passFolderPath = file.Directory + "\\Pass";
                            string _successDestinationPath = System.IO.Path.Combine(passFolderPath, DateTime.Now.ToString("yyyy-MM-dd fff") + "_" + file.Name);
                            if (!System.IO.Directory.Exists(passFolderPath))
                            {
                                System.IO.Directory.CreateDirectory(file.Directory + "\\Pass");
                            }
                            System.IO.File.Copy(originalFilePath, _successDestinationPath, false);
                        }
                        //If file not readble
                        else
                        {
                            string failFolderPath = file.Directory + "\\Fail";
                            string _failDestinationPath = System.IO.Path.Combine(failFolderPath, DateTime.Now.ToString("yyyy-MM-dd fff") + "_" + file.Name);
                            if (!System.IO.Directory.Exists(failFolderPath))
                            {
                                System.IO.Directory.CreateDirectory(file.Directory + "\\Fail");
                            }
                            System.IO.File.Copy(originalFilePath, _failDestinationPath, false);
                        }
                    }
                }

            }

        }


        public static bool ReadFile(string filepath)
        {
            bool IsReadble = false;
            Workbook workbook = excel.Workbooks.Open(filepath);
            Worksheet worksheet = workbook.Sheets[1];
            Range range = worksheet.UsedRange;
            int rowCount = range.Rows.Count;
            int colCount = range.Columns.Count;
            for (int i = 1; i <= rowCount; i++)
            {
                for (int j = 1; j <= colCount; j++)
                {
                    if (range.Cells[i, j] != null && range.Cells[i, j].Value2 != null)
                    {
                        IsReadble = true;
                    }
                }

            }
            return IsReadble;
        }
    }

Leave a Reply