1. CellReferenceUtil
package edu.xmu.excel.util; public class CellReferenceUtil { /** * Input Coordination: C<br/> * Output Column Number: 2<br/> * * @param coordName * @return colIndex: 0 based index */ public static int getColIndex(String colName) { colName = colName.toUpperCase(); int value = 0; for (int i = 0; i < colName.length(); i++) { int delta = colName.charAt(i) - 64; value = value * 26 + delta; } int colIndex = value - 1; return colIndex; } /** * Input column index: 2 <br/> * Output column name: C <br/> * * @param colIndex * @return */ public static String getColName(int colIndex) { int quotient = (colIndex) / 26; if (quotient > 0) { return getColName(quotient - 1) + (char) ((colIndex % 26) + 65); } else { return "" + (char) ((colIndex % 26) + 65); } } /** * Input coord: C8<br/> * Output Row Number: 7<br/> * * @param coordName * @return rowIndex starts with 0 */ public static int getRowIndex(String rowName) { int rowIndex = Integer.parseInt(rowName) - 1; return rowIndex; } /** * Input rowIndex: 7 <br/> * Output rowName: 8 <br/> * * @param rowIndex * @return */ public static String getRowName(int rowIndex) { int rowName = rowIndex + 1; return String.valueOf(rowName); } /** * Input pos: col = 2, row = 7<br/> * Output coord: C8<br/> * * @param colIndex * @param rowIndex * @return */ public static String getCoordName(int colIndex, int rowIndex) { String colName = getColName(colIndex); String rowName = getRowName(rowIndex); return colName + rowName; } /** * Input coordName: C8 <br/> * Output colIndex: 2 <br/> * * @param coordName * @return colIndex: Starts with 0 */ public static int getColIndexByCoordName(String coordName) { String[] colAndRowName = splitColAndRow(coordName); String colName = colAndRowName[0]; return getColIndex(colName); } /** * Input coordName: C8 <br/> * Output rowIndex: 7 <br/> * * @param coordName * @return rowIndex: 0 based index */ public static int getRowIndexByCoordName(String coordName) { String[] colAndRowName = splitColAndRow(coordName); String rowName = colAndRowName[1]; return getRowIndex(rowName); } /** * Input coordName: C8 <br/> * Output : String[]{C, 8} <br/> * * @param coordName * @return */ private static String[] splitColAndRow(String coordName) { int rowNumStartIndex = 0; for (int i = 0; i < coordName.length(); i++) { char ch = coordName.charAt(i); if (Character.isDigit(ch)) { rowNumStartIndex = i; break; } } String colName = coordName.substring(0, rowNumStartIndex); String rowName = coordName.substring(rowNumStartIndex); return new String[] { colName, rowName }; } }
2. Test case:
package edu.xmu.excel.util; import static org.junit.Assert.assertEquals; import org.junit.Test; public class CellReferenceUtilTest { /** * Test method for * {@link edu.xmu.excel.util.CellReferenceUtil#getColIndex(String)} */ @Test public void getColIndexTest() { int colIndex = CellReferenceUtil.getColIndex("A"); assertEquals(0, colIndex); colIndex = CellReferenceUtil.getColIndex("AA"); assertEquals(26, colIndex); colIndex = CellReferenceUtil.getColIndex("AAA"); assertEquals(26 * 26 + 26, colIndex); } /** * Test method for * {@link edu.xmu.excel.util.CellReferenceUtil#getColName(int)} */ @Test public void getColNameTest() { String colName = CellReferenceUtil.getColName(0); assertEquals("A", colName); colName = CellReferenceUtil.getColName(26); assertEquals("AA", colName); colName = CellReferenceUtil.getColName(26 * 26 + 26); assertEquals("AAA", colName); } /** * Test method for * {@link edu.xmu.excel.util.CellReferenceUtil#getRowIndex(String)} */ @Test public void getRowIndexTest() { int rowIndex = CellReferenceUtil.getRowIndex("8"); assertEquals(7, rowIndex); rowIndex = CellReferenceUtil.getRowIndex("27"); assertEquals(26, rowIndex); } /** * Test method for * {@link edu.xmu.excel.util.CellReferenceUtil#getRowName(int)} */ @Test public void getRowNameTest() { String rowName = CellReferenceUtil.getRowName(7); assertEquals("8", rowName); rowName = CellReferenceUtil.getRowName(26); assertEquals("27", rowName); } /** * Test method for * {@link edu.xmu.excel.util.CellReferenceUtil#getColIndexByCoordName(String)} */ @Test public void getColIndexByCoordNameTest() { int colIndex = CellReferenceUtil.getColIndexByCoordName("C8"); assertEquals(2, colIndex); colIndex = CellReferenceUtil.getColIndexByCoordName("AA21"); assertEquals(26, colIndex); colIndex = CellReferenceUtil.getColIndexByCoordName("AAA21"); assertEquals(26 * 26 + 26, colIndex); } /** * Test method for * {@link edu.xmu.excel.util.CellReferenceUtil#getRowIndexByCoordName(String)} */ @Test public void getRowIndexByCoordNameTest() { int rowIndex = CellReferenceUtil.getRowIndexByCoordName("C8"); assertEquals(7, rowIndex); rowIndex = CellReferenceUtil.getRowIndexByCoordName("AA21"); assertEquals(20, rowIndex); rowIndex = CellReferenceUtil.getRowIndexByCoordName("AA222"); assertEquals(221, rowIndex); } /** * Test method for * {@link edu.xmu.excel.util.CellReferenceUtil#getCoordName(int, int)} */ @Test public void getCoordNameTest() { String coordName = CellReferenceUtil.getCoordName(2, 7); assertEquals("C8", coordName); coordName = CellReferenceUtil.getCoordName(26, 21); assertEquals("AA22", coordName); coordName = CellReferenceUtil.getCoordName(26 * 26 + 26, 21); assertEquals("AAA22", coordName); } }
相关推荐
org.apache.poi:poi:4.1.2 org.apache.poi:poi-ooxml:4.1.2 org.apache.poi:poi-ooxml-schemas:4.1.2 org.apache.xmlbeans:xmlbeans:3.1.0 com.github.virtuald:curvesapi:1.06 com.zaxxer:SparseBitSet:1.2 commons...
implementation group: 'org.apache.poi', name: 'poi', version: '***' Gradle (Short): implementation 'org.apache.poi:poi:***' Gradle (Kotlin): implementation("org.apache.poi:poi:***") ``` # 含有的 ...
java poi官方文档,开发java poi必备,纯java支持微软办公软件的类库
implementation group: 'org.apache.poi', name: 'poi-ooxml', version: '***' Gradle (Short): implementation 'org.apache.poi:poi-ooxml:***' Gradle (Kotlin): implementation("org.apache.poi:poi-ooxml:***...
标签:apache、poi、中英对照文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用...
java利用POI技术上传&导出Excel表格并保存数据至数据库
标签:apache、poi、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,文档中的代码和结构保持不变,注释和说明精准翻译,请放心使用。
JAVA poi-4.1.2 完整包,操作excel,word,ppt必备。Apache POI是Apache软件基金会的开源项目,POI提供API给Java程序对Microsoft Office格式档案读和写的功能。
implementation group: 'org.apache.poi', name: 'poi', version: '***' Gradle (Short): implementation 'org.apache.poi:poi:***' Gradle (Kotlin): implementation("org.apache.poi:poi:***") ``` # 含有的 ...
基于:iText 2.1.7,OpenPdf,JExcelAPI,POI库。 基于Xml的输入源,带有集成的WebJava环境对象。 包括动态标签的结构。 演示...
Java实现Excel读写的poi 5.2.1版本jar
JAVA POI Excel转Html,代码和所需的jar都在压缩包,项目在线预览需求,实现后分享下
poi3.17jar包下载,支持excel2003,excel2007,亲测可用。poi3.17jar包下载,支持excel2003,excel2007,亲测可用。poi3.17jar包下载,支持excel2003,excel2007,亲测可用。poi3.17jar包下载,支持excel2003,excel2007...
Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能。 结构: HSSF - 提供读写Microsoft Excel格式档案的功能。 XSSF - 提供读写Microsoft Excel ...
Java POI 生成Word文档,支持图片插入,关键是修改XML部分,本人已测试通过。
赠送jar包:poi-4.1.2.jar; 赠送原API文档:poi-4.1.2-javadoc.jar; 赠送源代码:poi-4.1.2-sources.jar; 赠送Maven依赖信息文件:poi-4.1.2.pom; 包含翻译后的API文档:poi-4.1.2-javadoc-API文档-中文(简体)-...
Java POI 导入导出Excel简单实例源代码 该源代码的jar包,参见以下博文附录截图 Java POI导出EXCEL经典实现 Java导出Excel弹出下载框 http://blog.csdn.net/evangel_z/article/details/7332535 web页面导出Excel...
java开发中操作excle的jar包,功能齐全,很强大,非常实用。
java poi 实现word文本的替换,支持doc和docx格式的文本替换
事件驱动解析是把文件转换成xml,然后一边读取一边解析,这样就对内存的占用就会很少,可以很好的处理poi出现OOM的问题。 maven添加需要的jar包 <groupId>org.apache.poi <artifactId>poi <version>3.15 ...