如何使用C#.net导入.CSV表单名称 [英] How to Import .CSV sheetname using C#.net

查看:63
本文介绍了如何使用C#.net导入.CSV表单名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



请帮我导入 .csv 文件表名称。

Hi all,

Please help me to import .csv file sheet name.

string full = Path.GetFullPath(excelFile);
string file = Path.GetFileName(full);
string dir = Path.GetDirectoryName(full);
connString = @"Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=\"" + dir + \\";"
                             +  "Extended Properties=\"text;HDR=No;FMT=Delimited\"";
objConn = new OleDbConnection(connString); 
string query = "SELECT * FROM " + file; 
OleDbDataAdapter dAdapter = new OleDbDataAdapter(query, connString);  
dAdapter.Fill(dTable);              
objConn.Open();
dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);





我知道连接字符串中的 .csv 扩展文件,我们无法为数据源提供文件路径(就像 .xls .xlsx file),而我们需要提供上传的 .csv 存在的文件夹路径。 。



我也提到过同样的...

我可以上传所有上传的 .csv dTable 中的文件值。

但是在检索工作表名称时,我无法传递上传的文件名,因为...



要检索我们需要写这行的上传文件的工作表名称



As I know for .csv extension files in connection string, we cant give file path for data source(as like as .xls or .xlsx file), instead we need to give the folder path in which uploaded .csv exists..

So same I have mentioned...
And I am able to get all uploaded .csv file values in dTable.
But while retrieving sheet names I am unable to pass Uploaded file name because...

To retrieve sheet name of an uploaded file we need to write this line

dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);



但是我在这里传递 objConn (其中包含数据源为dir)

所以, dt 检索该connectionstring dir文件夹中的所有文件(在 dt 我需要检索上传的 .csv中的工作表名称 file),所有这些文件都显示为工作表名称。



这个问题无法清除,我需要上传 .csv 文件表名..



怎么做?

谷歌过去3天,但我无法得到解决方案。



请帮忙。



提前致谢。


But here I am passing objConn(Which consists datasource as dir )
So, dt retrieves all files present in that connectionstring dir folder ( in dt I need to retrieve sheet names present in uploaded .csv file ), and all those files are showing as a sheet names.

This is the Issue unable to clear and I need uploaded .csv file sheetnames..

How to do that ?
I Google from past 3 days, but I am unable to get the solution.

Please help.

Thanks in advance.

推荐答案

工作表名称应以< filename>格式返回csv 并且您可以将该名称与现有连接字符串一起使用以访问该文件的记录。类似于:

The sheet names should be returned in the form <filename>#csv and you can use that name with the existing connection string to access the records of the file. Something like:
string tableName = dt.Rows[0].["TABLE_NAME"].ToString();
OleDbCommand command = new OleDbCommand(string.Format("Select * From [{0}]", tableName), objConn );
OleDbDataReader reader = command.ExecuteReader();


for (int i = 0; i < tablesNames.Rows.Count; i++)
                            {
                                string tableName = tablesNames.Rows[i]["TABLE_NAME"].ToString().Replace("


)。替换( )。替换( csv )。替换( CSV );
tableName = tableName.Replace( ' );
combomappingsheets.Items.Add(tableName.Trim());
}
", "").Replace("#", "").Replace("csv", "").Replace("CSV", ""); tableName = tableName.Replace("'", ""); combomappingsheets.Items.Add(tableName.Trim()); }


这篇关于如何使用C#.net导入.CSV表单名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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