我需要读取一个excel文件,搜索每个值的ID并将这些值保存在数据库中: Excel original
这是我的代码:
int rowCount = 51,colCount = 11;
Excel.Application xlApp = new Excel.Application();
Excel.Workbook xlWorkbook = xlApp.Workbooks.Open(@"C:\Users\melissa\Desktop\lista_items.xlsx");
Excel._Worksheet xlWorksheet = xlWorkbook.Sheets[1];
Excel.Range xlRange = xlWorksheet.UsedRange;
for (int i = 2; i <= rowCount; i++)
{
item = xlRange.Cells[i, 1].Value2.ToString();
id_color = consultas.buscar_color_codigo(xlRange.Cells[i, 2].Value2.ToString());
id_fabricante = consultas.buscar_fabricante(xlRange.Cells[i, 3].Value2.ToString());
id_size = consultas.buscar_talla(xlRange.Cells[i, 4].Value2.ToString());
descripcion = xlRange.Cells[i, 5].Value2.ToString();
id_body_type = consultas.buscar_body_type(xlRange.Cells[i, 6].Value2.ToString());
id_gender = consultas.buscar_genero(xlRange.Cells[i,7].Value2.ToString());
id_fabric_type = consultas.buscar_fabric_type(xlRange.Cells[i, 8].Value2.ToString());
id_fabric_percent = consultas.buscar_percent(xlRange.Cells[i, 9].Value2.ToString());
id_yarn = consultas.buscar_yarn(xlRange.Cells[i,10].Value2.ToString());
division = xlRange.Cells[i, 11].Value2.ToString();}
要保存在数据库中:
Conexion con_s = new Conexion();
SqlCommand com_s = new SqlCommand();
com_s.Connection = con_s.AbrirConexion();
com_s.CommandText = " INSERT INTO items(item,id_color,id_fabricante,id_size,descripcion,id_body_type,id_gender,id_fabric_type,id_fabric_percent,id_yarn,division)VALUES" +
"('" + item + "','" + id_color + "','" + id_fabricante + "','" + id_size + "','" + descripcion + "','" + id_body_type +
"','" + id_gender + "','" + id_fabric_type + "','" + id_fabric_percent + "','" + id_yarn + "','" + division + "' ) ";
com_s.ExecuteNonQuery();
con_s.CerrarConexion();
它仅搜索并保存10行,仅此而已。它没有显示任何错误,只是停止。我已经更改了httpRuntimeexecutionTimeout,仍然什么也没有。
任何帮助将不胜感激
答案 0 :(得分:0)
在上传excel文件之前,请从excel的编辑标签中清除所有格式和注释等。它影响excel的UsedRange属性。 如果您使用此行代码来获取上次使用的单元格和上一次使用的行,也会很有帮助。
int lastColumn = xlWorkSheet.LastColumnUsed().ColumnNumber();
int lastRow = xlWorkSheet.LastRowUsed().RowNumber();
希望有帮助。