如何检索excel文件名 [英] How to retrieve the excel file name

查看:83
本文介绍了如何检索excel文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码如下



My code as follows

int count = 0;
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
SqlConnection sqlConnection = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
DataSet ds = new DataSet();
cmd.CommandText = "select * from Empdetails";
cmd.CommandText += " where shifttype = @par ";
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
	string filePath = @"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls";
	System.IO.StreamWriter sw_In = new System.IO.StreamWriter(filePath);
	while (reader.Read())
	{
		if (count == 0)
		{
			for (int i = 0; i < reader.FieldCount; i++)
			{
				sw_In.AutoFlush = true;
				sw_In.Write(reader.GetName(i) + "\t");
			}
			sw_In.Write("\n");
			count = 1;
		}
		for (int i = 0; i < reader.FieldCount; i++)
		{
			sw_In.AutoFlush = true;
			sw_In.Write(reader[i].ToString() + "\t");
		}
		sw_In.Write("\n");
	}
}
reader.Close();
sqlConnection.Close();





当我运行上面的代码时,两个excel文件在C Folder下的桌面下载关注





when i run the above code two excel file is downloaded in desktop under C Folder as follows

1Excel
2Excel



i希望检索并显示上述Excel文件名。



我该怎么做才能退出excel文件名并显示


i want the above Excel file name to retrieved and displayed.

for that how can i do to retireve the excel file name and to be displayed

推荐答案

你使用 System.IO.tory.GetFiles(path \\to\\files)。这会将一个字符串数组返回给文件名。
You use the System.IO.tory.GetFiles("path\\to\\files"). That return a string array to file names.


我必须遗漏一些大的东西或者你不理解你应该写的代码。



我不明白你为什么在检索文件名时遇到问题,因为这段代码正在创建文件,而且为了创建文件,代码会给出文件名。

在这里,您在创建文件之前构建完整的文件名:

I must be missing something big or you don't understand the code you supposed to have written.

I don't understand why you have a problem retrieving the file names, because this code is creating the files, and to create the files, the code gives the file names.
Here, you build the full file name before creating the file:
string filePath = @"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls";



路径 C:\ Users \ God \Desktop \ DataDump \

文件名为 j +Excel

扩展名为。xls



Nota:您的代码很复杂,可以简化为:


The path is C:\Users\God\Desktop\DataDump\
The file name is j + "Excel"
The extension is ".xls"

Nota: your code is complicated and can be simplified to:

for (int i = 0; i < reader.FieldCount; i++)
{
    sw_In.AutoFlush = true;
    sw_In.Write(reader.GetName(i) + "\t");
}
sw_In.Write("\n");
while (reader.Read())
{
    for (int i = 0; i < reader.FieldCount; i++)
    {
        sw_In.AutoFlush = true;
        sw_In.Write(reader[i].ToString() + "\t");
    }
    sw_In.Write("\n");
}


这篇关于如何检索excel文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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