Dieser Artikel beschreibt die Konvertierungsfunktion von Excel -Spaltennummern und in Java implementierten Briefen. Teilen Sie es für Ihre Referenz wie folgt weiter:
Wenn wir den Import und den Export von Excel implementieren, müssen wir den Benutzer häufig genau auffordern, die spezifische Excel -Zelle zu fordern. Hier müssen wir die Spaltennummern von Excel in die Nummer und die Buchstaben konvertieren. Heute wird diese Anforderung nur verwendet, also haben wir eine Demo geschrieben, um zusammenzufassen:
Java -Implementierung:
Paketstest;/** * Behandeln Sie die Excel-Spalte indextostr und strtoIndex * @author Stephen.huang * @Version 2015-7-8 */public class excelcolumn {public static void main (String [] args) {String colstr = "aa"; int colindex = excelcolstrtonum (colstr, colstr.length ()); System.out.println ("'" + colstr + "' Spaltenindex von" + colindex); colindex = 26; colstr = excelcolindextostr (colindex); System.out.println (colindex + "Spalte in Excel von" + colstr); colstr = "aaaa"; colindex = excelcolstronum (colstr, colstr.length ()); System.out.println ("'" + colstr + "' Spaltenindex von" + colindex); colindex = 466948; colstr = excelcolindextostr (colindex); System.out.println (colindex + "Spalte in Excel von" + colstr); } / ** * Excel -Spaltenindex begin 1 * @param colstr * @param Länge * @Return * / public static int excelcolstronum (String colstr, int länge) {int num = 0; int result = 0; für (int i = 0; i <länge; i ++) {char ch = colstr.charat (Länge - i - 1); num = (int) (ch - 'a' + 1); num *= math.pow (26, i); Ergebnis += num; } Rückgabeergebnis; } / ** * Excel Column Index begin 1 * @param columnIndex * @return * / public static String excelColIndextOStr (int columnIndex) {if (columnIndex <= 0) {return null; } String columnStr = ""; columnIndex--; do {if (columnstr.length ()> 0) {columnIndex--; } columnStr = ((char) (columnIndex % 26 + (int) 'a')) + columnstr; columnIndex = (int) ((columnIndex - columnIndex % 26) / 26); } while (columnIndex> 0); Rückgabespalte; }}Testergebnisse:
Spaltenindex von 'AA' Spalte in Excel of Z'aaaa 'Spaltenindex von 18279466948 In Excel von ZnSN
For more information about Java related content, please check out the topics of this site: "Summary of Java Operation Excel Skills", "Summary of Java+MySQL Database Programming", "Tutorial on Java Data Structure and Algorithm", "Summary of Java File and Directory Operation Skills" and "Summary of Java Operation DOM Node Skills"
Ich hoffe, dieser Artikel wird für Java -Programme aller hilfreich sein.