在C#程序中读取Excel文件 [英] Reading an excel file in a c# program
本文介绍了在C#程序中读取Excel文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码从我的C#程序中的excel文件中读取数据,问题是,如果未明确打开要读取的文件,则会在带下划线的行处显示错误:
I use the following code to read data from an excel file in my c# program, the problem is that it gives an error at the underlined line if the file im supposed to read is not explicitly opened:
<br />
//Getting Sheet Name<br />
Excel.ApplicationClass app = new ApplicationClass();<br />
Excel.Workbook workBook = app.Workbooks.Open(textFileName.Text, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, false, false);<br />
Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;<br />
app.Visible = false;<br />
//-----------------------<br />
if (toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)<br />
{<br />
toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;<br />
}<br />
toolStripProgressBar1.PerformStep();<br />
string FileName = workSheet.Name;<br />
app.Workbooks.Close();<br />
<br />
string connectionString = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=" + textFileName.Text + ";";<br />
connectionString += "Extended Properties=\"Excel 8.0;HDR=YES;\"";<br />
//<br />
OleDbCommand myCommand = new OleDbCommand("Select * from [" + FileName + "$];");<br />
OleDbConnection myConnection = new OleDbConnection(connectionString);<br />
myConnection.Open();<br />
myCommand.Connection = myConnection;<br />
OleDbDataReader myReader = myCommand.ExecuteReader();<br />
MessageBox.Show("hello");<br />
while(myReader.Read())<br />
{<br />
if (toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)<br />
{<br />
toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;<br />
}<br />
toolStripProgressBar1.PerformStep();<br />
<br />
TT_DT = myReader.GetDateTime(0);<br />
Subject = myReader.GetString(1);<br />
TT_ID = myReader.GetValue(2).ToString();<br />
<br />
LoadID(TT_DT, Subject, TT_ID);<br />
}<br />
myConnection.Close();<br />
推荐答案
;);< br/> OleDbConnection myConnection =新的OleDbConnection(connectionString);< br/> myConnection.Open(); < br/> myCommand.Connection = myConnection;< br/> OleDbDataReader myReader = myCommand.ExecuteReader();< br/> MessageBox.Show("hello");< br/> while(myReader.Read())< br/> {< br/> 如果(toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)< br/> {< br/> toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;< br/> }< br/> toolStripProgressBar1.PerformStep();< br/> < br/> TT_DT = myReader.GetDateTime(0);< br/> Subject = myReader.GetString(1);< br/> TT_ID = myReader.GetValue(2).ToString();< br/> < br/> LoadID(TT_DT,主题,TT_ID);< br/> }< br/> myConnection.Close();< br/>
;");<br /> OleDbConnection myConnection = new OleDbConnection(connectionString);<br /> myConnection.Open();<br /> myCommand.Connection = myConnection;<br /> OleDbDataReader myReader = myCommand.ExecuteReader();<br /> MessageBox.Show("hello");<br /> while(myReader.Read())<br /> {<br /> if (toolStripProgressBar1.Value == toolStripProgressBar1.Maximum)<br /> {<br /> toolStripProgressBar1.Value = toolStripProgressBar1.Minimum;<br /> }<br /> toolStripProgressBar1.PerformStep();<br /> <br /> TT_DT = myReader.GetDateTime(0);<br /> Subject = myReader.GetString(1);<br /> TT_ID = myReader.GetValue(2).ToString();<br /> <br /> LoadID(TT_DT, Subject, TT_ID);<br /> }<br /> myConnection.Close();<br />
您是说它仅在Excel已打开文件的情况下起作用吗?那可能是限制以这种方式读取Excel文件,如果这样,我怀疑您可以在不更改读取方式的情况下对其进行修复.
Do you mean it only works if Excel has the file open already ? That may be a limitation of reading Excel files that way, if so, I doubt you can fix it without changing your method of reading them.
这篇关于在C#程序中读取Excel文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文