用于读取文件夹和文件名中的所有文本文件名的代码发送到excel。 [英] code for read all text file names in the folder and file names send to excel.

查看:79
本文介绍了用于读取文件夹和文件名中的所有文本文件名的代码发送到excel。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用于读取文件夹中所有文本文件名的代码,并创建excel文件和文件名发送到excel in c#.net

code for read all text file names in the folder and create excel file and file names send to excel in c#.net

推荐答案

读取文件名很简单:

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();



保存到Excel更复杂,但这可能有助于简化它:使用C#将数据写入Excel [ [ ^ ]


另一种方法是将所有文件名转换为DataTable [ ^ ]而不是使用 CopyFromRecordset方法 [ ^ ]用于MS Excel范围。 />


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





请点击以下链接:如何使用Visual C#2005或Visual C#.NET将数据传输到Excel工作簿 [ ^ ]


这篇关于用于读取文件夹和文件名中的所有文本文件名的代码发送到excel。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆