Excel文件处理 [英] Excel File Processing
本文介绍了Excel文件处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在这里有一些代码,这很困扰我.我有一个要处理的Excel文件,当我尝试访问一行中的列时,没有任何数据.任何帮助将不胜感激:
I have some code here and it is stumping me. I have an Excel file that I am trying to process, when I try to access a column in a row I don''t get any data. Any help would be appreciated:
try
{
OleDbConnection ExcelConnection = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtFileName.Text + "'" + ";Extended Properties=Excel 8.0;");
OleDbDataAdapter ExcelCommand = new OleDbDataAdapter("select * from [Contacts$]", ExcelConnection);
ExcelCommand.Fill(dtExcel, "Excel");
ExcelConnection.Close();
if (ValidRec()) CrtNwRecords();
else MessageBox.Show("Errors Exist in Spreadsheet");
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
private bool ValidRec()
{
for (int x = 0; x < dtExcel.Tables[0].Rows.Count; x++)
{
if (dtExcel.Tables[0].Rows[x][0].ToString() == "") MessageBox.Show(dtExcel.Tables[0].Rows[x][0].ToString()); //First Name
}
return true;
}
<pre></pre>
推荐答案
",ExcelConnection) ExcelCommand.Fill(dtExcel," ); ExcelConnection.Close(); if(ValidRec())CrtNwRecords(); else MessageBox.Show(" ) ; } 抓住(例外) { MessageBox.Show(ex.ToString()); } } 私人布尔ValidRec() { for(int x = 0 ; x< dtExcel.表[0] .Rows.Count; x ++) { if(dtExcel.Tables [ 0 ].Rows [x] [ 0 ].ToString()== " )MessageBox.Show(dtExcel.Tables [ 0 ].rows [x] [ 0 ].ToString()); //名字 } 返回true ; }
", ExcelConnection); ExcelCommand.Fill(dtExcel, "Excel"); ExcelConnection.Close(); if (ValidRec()) CrtNwRecords(); else MessageBox.Show("Errors Exist in Spreadsheet"); } catch(Exception ex) { MessageBox.Show(ex.ToString()); } } private bool ValidRec() { for (int x = 0; x < dtExcel.Tables[0].Rows.Count; x++) { if (dtExcel.Tables[0].Rows[x][0].ToString() == "") MessageBox.Show(dtExcel.Tables[0].Rows[x][0].ToString()); //First Name } return true; }
<pre></pre>
这对我有用:
This worked for me:
OleDbConnection ExcelConnection = new OleDbConnection(@"provider=Microsoft.Jet.OLEDB.4.0;Data Source='c:\temp\excel_data_file.xls';Extended Properties=Excel 8.0;");
OleDbDataAdapter ExcelCommand = new OleDbDataAdapter("select * from [Tabelle1
",ExcelConnection); DataTable dtExcel = 新 DataTable(" ); ExcelCommand.Fill(dtExcel); Console.WriteLine(" ); Console.WriteLine(" + dtExcel.Columns [" + dtExcel.Columns [" ); foreach (DataRow dr in dtExcel.Rows中) { Console.WriteLine(" + dr [ 0 ].ToString()+ " + dr [
", ExcelConnection); DataTable dtExcel = new DataTable("is_wuascht"); ExcelCommand.Fill(dtExcel); Console.WriteLine("list of column names"); Console.WriteLine("column name: " + dtExcel.Columns[0].ColumnName); Console.WriteLine("column name: " + dtExcel.Columns[1].ColumnName); Console.WriteLine("row data"); foreach (DataRow dr in dtExcel.Rows) { Console.WriteLine("Row: " + dr[0].ToString() + ":" + dr[1].ToString()); }
这篇关于Excel文件处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文