1. 程式人生 > >JS 數字轉換為EXCEL字母列

JS 數字轉換為EXCEL字母列

/**

 * Convert from numeric position to letter for column names in Excel

 * @param  {int} n Column number

 * @return {string} Column letter(s) name

 */

function createCellPos( n ){

	var ordA = 'A'.charCodeAt(0);

	var ordZ = 'Z'.charCodeAt(0);

	var len = ordZ - ordA + 1;

	var s = "";



	while( n >= 0 ) {

		s = String.fromCharCode(n % len + ordA) + s;

		n = Math.floor(n / len) - 1;

	}



	return s;

}