Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
code for read all text file names in the folder and create excel file and file names send to excel in c#.net
Posted
Comments
Raja Soosai 11-May-13 6:54am    
What you have tried?

Reading file names is pretty easy:
C#
string[] files = Directory.GetFiles(@"D:\Temp");

If you want the filename without the path then add:
C#
string[] justFiles = files.Select(f => Path.GetFileName(f)).ToArray();
Or
C#
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#[^]
 
Share this answer
 
Comments
Maciej Los 11-May-13 8:01am    
+5
Another way is to get all file names into DataTable[^] and than use CopyFromRecordset method[^] for MS Excel range.

C#
//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[^]
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900