C#Office interop Excel – 超出界限

我有一个程序,我需要维护(不是由我写的)实际function是做什么,它从互联网上获得一个Excel文件,然后将其转换为HTML(我甚至不知道为什么),然后尝试将其保存到一个新的CSV文件…

长话短说,我的问题在整个代码驻留在这两个部分

xl = new Microsoft.Office.Interop.Excel.Application(); xl.Visible = false; //p_sUBKPath is the path of the HTML file that has the converted excel file Microsoft.Office.Interop.Excel.Workbook workbook = xl.Workbooks.Open(p_sUBKPath, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); Microsoft.Office.Interop.Excel.Worksheet ws = (Microsoft.Office.Interop.Excel.Worksheet)workbook.Sheets[1]; Microsoft.Office.Interop.Excel.Range range = ws.UsedRange; object[,] values = (object[,])range.Value2; 

该代码有一个if语句,试图确保html中第一行的每个值都匹配所需的值

 if (Convert.ToString(values[1, 1]).ToUpper().Trim() == "DOMAIN NAME" && values[1, 2]).ToUpper().Trim() == "SHORT NAME" && values[1,3]).ToUpper().Trim() == "LONG NAME" ) { //do something } 

if语句返回true并跳转到else语句,经过几次debugging,并尝试了一些代码行,结果是值[1,2]和值[1,3]正在返回数组边界错误。 而值[1,1]的值是包含DOMAIN NAME,SHORT NAME和LONG NAME的整行,所以即使这个语句也返回flase

 if (Convert.ToString(values[1, 1]).ToUpper().Trim() == "DOMAIN NAME") { //do something } 

任何想法在代码中发生了什么?