EXCEL中的数据,这是第一列,后面的省略
室号
401A
402B
403
404
405A
406B
string connect = @"Provider=Microsoft.JET.OLEDB.4.0; data source=E:\sandbox\dotnet\database\new.xls;Extended Properties=Excel 8.0;";
string sql = "select * from [统计$]";
OleDbConnection db_conn = new OleDbConnection(connect);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(sql, db_conn);foreach (DataRow row in dt.Rows)
{
string s = (string)row["室号"];
MessageBox.Show(s);
}
db_conn.Close();开始两个都正常,到第三个竟然报Unable to cast object of type 'System.DBNull' to type 'System.String'
接受不了唯一的区别就是开头两个带字母,第三个是数字,但我已经在excel中将格子的属性改为文本了
而且dt.Columns["室号"].DataType也是String
这不是折腾嘛
大家帮忙,感谢万分
室号
401A
402B
403
404
405A
406B
string connect = @"Provider=Microsoft.JET.OLEDB.4.0; data source=E:\sandbox\dotnet\database\new.xls;Extended Properties=Excel 8.0;";
string sql = "select * from [统计$]";
OleDbConnection db_conn = new OleDbConnection(connect);
OleDbDataAdapter adapter = new OleDbDataAdapter();
adapter.SelectCommand = new OleDbCommand(sql, db_conn);foreach (DataRow row in dt.Rows)
{
string s = (string)row["室号"];
MessageBox.Show(s);
}
db_conn.Close();开始两个都正常,到第三个竟然报Unable to cast object of type 'System.DBNull' to type 'System.String'
接受不了唯一的区别就是开头两个带字母,第三个是数字,但我已经在excel中将格子的属性改为文本了
而且dt.Columns["室号"].DataType也是String
这不是折腾嘛
大家帮忙,感谢万分
修改如下:foreach (DataRow row in dt.Rows)
{
string s = "";
if(row["室号"] != null)
{
string s = (string)row["室号"];
}
MessageBox.Show(s);
}
http://blog.csdn.net/chengking/archive/2005/10/07/496656.aspx
还是报同样的错误
因为row["室号"]不是null是DBNull
我前面这样写
foreach(DataRow row in dt.Rows) {
object o = row["室号"];
MessageBox.Show(o.ToString());
}
虽然可以避免问题不报错
但其中的403,404取不出来
非常奇怪401A
402B
403
404
405A
406B我将上面的excel中数据中的字符全部去掉
401
402
403
404
405
406还是同样的代码,string s = (string)row["室号"];
竟然报错Unable to cast object of type 'System.Double' to type 'System.String'
改成double d = (double)row["室号"];
MessageBox成功!
这我就更想不通了,我在excel中已经将格子的属性改为文本,确定没有绿色三角,怎么会这样?
excel中列数据的类型到底怎么来确定?
这句的意思明明是DBNULL.你把你的语句F9跟踪出来放到SQl查询分析器中运行一下.
错误不在程序里,在excel中
excel认为404这个单元格不是string类型的
要扭曲一下把他强行先改成404aaa再去掉404,之后有绿色三角提示强行存文本才行
妖怪啊
毕竟excel不是database,不能直接设置类型...
大家是怎么做的?
{
s = Convert.ToString(row["室号"]);
MessageBox.Show(s);
}
{
if(!oDS.Tables[0].Rows[i][0].Equals(System.DBNull.Value))
{
string sMessage= oDS.Tables[0].Rows[i][0].ToString().Trim();
}
} 樓主可以試著這樣讀取數據.而且據我的經驗所知,讀取EXCEL時,每列的類型 是通過該列的
第一行值得实際類型來確定的.而且如果你後面的數據格下拉過,即時沒有內容他也會讀取,只是值是System.DBNull.
也就是的oDS.Tables[0].Rows.Count值會大於你的实際所有的值的行數.
第一行值得实際類型來確定的.????
第一列肯定是文本吧应该是第二列吧
如果如下所示
VGA 標準工時與ON LINE人力一覽表
客戶 機 種 聯板數 SMT點數/ pcs VGA各線別C.T (Sec)
Total BOT TOP A~C B C D E F G "other
lines"
CP QP CP QP
168 8873-060 4 637 356 16 245 20 6.2 3 4 W 12
168 8808-320 4 145 145 2 2 2 W 2 2T T
168 8808-320 4 145 145 r
168 8878-230 4 412 180 6 214 12 4t
168 8830-030 2 672 318 19 330 5 6.2 23.29 23.29' 23.29' 23.29
你就知道了. 而且讀取 EXCEL時都可以轉化為XML格式.NET是如何轉化的樓主感興趣可以去研究一下.
那么如果excel
1--------------------------
室号
401A
402B
403
404
405A
406B2--------------------------
室号
401
402B
403
404
405A
406B这两种会有区别吗?