この記事では、Javaに実装されたExcel列番号と文字の変換関数について説明します。次のように、参照のために共有してください。
Excelのインポートとエクスポートを実装するとき、特定のExcelセルに促すようにユーザーに正確に促す必要があります。ここでは、Excelの列番号を数と文字に変換する必要があります。今日、この要件が使用されているだけなので、要約するデモを書きました。
Javaの実装:
パッケージテスト;/** * excel列indextoStrおよびstrtoindex * @author stephen.huang * @version 2015-7-8 */public class excelcolumn {public static void main(string [] args){string colstr = "aa"; int colindex = excelcolstronum(colstr、colstr.length()); system.out.println( "'" + colstr + "'列インデックス" + colindex); colindex = 26; colstr = excelcolindextoStr(colindex); system.out.println(colindex + "column in excel of" + colstr); colstr = "aaaa"; colindex = excelcolstronum(colstr、colstr.length()); system.out.println( "'" + colstr + "'列インデックス" + colindex); colindex = 466948; colstr = excelcolindextoStr(colindex); system.out.println(colindex + "column in excel of" + colstr); } / ** * excel column index begin 1 * @param colstr * @param length * @return * / public static int excolstronum(string colstr、int length){int num = 0; int result = 0; for(int i = 0; i <length; i ++){char ch = colstr.charat(length -i -1); num =(int)(ch- 'a' + 1); num *= math.pow(26、i);結果 += num; } return result; } / ** * excel column index begin 1 * @param columnindex * @return * / public static string excelcolindextostost(int columnindex){if(columnindex <= 0){return null; }文字列columnStr = ""; columnindex-- do {if(columnStr.Length()> 0){columnIndex--; } columnStr =((char)(columnindex%26 +(int) 'a')) + columnStr; columnIndex =(int)((columnindex -columnindex%26) / 26); } while(columnindex> 0); Return Columnstr; }}テスト結果:
Z'AAAAのExcelの2726列の「AA」列インデックス '18279466948 column in excel of Znsn
Java関連のコンテンツの詳細については、このサイトのトピックをご覧ください:「Java Operation Excelスキルの要約」、「Java+MySQLデータベースプログラミングの概要」、「Javaデータ構造とアルゴリズムに関するチュートリアル」、Javaファイルとディレクトリ運用スキルの要約」、およびJava Operation Dom Nodeスキルの要約」
この記事がみんなのJavaプログラミングに役立つことを願っています。