//在将数据添加到列表中时,我得到一个异常,即输入字符串的格式不正确。我正在尝试将第3列的所有数据存储在第3行,最后是列表。< / p>
public static int Get_Retail_Team_Id(string Incident_Path, Int32 Retail_Id)
{
int lastRow = 0;
List<Int32> List_Id = new List<Int32>();
Excel.Application MyApp;
Excel.Workbook MyBook;
Excel.Worksheet MySheet;
Excel.Range range;
MyApp = new Excel.Application();
MyBook = MyApp.Workbooks.Open(Incident_Path);
MySheet = (Excel.Worksheet)MyBook.Sheets["team"]; // Explict cast is not required here
lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;
for(int i=3; i<=lastRow; i++)
{
if(MySheet.Cells[i,1].Value !="")
{
int val = Convert.ToInt32(MySheet.Cells[i, 1].Value);//Here getting an exception that input string is not in a correct format
List_Id.Add(val);
}
else
{
break;
}
}
int Result=0;
foreach (Int32 id in List_Id)
{
if(Retail_Id==id)
{
Result=1;
break;
}
else
{
Result=0;
}
}
return Result;
}
答案 0 :(得分:2)
您可以尝试解析字符串,如果它是一个有效的int格式,如下所示
int j;
if (Int32.TryParse(MySheet.Cells[i, 1].Value, out j))
List_Id.Add(j);
else
Console.WriteLine("String could not be parsed." + MySheet.Cells[i, 1].Value);