Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How to read text file in c# technology and paste the same file to Excel?

I a text file in a folder and i wanna read the file using C# coding and paste the same file to Excel using C# ?? please Help me on the same

user-image
Question added by Neetesh Bangera , Software Developer cum Application Engineer , Globysn Innoventures (P) Ltd.
Date Posted: 2013/10/24
Amen Ayach
by Amen Ayach , .Net Team Leader, Project Coordinator and Senior Solution Designer , Netways

You'll need the following three functions:

 

 private List<string> GetFileTextList(string filename)

        {

            List<string> res = new List<string>();

            try

            {

                if (!System.IO.File.Exists(filename)) throw new System.IO.IOException("Ooops, file not found !!!");

 

                using (System.IO.StreamReader sr = new System.IO.StreamReader(filename))

                {

                    while (!sr.EndOfStream)

                    {

                        res.Add(sr.ReadLine());

                    }

                    sr.Close();

                }

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

                throw;

            }          

            return res;

        }

           

        /*=====================================================*/    

 

        public bool SaveTextToExcel(string path, List<string> sList)

        {

            bool res = false;

            Excel.Application xlApp = null;

            Excel.Workbook xlWorkBook = null;

            Excel.Worksheet xlWorkSheet = null;

 

            try

            {

                System.Globalization.CultureInfo oldCI = System.Threading.Thread.CurrentThread.CurrentCulture;

 

                object misValue = System.Reflection.Missing.Value;

 

                xlApp = new Excel.ApplicationClass();

                System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

 

                xlWorkBook = xlApp.Workbooks.Add(misValue);

                xlWorkSheet = (Excel.Worksheet)xlWorkBook.Sheets["sheet1"];

 

                for (int i =0; i < sList.Count; i++)

                {

                    xlWorkSheet.Cells[i +1,1] = sList[i];

                }

 

                xlWorkSheet.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookDefault, Type.Missing, Type.Missing,

                false, false, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlNoChange,

                Type.Missing, Type.Missing, Type.Missing);//, Type.Missing, Type.Missing);

                xlWorkBook.Close(null, null, null);

                xlApp.Quit();

 

                System.Threading.Thread.CurrentThread.CurrentCulture = oldCI;

                res = true;

 

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                if (xlApp != null)

                    releaseObject(xlApp);

                if (xlWorkBook != null)

                    releaseObject(xlWorkBook);

                if (xlWorkSheet != null)

                    releaseObject(xlWorkSheet);

            }

 

            return res;

        }

 

        /*=====================================================*/    

 

        private void releaseObject(object obj)

        {

            try

            {

                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

                obj = null;

            }

            catch (Exception)

            {

                obj = null;

            }

            finally

            {

                GC.Collect();

            }

        }

 

        /*=====================================================*/    

 

Usage :

       

      string SomeFilePath = "C:\t.txt"

      List<string> txt = GetFileTextList(SomeFilePath);

      SaveTextToExcel("C:\SomeExcelFile.xls", txt);

 

More Questions Like This