EXCEL单元格的获取——多例模式
2023-09-11 14:20:43 时间
因为Excel的单元格的行列与单元格是一一相应的,行与列组成的是一对联合主键。给定一个单元格行列或者给定一个单元格名称。须要找到相应的单元格;这样就形成了一种映射关系。须要使用单例模式的变式——多例模式,进行实现。
多例模式的核心是用一个HashMap<K,V>来实现这样的映射关系。V明显是目标单元格。K必须保存单元格的行与列一一相应信息,能够用单元格名称来表示;实现代码例如以下:
import java.util.ArrayList; import java.util.HashMap; /** * @author lcx * */ public class CellMap { private static HashMap<String,CellMap> map=new HashMap(); private CellMap(String cellName) { System.out.println("新建一个:"+cellName); map.put(cellName, this); } public static CellMap getInstance(int row,int col) { return getInstance( cellName(row,col)); } public static CellMap getInstance(String cellName) { if(map.get(cellName)!=null) return map.get(cellName); else return new CellMap(cellName); } private static String cellName(int row,int col) { ArrayList<Character> list=new ArrayList(); while(col>0) { list.add((char) (col%26+'A'-1)); col/=26; } StringBuffer buffer=new StringBuffer(); for(int i=list.size()-1;i>=0;i--) buffer.append(list.get(i)); buffer.append(""+row); return buffer.toString(); } public static void main(String[] args) { //首次获取 for(int i=1;i<10;i++) for(int j=1;j<10;j++) { CellMap.getInstance(i, j); } //再次获取 for(int i=1;i<10;i++) for(int j=1;j<10;j++) { CellMap.getInstance(i, j); } } }
相关文章
- 远程办公使用单位内网Excel【内网穿透】
- 码栈开发手册(四)---编码方式开发(EXCEL处理相关函数)
- Excel VLOOKUP实用教程之 03 使用下拉列表作为查找值vlookup?(教程含数据excel)
- Excel VLOOKUP实用教程之 02 vlookup如何双向查找,两个字段查询数据?(教程含数据excel)
- Excel 函数教程之VLOOKUP实用教程与10个适用于初学者和高级用户的VLOOKUP案例(教程含数据excel)
- Excel 数据透视表教程大全之 04 按日期分组(教程含样本数据)
- 基于C#语言利用Microsoft.office.introp.excel操作Excel总结
- Excel自动化之如何直接提前网页html表格数据并分析
- Excel VLOOKUP实用教程之 02 vlookup如何双向查找,两个字段查询数据?(教程含数据excel)
- Excel VLOOKUP实用教程之 05 vlookup如何从列表中获取最后一个值?(教程含数据excel)
- Excel教程之学生成功所需的 5 个电子表格
- excel-合并多个Excel文件--VBA合并当前目录下所有Excel工作簿中的所有工作表
- NPOI利用多任务模式分批写入多个Excel
- sql server导出到excel错误:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
- Excel VLOOKUP 函数的使用