In previous projects, we often read the data of a row and a column in Excel. If we start to splice strings from subscript 0 every time, it's really not the same style of lazy people like me. Therefore, we wrote some codes to directly read the data of the corresponding cell in excel through the values of rows and columns
Read Excel to DataTable, portal: https://blog.csdn.net/a33130317/article/details/90018777
#region reads the data of a cell in excel // Enter the actual row and column of excel to read the corresponding data in DataTable private string getExcelData(DataTable dt, int x, string y) { int column = this.columnNameConvertInt[y.ToLower()]; if(dt.Rows[x - 1][column].ToString().Equals("")) { return ""; } return dt.Rows[x - 1][column].ToString(); } // Set columnNameConvertInt property: convert excel column name to int private void setColumnNameConvertInt() { columnNameConvertInt.Add("a", 0); columnNameConvertInt.Add("b", 1); columnNameConvertInt.Add("c", 2); columnNameConvertInt.Add("d", 3); columnNameConvertInt.Add("e", 4); columnNameConvertInt.Add("f", 5); columnNameConvertInt.Add("g", 6); columnNameConvertInt.Add("h", 7); columnNameConvertInt.Add("i", 8); columnNameConvertInt.Add("j", 9); columnNameConvertInt.Add("k", 10); columnNameConvertInt.Add("l", 11); columnNameConvertInt.Add("m", 12); columnNameConvertInt.Add("n", 13); columnNameConvertInt.Add("o", 14); columnNameConvertInt.Add("p", 15); columnNameConvertInt.Add("q", 16); columnNameConvertInt.Add("r", 17); columnNameConvertInt.Add("s", 18); columnNameConvertInt.Add("t", 19); columnNameConvertInt.Add("u", 20); columnNameConvertInt.Add("v", 21); columnNameConvertInt.Add("w", 22); columnNameConvertInt.Add("x", 23); columnNameConvertInt.Add("y", 24); columnNameConvertInt.Add("z", 25); columnNameConvertInt.Add("aa", 26); columnNameConvertInt.Add("ab", 27); columnNameConvertInt.Add("ac", 28); columnNameConvertInt.Add("ad", 29); columnNameConvertInt.Add("ae", 30); columnNameConvertInt.Add("af", 31); columnNameConvertInt.Add("ag", 32); columnNameConvertInt.Add("ah", 33); columnNameConvertInt.Add("ai", 34); columnNameConvertInt.Add("aj", 35); columnNameConvertInt.Add("ak", 36); columnNameConvertInt.Add("al", 37); columnNameConvertInt.Add("am", 38); columnNameConvertInt.Add("an", 39); columnNameConvertInt.Add("ao", 40); columnNameConvertInt.Add("ap", 41); columnNameConvertInt.Add("aq", 42); columnNameConvertInt.Add("ar", 43); columnNameConvertInt.Add("as", 44); columnNameConvertInt.Add("at", 45); columnNameConvertInt.Add("au", 46); columnNameConvertInt.Add("av", 47); columnNameConvertInt.Add("aw", 48); columnNameConvertInt.Add("ax", 49); columnNameConvertInt.Add("ay", 50); columnNameConvertInt.Add("az", 51); } #endregion
Method to read the data in Row 2 and column A (column name can be case sensitive):
getExcelData(dt, 2, "a")
Note: in the method of converting excel column name to int, you can also generate it through the method of char a+1. I'm really lazy this time