简体   繁体   English

使用从Excel文件读取的内容执行SQL Server查询

[英]Execute SQL Server query using content read from Excel file

I'm using the following code to read content from a spreadsheet then populate a gridview with the contents. 我正在使用以下代码从电子表格中读取内容,然后使用内容填充gridview。

Instead of populating the gridview with the contents of the spreadsheet, I'd like to read each line of the spreadsheet and run a SQL Server query on the value of each line. 我不想用电子表格的内容填充gridview,而是要读取电子表格的每一行,并对每一行的值运行SQL Server查询。 I'd like to populate the gridview with the resulting data from these queries. 我想用这些查询的结果数据填充gridview。

I'm having issues figuring out how to read the excel file line by line then inserting these values into a query. 我在弄清楚如何逐行读取excel文件然后将这些值插入查询中时遇到问题。 Any assistance would be very much appreciated. 任何帮助将不胜感激。

//Read Data from First Sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT sku From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();

//Bind Data to GridView
GridView1.Caption = Path.GetFileName(FilePath);
GridView1.DataSource = dt;
GridView1.DataBind();

Try using Microsoft Excel Interop. 尝试使用Microsoft Excel Interop。

http://msdn.microsoft.com/en-us/library/ms173186(v=vs.80).aspx http://msdn.microsoft.com/en-us/library/ms173186(v=vs.80).aspx

http://www.dotnetperls.com/excel http://www.dotnetperls.com/excel

It's not difficult, just google it. 这并不困难,只需用谷歌搜索即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM