package cn.eguid;
import java.io.Closeable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Comment;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
/**
* --2016.3.23
* 解析EXCEL文档1.2
* 支持xlsx和xls文档解析,全面兼容OFFICE所有EXCEL版本文件
* @author eguid
*
** --2016.3.21
* 解析EXCEL文档1.1
* 支持xls文档解析
* @author eguid
*/
public class poi {
/**
* 按照给定的字段进行解析
* 如给定数组:{id,name,sal,date}
* @throws IOException
* @throws InvalidFormatException
*/
public static Map> parseByfield(File file,String[] fields) throws InvalidFormatException, IOException
{
Workbook wb=createWorkbook(file);
Sheet sheet=wb.getSheetAt(0);
//列
Cell cell=null;
//暂时存放
String data=null;
//最大行数
int maxRowNum=sheet.getLastRowNum();
//最大列数
int MaxCellNum=sheet.getRow(0).getLastCellNum();
Listlist=null;
Map>map=null;
map=new HashMap>();
for(int i=0;i();
String title=null;
for(int j=0;j> map=new HashMap>();
String title=null;//标题
int rowindex=0;//行数
int cellindex=0;//列数
String data=null;//用于暂存数据
while(rows.hasNext())
{
List list=new ArrayList();
cellindex=0;
//获取行中数据
Row row = (Row) rows.next();
//获取列迭代器
Iterator cells = row.cellIterator();
while(cells.hasNext())
{
//获取列中数据
Cell cell = (Cell) cells.next();
//获取每个单元格的值
//将标题下的内容放到list中
list.add( getValue4Cell(cell));
}
//将解析完的一列数据压入map
map.put(""+rowindex++, list);
}
return map;
}
/**
* 把默认的格式转换成这种格式
* id [1,2,3,4,5]
*name [wang,liang,eguid,qq,yy]
*
* @param map map格式:Map>
* @return Map>
*/
public static Map> format(Map> map)
{
Map> newmap=new HashMap>();
//获取标题行有多少列
String[] titles=new String[map.get("0").size()];
int index=0;
//获取所有标题
for(String s:map.get("0"))
{
titles[index++]=s;
}
//控制List
for(int i=0;inewlist=new ArrayList();
//控制map
for(int j=1;j>map=parse1(new File("测试.xlsx"));
Map >newmap=format(map);
for(Entry>e:newmap.entrySet())
{
System.out.println(e.getKey());
System.out.println(e.getValue());
}
}
/**
格式:
key value
0 [id, name, sex, sal, date]
1 [1.0, wang, 1.0, 1000.0, 42287.0]
2 [2.0, liang, 1.0, 1001.0, 42288.0]
3 [3.0, eguid, 1.0, 1002.0, 42289.0]
4 [4.0, qq, 0.0, 1003.0, 42290.0]
5 [5.0, yy, 0.0, 1004.0, 42291.0]
* @throws InvalidFormatException
*/
public static void test2() throws IOException, InvalidFormatException
{
Map>map=parse1(new File("测试.xlsx"));
for(Entry>e:map.entrySet())
{
System.out.println(e.getKey());
System.out.println(e.getValue());
}
}
public static void main(String[]args) throws IOException, InvalidFormatException
{
//System.out.println(parseFileSuffix(new File("测试.xlsx")));
// test1();
// test2();
Map> map=parseByfield(new File("测试.xlsx"),new String[]{"id","name"});
System.out.println(map);
}
}