POI操作Excel:int类型的列号转换为真实的Excel列号名称

在用POI做Excel通用导出时,需要对指定列进行求和sum。例如需要对第3列的100行数据求和,公式为sum(C1:C100),在POI中只知道当前列号的索引是2,如何完成从列号2--->C的转换。 我写了一个方法进行转换,如下

	public static String getColumnNum(int i) {
		String strResult = "";
		int intRound = i / 26;
		int intMod = i % 26;
		if (intRound != 0) {
			strResult = String.valueOf(((char) (intRound + 64)));
		}
		strResult += String.valueOf(((char) (intMod + 64)));
		return strResult;
	}


		/**
		 * Converts an Excel column name like "C" to a zero-based index.
		 * 
		 * @param name
		 * @return Index corresponding to the specified name
		 */
		private int nameToColumn(String name) {
			int column = -1;
			for (int i = 0; i < name.length(); ++i) {
				int c = name.charAt(i);
				column = (column + 1) * 26 + c - 'A';
			}
			return column;
		}


猜你喜欢

转载自huiy.iteye.com/blog/1851347
今日推荐