Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
code for read all text file names in the folder and create excel file and file names send to excel in c#.net
Posted 11-May-13 1:52am
subbu a365
Comments
Raja Soosai at 11-May-13 6:54am
   
What you have tried?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Reading file names is pretty easy:
            string[] files = Directory.GetFiles(@"D:\Temp");
If you want the filename without the path then add:
            string[] justFiles = files.Select(f => Path.GetFileName(f)).ToArray();
Or
            string[] justFiles = files.Select(f => Path.GetFileNameWithoutExtension(f)).ToArray();
Saving to Excel is more complex, but this may help simplify it: Write Data to Excel using C#[^]
  Permalink  
Comments
Maciej Los at 11-May-13 8:01am
   
+5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Another way is to get all file names into DataTable[^] and than use CopyFromRecordset method[^] for MS Excel range.
 
            //get files into datatable
            string[] filePaths = Directory.GetFiles(@"F:\\Download\\", "*.txt",SearchOption.AllDirectories);
            DataTable dt = new DataTable("Files");
            DataColumn dc = new DataColumn("File", Type.GetType("String"));
            dt.Columns.Add(dc);
            for (int i = filePaths.GetLowerBound(0); i < filePaths.GetUpperBound(0); i++)
            {
                DataRow dr = dt.NewRow();
                dr["File"] = filePaths[i];
            }
 
            //create new workbook and insert data using CopyFromRecordset method ;)
            //follow the link: http://support.microsoft.com/kb/306023
 
Please, follow this link: How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET[^]
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100