我有一个Excel工作表我想读入数据表 - 除了Excel表格中的一个特定列之外,一切都很好.列,"产品ID",就像是值的组合##########和n#########.
我试图让OleDB通过将其读入数据集/数据表来自动处理所有内容,但"ProductID"中的任何值都会n######丢失,忽略并留空.我尝试通过使用datareader循环遍历每一行来手动创建我的DataTable,但结果完全相同.
这是代码:
// add the column names manually to the datatable as column_1, column_2, ...
for (colnum = 0; colnum < num_columns; colnum ++){
ds.Tables["products"].Columns.Add("column_" +colnum , System.Type.GetType("System.String"));
}
while(myDataReader.Read()){
// loop through each excel row adding a new respective datarow to my datatable
DataRow a_row = ds.Tables["products"].NewRow();
for (col = 0; col < num_columns; col ++){
try { a_row[col] = rdr.GetString(col); }
catch { a_row[col] = …Run Code Online (Sandbox Code Playgroud) 我需要访问Excel电子表格并将电子表格中的数据插入SQL数据库.但是主键是混合的,大多数是数字的,有些是字母数字.
我遇到的问题是,当数字和字母数字键在同一个电子表格中时,字母数字单元格返回空白值,而所有其他单元格返回其数据没有问题.
我正在使用OleDb方法来访问Excel文件.使用Command字符串检索数据后,我将数据放入DataAdapter,然后填充DataSet.我遍历DataSet中第一个DataTable中的所有行(dr).
我使用dr ["..."]引用列.ToString()
如果我在Visual Studio 2008中调试项目并查看"扩展属性",将鼠标悬停在"dr"上我可以查看DataRow的值,但应该是字母数字的主键是{}.其他值用引号括起来,但空白值有大括号.
这是C#问题还是Excel问题?
有没有人曾经遇到过这个问题,或者可能找到了解决方法/修复方法?
提前致谢.