jar 包:
poi-3.13.jar
poi-ooxml-3.13.jar
poi-ooxml-schemas-3.13.jar
xmlbeans-2.6.0.jar
slf4j-api-1.4.3.jar
slf4j-log4j12-1.4.3.jar
log4j-1.2.17.jar
junit-4.10.jar
import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.util.LinkedList; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.ss.usermodel.Cell; 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.xssf.usermodel.XSSFWorkbook; import org.apache.xmlbeans.impl.piccolo.io.FileFormatException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @ClassName: ReadExcelUtils * @Description: 读取excel(兼容03和07格式) * @author * @company * @date 2015年12月18日 * @version V1.0 */ public class ReadExcelUtils { private static final Logger log = LoggerFactory .getLogger(ReadExcelUtils.class); private static final String EXTENSION_XLS = "xls"; private static final String EXTENSION_XLSX = "xlsx"; /** * 读取excel文件内容 * * @param filePath * @throws Exception */ public List
关闭 I/O 流:
public static void closeStream(InputStream is, OutputStream os) { if (null != is) { try { is.close(); is = null; } catch (IOException e) { log.error("close InputStream fail!", e); } } if (null != os) { try { os.close(); os = null; } catch (IOException e) { log.error("close OutputStream fail!", e); } } }
文件内容:
测试(junit4):
import java.util.List; import java.util.Map; import org.junit.Test; public class TestReadExcelUtils { @Test public void readExcel() { try { // f:\\Test.xlsx List> list = new ReadExcelUtils() .readExcel("f:\\红外系统权限统计--系统设置.xls"); if (null != list && !list.isEmpty()) { System.out.println(list); } else { System.out.println("Empty"); } } catch (Exception e) { e.printStackTrace(); } } }
测试结果:
[ {LEAF_=1}, {DISPLAY_NAME=图像归档日期}, {NAME=settting:imgfiledate}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=PMS远程调用地址}, {NAME=settting:pmsrpcurl}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=诊断规则列表}, {NAME=setting:rulelist}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=添加诊断规则}, {NAME=setting:addrule}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=编辑诊断规则}, {NAME=setting:editrule}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=删除诊断规则}, {NAME=setting:delrule}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=图谱特征提取列表}, {NAME=setting:charactlist}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=提取特征}, {NAME=setting:extractcharacter}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=用户管理列表}, {NAME=setting:userlist}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=添加用户}, {NAME=setting:adduser}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=编辑用户}, {NAME=setting:edituser}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=删除用户}, {NAME=setting:deluser}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=角色管理列表}, {NAME=setting:rolelist}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=添加角色}, {NAME=setting:addrole}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=编辑角色}, {NAME=setting:editrole}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=删除角色}, {NAME=setting:delrole}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=权限管理列表}, {NAME=setting:permissionlist}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=添加权限}, {NAME=setting:addperimssion}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=编辑权限}, {NAME=setting:editpermission}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=}, {LEAF_=1}, {DISPLAY_NAME=删除权限}, {NAME=setting:delpermission}, {PARENT_=CED99042927A4B0A9C5326B3C1A98601}, {MENU=Y}, {=} ]