string table0 = textBox1.Text.Trim();string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+table0+";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [招生信息$] ";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); DataSet myDataSet = new DataSet(); myCommand.Fill(myDataSet, "[招生信息$]");
System.Data.DataTable ExcelTable = new System.Data.DataTable();
string excelname,excelcode,excelCID;
int d2 = 0;
int d1 = 0;
int t=myDataSet .Tables ["[招生信息$]"].Rows .Count ; for (int j = 2; j < t; j++)
{ string ExcelSN = myDataSet.Tables["[招生信息$]"].Rows[j][1].ToString();
string ExcelSS = myDataSet.Tables["[招生信息$]"].Rows[j][2].ToString();
string ExcelSB = myDataSet.Tables["[招生信息$]"].Rows[j][3].ToString(); excelname = myDataSet.Tables["[招生信息$]"].Rows[j][0].ToString();
excelCID = myDataSet.Tables["[招生信息$]"].Rows[j][4].ToString(); excelcode = myDataSet.Tables["[招生信息$]"].Rows[j][5].ToString(); myConn.Close(); //以上是对excel表的读取
}到excelcode这提示“无法取到列5这是什么原因,请各位大侠们指点一下,谢谢!
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [招生信息$] ";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); DataSet myDataSet = new DataSet(); myCommand.Fill(myDataSet, "[招生信息$]");
System.Data.DataTable ExcelTable = new System.Data.DataTable();
string excelname,excelcode,excelCID;
int d2 = 0;
int d1 = 0;
int t=myDataSet .Tables ["[招生信息$]"].Rows .Count ; for (int j = 2; j < t; j++)
{ string ExcelSN = myDataSet.Tables["[招生信息$]"].Rows[j][1].ToString();
string ExcelSS = myDataSet.Tables["[招生信息$]"].Rows[j][2].ToString();
string ExcelSB = myDataSet.Tables["[招生信息$]"].Rows[j][3].ToString(); excelname = myDataSet.Tables["[招生信息$]"].Rows[j][0].ToString();
excelCID = myDataSet.Tables["[招生信息$]"].Rows[j][4].ToString(); excelcode = myDataSet.Tables["[招生信息$]"].Rows[j][5].ToString(); myConn.Close(); //以上是对excel表的读取
}到excelcode这提示“无法取到列5这是什么原因,请各位大侠们指点一下,谢谢!
excelcode 看一下它的值是多少。应该是没取到吧~
debug调试,看myDataSet .Tables ["[招生信息$]"]这张表里的数据看读取到的内容是什么
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + table0 + ";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [招生信息$] ";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet, "[招生信息$]");
//System.Data.DataTable ExcelTable = new System.Data.DataTable(); dataGridView1.DataSource = myDataSet.Tables["[招生信息$]"];
dataGridView1中找不到第excel表中F列数据。
string table0 = textBox1.Text.Trim();string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source ="+table0+";Extended Properties=\"Excel 8.0;HDR=NO;IMEX=1\"";
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [招生信息$] ";
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn); DataSet myDataSet = new DataSet(); myCommand.Fill(myDataSet, "[招生信息$]");
System.Data.DataTable ExcelTable = new System.Data.DataTable();
string excelname,excelcode,excelCID;
int d2 = 0;
int d1 = 0;
int t=myDataSet .Tables ["[招生信息$]"].Rows .Count ;
int l1 = myDataSet.Tables["[招生信息$]"].Columns.Count;
这里l1=5,为什么excelcode = myDataSet.Tables["[招生信息$]"].Rows[j][5].ToString();会提示没有列5?请大家帮忙看一下,谢谢!
第二,如果读取excel表中列数,是有F列的。
不知道这是什么原因?
F列手动给里边从上到下加入空格在保存就可以读取了,试试