บทความนี้อธิบายฟังก์ชั่นการแปลงของหมายเลขคอลัมน์ Excel และตัวอักษรที่ใช้ใน Java แบ่งปันสำหรับการอ้างอิงของคุณดังนี้:
เมื่อเราใช้การนำเข้าและส่งออก Excel เรามักจะต้องแจ้งให้ผู้ใช้ทราบอย่างถูกต้องเพื่อแจ้งเซลล์ Excel ที่เฉพาะเจาะจง ที่นี่เราต้องแปลงหมายเลขคอลัมน์ของ Excel เป็นหมายเลขและตัวอักษร วันนี้ข้อกำหนดนี้เพิ่งใช้ดังนั้นเราจึงเขียนตัวอย่างเพื่อสรุป:
การใช้งาน Java:
การทดสอบแพ็คเกจ;/** * จัดการกับคอลัมน์ excel indextoStr และ strtoindex * @author Stephen.huang * @version 2015-7-8 */คลาสสาธารณะ ExcelColumn {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {String colstr = "AA"; int colindex = excelcolstronum (colstr, colstr.length ()); System.out.println ("" " + colstr +" 'ดัชนีคอลัมน์ของ " + colindex); colindex = 26; colstr = excelColindExtOSTR (colindex); System.out.println (คอลัมน์ colindex + "ใน excel ของ" + colstr); colstr = "aaaa"; colindex = excelcolstronum (colstr, colstr.length ()); System.out.println ("" " + colstr +" 'ดัชนีคอลัมน์ของ " + colindex); colindex = 466948; colstr = excelColindExtOSTR (colindex); System.out.println (คอลัมน์ colindex + "ใน excel ของ" + colstr); } / ** * ดัชนีคอลัมน์ excel เริ่มต้น 1 * @param colstr * @param ความยาว * @return * / สาธารณะคงที่ int excelcolstronum (สตริง colstr ความยาว int) {int num = 0; int result = 0; สำหรับ (int i = 0; i <length; i ++) {char ch = colstr.charat (ความยาว - i - 1); num = (int) (ch - 'a' + 1); num *= math.pow (26, i); ผลลัพธ์ += num; } ผลตอบแทนผลลัพธ์; } / ** * ดัชนีคอลัมน์ Excel เริ่มต้น 1 * @param columnindex * @return * / สตริงคงที่สาธารณะ excelcolindextoStr (int columnindex) {ถ้า (columnindex <= 0) {return null; } String columnstr = ""; ColumnIndex--; ทำ {ถ้า (columnstr.length ()> 0) {columnindex--; } columnstr = ((char) (columnindex % 26 + (int) 'a')) + columnstr; columnindex = (int) ((columnindex - columnindex % 26) / 26); } ในขณะที่ (columnindex> 0); คืนคอลัมน์; -ผลการทดสอบ:
ดัชนีคอลัมน์ 'AA' ของคอลัมน์ 2726 ใน Excel of Z'AAAA 'ดัชนีคอลัมน์ 18279466948 คอลัมน์ใน Excel of ZNSN
สำหรับข้อมูลเพิ่มเติมเกี่ยวกับเนื้อหาที่เกี่ยวข้องกับ Java โปรดตรวจสอบหัวข้อของไซต์นี้: "สรุปการดำเนินงาน Java Excel Skills", "บทสรุปของ Java+MySQL Database Programming", "การสอนเกี่ยวกับโครงสร้างข้อมูล Java และอัลกอริทึม"
ฉันหวังว่าบทความนี้จะเป็นประโยชน์กับการเขียนโปรแกรม Java ของทุกคน