package excel;
//该例子用POI 3.0实现,请自行到APATCH网站下载POI
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
* EXCEL文档解析工具类 该工具能将EXCEL文档中的表解析为由JAVA基础类构成的数据集合 整个EXCEL表由多个行组成.每行用一个LIST表示.
* EXCEL中的行由一个LIST表示,各列的数据索引从0开始一一对齐存放在这个LIST中; 多个行构成整个表,由一个LIST存放多个行.
*
*******************************************
*/
public class JieXiExcel {
private HSSFWorkbook workbook;
public JieXiExcel(File excelFile) throws FileNotFoundException, IOException {
workbook = new HSSFWorkbook(new FileInputStream(excelFile));
}
/**
* 获得表中的数据
*
* @param sheetNumber
* 表格索引(EXCEL 是多表文档,所以需要输入表索引号)
* @return 由LIST构成的行和表
* @throws FileNotFoundException
* @throws IOException
*/
public List<List> getDatasInSheet(int sheetNumber)
throws FileNotFoundException, IOException {
List<List> result = new ArrayList<List>();
// 获得指定的表
HSSFSheet sheet = workbook.getSheetAt(sheetNumber);
// 获得数据总行数
int rowCount = sheet.getLastRowNum();
if (rowCount < 1) {
return result;
}
// 逐行读取数据
for (int rowIndex = 0; rowIndex <= rowCount; rowIndex++) {
// 获得行对象
HSSFRow row = sheet.getRow(rowIndex);
if (row != null) {
List<Object> rowData = new ArrayList<Object>();
// 获得本行中单元格的个数
int columnCount = row.getLastCellNum();
// 获得本行中各单元格中的数据
for (int columnIndex = 0; columnIndex < columnCount; columnIndex++) {
HSSFCell cell = row.getCell(columnIndex);
// 获得指定单元格中数据
Object cellStr = this.getCellString(cell);
rowData.add(cellStr);
}
result.add(rowData);
}
}
return result;
}
/**
* 获得单元格中的内容
*
* @param cell
* @return
*/
protected Object getCellString(HSSFCell cell) {
Object result = null;
if (cell != null) {
int cellType = cell.getCellType();
switch (cellType) {
case HSSFCell.CELL_TYPE_STRING:
result = cell.getRichStringCellValue().getString();
break;
case HSSFCell.CELL_TYPE_NUMERIC:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_FORMULA:
result = cell.getNumericCellValue();
break;
case HSSFCell.CELL_TYPE_ERROR:
result = null;
break;
case HSSFCell.CELL_TYPE_BOOLEAN:
result = cell.getBooleanCellValue();
break;
case HSSFCell.CELL_TYPE_BLANK:
result = null;
break;
}
}
return result;
}
public static void main(String[] args) throws Exception {
Map mapZhuData = new HashMap();
Map ciData = new HashMap();
File file = new File("C:\\Users\\qinghu\\zhu.xls");
JieXiExcel parser = new JieXiExcel(file);
List<List> datas = parser.getDatasInSheet(5);//改第几个表格,从0开始
for (int i = 0; i < datas.size(); i++) {// 显示数据
List row = datas.get(i);
Object value = row.get(2);
String[] data = String.valueOf(value).trim()
.replaceAll("[\\t\\n\\r]", ",").trim().split(",");
mapZhuData.put(String.valueOf(row.get(0)).trim(), data);
}
File file1111 = new File("C:\\Users\\qinghu\\ci.xls");
JieXiExcel parser1111 = new JieXiExcel(file1111);
List<List> datas1111 = parser1111.getDatasInSheet(0);
for (int i = 0; i < datas1111.size(); i++) {// 显示数据
List row = datas1111.get(i);
ciData.put(String.valueOf(row.get(2)).trim(), String.valueOf(row.get(1)).trim());
}
//System.out.println(ciData.get("CW-03-02-02-C02"));
while(true){
Scanner sc=new Scanner(System.in);
String testString = sc.next().trim();
//需要填写的编号"CW-11-01-05"
String[] data1=(String[]) mapZhuData.get(testString);
if(data1 != null){
for(String s:data1){
System.out.println(s+",匹配后的:"+ciData.get(s));
}
}else {
System.out.println("null");
}
}
}
}
java解析excel
最新推荐文章于 2023-07-24 09:40:04 发布