package totencent;
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;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.io.;
import java.util.;
public class ReadFile {
//每个组数的变量只能是静态的,才可以处理
public static int []flagnum = new int[204];
public static void main(String[] args) throws Exception {
//获得一个文件对象,参数是文件的全路径
File file = new File(“C:\Users\Administrator\Desktop\zhang.xls”);
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
//打开HSSFWorkbook
POIFSFileSystem fs = new POIFSFileSystem(in);
//创建HSSFWorkbook对象
HSSFWorkbook wb = new HSSFWorkbook(fs);
//excel中存在多个的sheet,循环读取
Map<HSSFCell,HSSFCell[]> result = new LinkedHashMap<>();
for(int sheetIndex =0;sheetIndex<wb.getNumberOfSheets();sheetIndex++){
HSSFSheet st = wb.getSheetAt(sheetIndex);
for (int rowIndex = 0; rowIndex <st.getLastRowNum() ; rowIndex++) {
HSSFRow row = st.getRow(rowIndex);
HSSFCell[] hssfRows = {row.getCell(1),row.getCell(2),row.getCell(3)};
result.put(row.getCell(0),hssfRows);
//打印Excel中的内容
// System.out.print(row.getCell(0));
// System.out.print(" “);
// System.out.print(row.getCell(1));
// System.out.print(” “);
// System.out.print(row.getCell(2));
// System.out.print(” ");
// System.out.println(row.getCell(3));
}
}
Set keySet = result.keySet();
//获取需要的数据组数量
int length = keySet.size();
System.out.println(length);
//遍历存放所有key的Set集合
Iterator it =keySet.iterator();
//定义一个变量,记录组数
Set<String> flagnumcount = new HashSet<>();
while(it.hasNext()){
//得到每一个key
HSSFCell key = it.next();
//通过key获取对应的value
HSSFCell[] value = result.get(key);
String keyString = key.toString();
//这里使用Set特性的唯一性,可以算出有多少组
flagnumcount.add(keyString);
for (int i = 0; i <flagnum.length ; i++) {
if (keyString.equalsIgnoreCase((i+1)+".0")){
flagnum[i]++;
}
}
System.out.println(keyString);
System.out.println(key+"==="+value[0]+" "+value[1]+" "+value[2]);
}
//减1是为了去除第一行
System.out.println("下面的数据是分组数目");
System.out.println(flagnumcount.size()-1);
ArrayList<ArrayList<String>> listResult = new ArrayList<ArrayList<String>>();
for (int i = 0; i <flagnum.length ; i++) {
//这里把需要用到的数据204个容器制造出来
ArrayList<String> s = new ArrayList<String>();
System.out.println("第"+(i+1)+"组的数据有"+flagnum[i]+"数据");
listResult.add(s);
}
System.out.println(listResult.size());
//根据第一列往每个数组中添加数据,正式填充数据
//因为遍历完了,所以还需要再来一次
Iterator<HSSFCell> it2 =keySet.iterator();
while(it2.hasNext()){
//得到每一个key
HSSFCell key = it2.next();
//通过key获取对应的value
HSSFCell[] value = result.get(key);
String keyString = key.toString();
//遍历204数据
for (int i = 0; i <flagnum.length ; i++) {
//如果是第一列的数据
if (keyString.equalsIgnoreCase((i+1)+".0")){
listResult.get(i).add(value[1].toString());
listResult.get(i).add(value[2].toString());
}
}
}
//这里从204个数组中遍历存进去的数据
System.out.println("这里开始打印数据");
System.out.println(listResult);
System.out.println("整体");
//测试打印第一行数据
System.out.println(listResult.get(0).get(0)+" "+listResult.get(0).get(1)+" "+listResult.get(1).get(0)+" "+listResult.get(1).get(1)+" "+listResult.get(2).get(0)+" "+listResult.get(2).get(1));
//打印第二行数据
//System.out.println(listResult.get(1).get(0)+" "+listResult.get(1).get(1)+" "+listResult.get(2).get(0)+" "+listResult.get(2).get(1)+" "+listResult.get().get(0)+" "+listResult.get(2).get(1));
in.close();
}
}