C #: est-il possible d'utiliser Letter Coords (A1) avec workSheet.Cells?

J'ai récemment utilisé powershell pour automatiser certaines choses avec Excel, et je pourrais tout simplement utiliser A1, A2, etc. Avec C #, il semble qu'il soit nécessaire d'utiliser [1,1], (style de coordonnées) ou bien vous obtenez un type décalage. Voici le code avec lequel je travaille:

//Generating User and Password int startCoordI = Int32.Parse(startCoord); int endCoordI = Int32.Parse(endCoord); int userCoordI = Int32.Parse(userCoord); int passwordCoordI = Int32.Parse(passwordCoord); int value = startCoordI; ssortingng Username = Convert.ToSsortingng(workSheet.Cells[userCoord, startCoordI].Value); MessageBox.Show(Username); ssortingng Password = Convert.ToSsortingng(workSheet.Cells[passwordCoord, startCoordI].Value); MessageBox.Show(Password); try { for (I = startCoordI; I <= endCoordI; I++) { System.Diagnostics.ProcessStartInfo proccessStartInfo = new System.Diagnostics.ProcessStartInfo("net", "user " + Username + " " + Password + " /add /passwordchg:no"); System.Diagnostics.Process proc = new System.Diagnostics.Process { StartInfo = proccessStartInfo }; proc.StartInfo.RedirectStandardOutput = true; proc.StartInfo.UseShellExecute = false; proccessStartInfo.CreateNoWindow = true; proc.Start(); //new user value++; Username = Convert.ToSsortingng(workSheet.Cells[userCoord, value].Value); Password = Convert.ToSsortingng(workSheet.Cells[passwordCoord, value].Value); } } catch (Exception ex) { MessageBox.Show(ex.Message); } 

Ce n'est pas insortingnsèquement un problème, mais il serait bon de pouvoir utiliser les coordonnées de style A1. Merci!

Pas insortingnsèquement, mais il serait vraiment facile de créer une méthode d'extension pour ce faire (je n'ai pas Excel installé, donc si un type est incorrect, corrigez-le;)):

 public static class ExcelExtensions { public static Range Named(this Range Cells, ssortingng CellName) { char cellLetter = CellName.Subssortingng(0, 1).ToUpper()[0]; int xCoordinate = (cellLetter - 'A') + 1; int yCoordinate = int.Parse(CellName.Subssortingng(1)); return Cells[yCoordinate, xCoordinate]; } } 

Maintenant, vous pouvez:

 workSheet.Cells.Named("B3").Value ..... 

À mon avis, il y aura une erreur si vous essayez de le faire avec des choses comme BA1 ou AAA1.

Cela devrait être juste:

 public static class ExcelExtensions { public static exc.Range Named(this exc.Range Cells, ssortingng CellName) { //Get Letter char[] charArray = CellName.ToCharArray(); ssortingng strLetter = ssortingng.Empty; foreach (char letter in charArray) { if (Char.IsLetter(letter)) strLetter += letter.ToSsortingng(); } //Convert Letter to Number double value = 0; if (strLetter.Length > 1) { foreach (char letter in strLetter) { if (value == 0) { value = (letter - 'A' + 1) * Math.Pow(26, (strLetter.Length -1)); } else { value += (letter - 'A' + 1); } } } else { char[] letterarray = strLetter.ToCharArray(); value = (letterarray[0] - 'A') + 1; } // ReadOut Number ssortingng strNumber = ssortingng.Empty; foreach (char numChar in CellName.ToCharArray()) { if (Char.IsNumber(numChar)) strNumber += numChar.ToSsortingng(); } return Cells[strNumber, value]; } } 

Je sais, c'est un code désordonné, mais ça marche 🙂