读取Excel和CSV文件数据
2011-11-17 16:19
531 查看
Excel:
string fileNameString = this.MapPath(".");
fileNameString += "..\\..\\..\\..\\data\\ExcelData.xls";
// Create connection object by using the preceding connection string.
string sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileNameString + ";Extended Properties=\"Excel 8.0;HDR=YES\"";
OleDbConnection myConnection = new OleDbConnection( sConn );
myConnection.Open();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new OleDbCommand to return data from worksheet.
OleDbCommand myCommand = new OleDbCommand( "Select * From [data1$A1:E25]", myConnection );
// create a database reader
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
CSV:
// Create a select statement and a connection string.
string mySelectQuery = "Select * from "+ file;
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+
path+ ";Extended Properties=\"Text;HDR=No;FMT=Delimited\"";
OleDbConnection myConnection = new OleDbConnection(ConStr);
// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
// open the connection
myCommand.Connection.Open();
// create a database reader
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
string fileNameString = this.MapPath(".");
fileNameString += "..\\..\\..\\..\\data\\ExcelData.xls";
// Create connection object by using the preceding connection string.
string sConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
fileNameString + ";Extended Properties=\"Excel 8.0;HDR=YES\"";
OleDbConnection myConnection = new OleDbConnection( sConn );
myConnection.Open();
// The code to follow uses a SQL SELECT command to display the data from the worksheet.
// Create new OleDbCommand to return data from worksheet.
OleDbCommand myCommand = new OleDbCommand( "Select * From [data1$A1:E25]", myConnection );
// create a database reader
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
CSV:
// Create a select statement and a connection string.
string mySelectQuery = "Select * from "+ file;
string ConStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+
path+ ";Extended Properties=\"Text;HDR=No;FMT=Delimited\"";
OleDbConnection myConnection = new OleDbConnection(ConStr);
// create a database command on the connection using query
OleDbCommand myCommand = new OleDbCommand(mySelectQuery, myConnection);
// open the connection
myCommand.Connection.Open();
// create a database reader
OleDbDataReader myReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection);
相关文章推荐
- excel、csv、txt文件数据读取
- CSV,EXCEL文件数据读取
- R—读取数据(导入csv,txt,excel文件)
- Python导出数据到Excel可读取的CSV文件的方法
- 网页端中将表格数据和JSON数据读取(导出)到本地的excel和csv文件中
- Python导出数据到Excel可读取的CSV文件的方法
- R—读取数据(导入csv,txt,excel文件)
- R—读取数据(导入csv,txt,excel文件)
- Java数据导入功能之读取Excel文件实例
- 利用pandas模块读取csv文件和excel表格,并用matplotlib画图
- 转换成CSV文件、Word、Excel、PDF等的方法--读取CSV文件的方法
- Java使用poi读取excel数据(excel可能很大,先转换为csv再读取)
- Java中读取Excel文件的内容和导出数据到Excel文件
- Python使用pandas & pymysql读取MySQL数据到csv文件中
- python3将csv文件中的两列数据读取出来,并且按行写入txt文本之中
- Java IO流将数据输出到.csv文件中,excel打开时乱码问题的解决
- R语言读CSV、txt文件方式以及read.table read.csv 和readr(大数据读取包)
- OLEDB读取Excel文件丢失部分数据
- 从Excel文件读取数据表
- 直接读取Excel文件数据