Click here to Skip to main content
11,412,159 members (64,595 online)
Rate this: bad
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
Posted 11-May-13 1:52am
subbu a365
Raja Soosai at 11-May-13 6:54am
What you have tried?
Rate this: bad
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();
            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#[^]
Maciej Los at 11-May-13 8:01am
Rate this: bad
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"));
            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:

Please, follow this link: How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET[^]

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

  Print Answers RSS
0 OriginalGriff 348
1 Sergey Alexandrovich Kryukov 225
2 Sascha Lefévre 175
3 ProgramFOX 130
4 Maciej Los 110
0 Sergey Alexandrovich Kryukov 8,980
1 OriginalGriff 7,263
2 Maciej Los 3,500
3 Abhinav S 3,248
4 Peter Leow 3,059

Advertise | Privacy | Mobile
Web01 | 2.8.150414.5 | Last Updated 11 May 2013
Copyright © CodeProject, 1999-2015
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