POI操作excel
来源:互联网 发布:淘宝前端团队 编辑:程序博客网 时间:2024/06/04 20:15
package com.poi;import java.io.File;import java.io.FileInputStream;import java.util.Iterator;import org.apache.poi.hssf.usermodel.HSSFCell;import org.apache.poi.hssf.usermodel.HSSFSheet;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.hssf.util.CellReference;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.Name;import org.apache.poi.ss.usermodel.Row;public class ReadFromXml {public static void main(String[] args) {//ReadFromXml.getData("目标地址");int [] Rowindexs=ReadFromXml.getRow_ColIndex(new String [] {"admin","maco"},"row");int [] ColunmIndexs=ReadFromXml.getRow_ColIndex(new String [] {"admin","maco"}, "colunm");for (int i : Rowindexs) {System.out.println("row:"+i);}for (int i : ColunmIndexs) {System.out.println("colunm:"+i);}}//根据Name Box(名称框)定位到单元格行、列 public static int [] getRow_ColIndex(String [] str,String type) { int [] indexs=new int [str.length]; try{ HSSFWorkbook wb=new HSSFWorkbook(new FileInputStream(new File("目标地址"))); int NameTotalNums=wb.getNumberOfNames(); for(int nameIndex=0;nameIndex<NameTotalNums;nameIndex++) { Name name=wb.getNameAt(nameIndex); for(int i=0;i<str.length;i++) { if(name.getNameName().equals(str[i])) { //根据String cellFullName=name.getNameName();来得到单格的名字如:"A1" String cellFullName=name.getRefersToFormula(); String [] cellStr=cellFullName.split("\\$"); StringBuilder sb=new StringBuilder(); sb.append(cellStr[cellStr.length-2]); sb.append(cellStr[cellStr.length-1]); //构造一个单元格的属性类 CellReference cellReference=new CellReference(sb.toString()); if(type.equals("row")) { int Rowindex=cellReference.getRow(); indexs[i]=Rowindex; }else if(type.equals("colunm")){ int Colunmindex=cellReference.getCol(); indexs[i]=Colunmindex; } } } } }catch(Exception e) { e.printStackTrace(); } return indexs; }public static void getData(String dir){try{FileInputStream fis=new FileInputStream(new File(dir));/*加载一个excel文件*/HSSFWorkbook wb=new HSSFWorkbook(fis);/*得到一个sheet工作单元*/HSSFSheet sheet=wb.getSheetAt(0);/*迭代拿到所有的行*/Iterator<Row> iterators=sheet.iterator(); while(iterators.hasNext()) { Row row=iterators.next(); /*迭代拿到所有的单元格*/ Iterator<Cell> cells=row.iterator(); while(cells.hasNext()) { Cell cell=cells.next(); String strCell = ""; switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_STRING: strCell=cell.getStringCellValue(); break; case HSSFCell.CELL_TYPE_NUMERIC: strCell=String.valueOf(cell.getNumericCellValue()); } System.out.print(strCell+""); } System.out.println(""); }}catch(Exception e){e.printStackTrace();}}}
0 0
- POI操作EXCEL
- POI操作EXCEL
- POI操作Excel文档
- 使用POI操作Excel
- poi操作excel文件
- poi操作excel文件
- poi 操作Excel
- POI操作Excel
- POI 操作excel
- POI 操作EXCEL
- poi操作excel文件
- 用POI操作Excel
- poi操作excel
- poi 操作 excel
- POI操作excel大全
- POI操作Excel总结
- POI操作EXCEL
- POI操作EXCEL(一)
- CUGBACM Codeforces Tranning 2 解题报告
- Sicily 1381. a*b【高精度乘法】
- 给视图添加导航栏
- 【Leetcode】Climbing Stairs JAVA
- Lighting System Design
- POI操作excel
- 第四篇:Hibernate开发步骤注意事项详解1
- 网页抓取:PHP实现网页爬虫方式小结
- VS2013实现孙鑫Ondraw中的一些功能
- 【UML学习】四种关系
- UVA 1590 IP Networks
- Choose and divide
- webview是用软解码或者硬解码可能导致视频播放有声音没有图像
- 日期Date操作