JAVA读取Excel兼容2003、2007

本文提供了一种使用Java读取Excel文件的解决方案,支持Excel 2003和2007两种版本。通过利用Apache POI库,该方法能够有效解析Excel表格并获取其中的数据。
Java代码 复制代码  收藏代码
  1. import java.io.File;   
  2.   
  3. import java.io.FileInputStream;   
  4.   
  5. import java.io.IOException;   
  6.   
  7. import java.io.InputStream;   
  8.   
  9. import java.util.ArrayList;   
  10.   
  11. import java.util.List;   
  12.   
  13.     
  14.   
  15. import org.apache.poi.hssf.usermodel.HSSFWorkbook;   
  16.   
  17. import org.apache.poi.ss.usermodel.Cell;   
  18.   
  19. import org.apache.poi.ss.usermodel.Row;   
  20.   
  21. import org.apache.poi.ss.usermodel.Sheet;   
  22.   
  23. import org.apache.poi.ss.usermodel.Workbook;   
  24.   
  25. import org.apache.poi.xssf.usermodel.XSSFWorkbook;   
  26.   
  27.     
  28.   
  29. /**  
  30.  
  31.  * @描述:测试excel读取  
  32.  
  33.  * 导入的jar包  
  34.  
  35.  * poi-3.8-beta3-20110606.jar  
  36.  
  37.  * poi-ooxml-3.8-beta3-20110606.jar  
  38.  
  39.  * poi-examples-3.8-beta3-20110606.jar  
  40.  
  41.  * poi-excelant-3.8-beta3-20110606.jar  
  42.  
  43.  * poi-ooxml-schemas-3.8-beta3-20110606.jar  
  44.  
  45.  * poi-scratchpad-3.8-beta3-20110606.jar  
  46.  
  47.  * xmlbeans-2.3.0.jar  
  48.  
  49.  * dom4j-1.6.1.jar  
  50.  
  51.  * jar包官网下载地址:http://poi.apache.org/download.html  
  52.  
  53.  * 下载poi-bin-3.8-beta3-20110606.zipp  
  54.  
  55.  * @时间:2011-8-9 下午03:15:15  
  56.  
  57.  */  
  58.   
  59. public class WDWExcelTest {   
  60.   
  61. /** 总行数 */  
  62.   
  63. private int totalRows = 0;   
  64.   
  65. /** 总列数 */  
  66.   
  67. private int totalCells = 0;   
  68.   
  69. /** 错误信息 */  
  70.   
  71. private String errorInfo;   
  72.   
  73.     
  74.   
  75. /** 构造方法 */  
  76.   
  77. public WDWExcelTest() {   
  78.   
  79. }   
  80.   
  81.     
  82.   
  83. /**  
  84.  
  85. * @描述:得到总行数  
  86.  
  87. * @时间:2011-8-9 下午04:27:34  
  88.  
  89. * @参数:@return  
  90.  
  91. * @返回值:int  
  92.  
  93. */  
  94.   
  95. public int getTotalRows() {   
  96.   
  97. return totalRows;   
  98.   
  99. }   
  100.   
  101.     
  102.   
  103. /**  
  104.  
  105. * @描述:得到总列数  
  106.  
  107. * @时间:2011-8-9 下午04:27:45  
  108.  
  109. * @参数:@return  
  110.  
  111. * @返回值:int  
  112.  
  113. */  
  114.   
  115. public int getTotalCells() {   
  116.   
  117. return totalCells;   
  118.   
  119. }   
  120.   
  121.        
  122.   
  123. /**  
  124.  
  125. * @描述:得到错误信息  
  126.  
  127. * @时间:2011-8-9 下午04:28:17  
  128.  
  129. * @参数:@return  
  130.  
  131. * @返回值:String  
  132.  
  133. */  
  134.   
  135. public String getErrorInfo() {   
  136.   
  137. return errorInfo;   
  138.   
  139. }   
  140.   
  141.   
  142.   
  143. /**  
  144.  
  145. * @描述:验证excel文件  
  146.  
  147. * @时间:2011-8-9 下午04:06:47  
  148.  
  149. * @参数:@param fileName  
  150.  
  151. * @参数:@return  
  152.  
  153. * @返回值:boolean  
  154.  
  155. */  
  156.   
  157. public boolean validateExcel(String fileName){   
  158.   
  159. /** 检查文件名是否为空或者是否是Excel格式的文件 */  
  160.   
  161. if (fileName == null || !( WDWUtil.isExcel2003(fileName) || WDWUtil.isExcel2007(fileName) ) ) {   
  162.   
  163. errorInfo = "文件名不是excel格式";   
  164.   
  165. return false;   
  166.   
  167. }   
  168.   
  169. /** 检查文件是否存在 */  
  170.   
  171. File file = new File(fileName);   
  172.   
  173. if (file == null || !file.exists()) {   
  174.   
  175. errorInfo = "文件不存在";   
  176.   
  177. return false;   
  178.   
  179. }   
  180.   
  181. return true;   
  182.   
  183. }   
  184.   
  185. /**  
  186.  
  187. * @描述:根据文件名读取excel文件  
  188.  
  189. * @时间:2011-8-9 下午03:17:45  
  190.  
  191. * @参数:@param fileName  
  192.  
  193. * @参数:@return  
  194.  
  195. * @返回值:List  
  196.  
  197. */  
  198.   
  199. public List<List<String>> read(String fileName) {   
  200.   
  201. List<List<String>> dataLst = new ArrayList<List<String>>();   
  202.   
  203. InputStream is = null;   
  204.   
  205. try {   
  206.   
  207. /** 验证文件是否合法 */  
  208.   
  209. if(!validateExcel(fileName)){   
  210.   
  211. System.out.println(errorInfo);   
  212.   
  213. return null;   
  214.   
  215. }   
  216.   
  217.   
  218. /** 判断文件的类型,是2003还是2007 */  
  219.   
  220. boolean isExcel2003 = true;   
  221.   
  222. if (WDWUtil.isExcel2007(fileName)) {   
  223.   
  224. isExcel2003 = false;   
  225.   
  226. }   
  227.   
  228. /** 调用本类提供的根据流读取的方法 */  
  229.   
  230. File file = new File(fileName);   
  231.   
  232. is = new FileInputStream(file);   
  233.   
  234. dataLst = read(is, isExcel2003);   
  235.   
  236. is.close();   
  237.   
  238. catch (Exception ex) {   
  239.   
  240. ex.printStackTrace();   
  241.   
  242. finally {   
  243.   
  244. if(is != null){   
  245.   
  246. try {   
  247.   
  248. is.close();   
  249.   
  250. catch (IOException e) {   
  251.   
  252. is = null;   
  253.   
  254. e.printStackTrace();   
  255.   
  256. }   
  257.   
  258. }   
  259.   
  260. }   
  261.   
  262. /** 返回最后读取的结果 */  
  263.   
  264. return dataLst;   
  265.   
  266. }   
  267.   
  268.     
  269.   
  270. /**  
  271.  
  272. * @描述:根据流读取Excel文件  
  273.  
  274. * @时间:2011-8-9 下午04:12:41  
  275.  
  276. * @参数:@param inputStream  
  277.  
  278. * @参数:@param isExcel2003  
  279.  
  280. * @参数:@return  
  281.  
  282. * @返回值:List  
  283.  
  284. */  
  285.   
  286. public List<List<String>> read(InputStream inputStream, boolean isExcel2003) {   
  287.   
  288. List<List<String>> dataLst = null;   
  289.   
  290. try {   
  291.   
  292. /** 根据版本选择创建Workbook的方式 */  
  293.   
  294. Workbook wb = isExcel2003 ? new HSSFWorkbook(inputStream) : new XSSFWorkbook(inputStream);   
  295.   
  296. dataLst = read(wb);   
  297.   
  298. catch (IOException e) {   
  299.   
  300. e.printStackTrace();   
  301.   
  302. }   
  303.   
  304. return dataLst;   
  305.   
  306. }   
  307.   
  308.     
  309.   
  310.   
  311. /**  
  312.  
  313. * @描述:读取数据  
  314.  
  315. * @时间:2011-8-9 下午04:37:25  
  316.  
  317. * @参数:@param wb  
  318.  
  319. * @参数:@return  
  320.  
  321. * @返回值:List<List<String>>  
  322.  
  323. */  
  324.   
  325. private List<List<String>> read(Workbook wb) {   
  326.   
  327. List<List<String>> dataLst = new ArrayList<List<String>>();   
  328.   
  329. /** 得到第一个shell */  
  330.   
  331. Sheet sheet = wb.getSheetAt(0);   
  332.   
  333. /** 得到Excel的行数 */  
  334.   
  335. this.totalRows = sheet.getPhysicalNumberOfRows();   
  336.   
  337. /** 得到Excel的列数 */  
  338.   
  339. if (this.totalRows >= 1 && sheet.getRow(0) != null) {   
  340.   
  341. this.totalCells = sheet.getRow(0).getPhysicalNumberOfCells();   
  342.   
  343. }   
  344.   
  345.   
  346. /** 循环Excel的行 */  
  347.   
  348. for (int r = 0; r < this.totalRows; r++) {   
  349.   
  350. Row row = sheet.getRow(r);   
  351.   
  352. if (row == null) {   
  353.   
  354. continue;   
  355.   
  356. }   
  357.   
  358. List<String> rowLst = new ArrayList<String>();   
  359.   
  360. /** 循环Excel的列 */  
  361.   
  362. for (short c = 0; c < this.getTotalCells(); c++) {   
  363.   
  364. Cell cell = row.getCell(c);   
  365.   
  366. String cellValue = "";   
  367.   
  368. if (cell == null) {   
  369.   
  370. rowLst.add(cellValue);   
  371.   
  372. continue;   
  373.   
  374. }   
  375.   
  376. /** 处理Excel的字符串 */  
  377.   
  378. cellValue = cell.getStringCellValue();   
  379.   
  380. rowLst.add(cellValue);   
  381.   
  382. }   
  383.   
  384. /** 保存第r行的第c列 */  
  385.   
  386. dataLst.add(rowLst);   
  387.   
  388. }   
  389.   
  390. return dataLst;   
  391.   
  392. }   
  393.   
  394.     
  395.   
  396.     
  397.   
  398. /**  
  399.  
  400. * @描述:main测试方法  
  401.  
  402. * @时间:2011-8-9 下午04:31:35  
  403.  
  404. * @参数:@param args  
  405.  
  406. * @参数:@throws Exception  
  407.  
  408. * @返回值:void  
  409.  
  410. */  
  411.   
  412. public static void main(String[] args) throws Exception {   
  413.   
  414. WDWExcelTest poi = new WDWExcelTest();   
  415.   
  416. //List<List<String>> list = poi.read("d:/aaa.xls");   
  417.   
  418. List<List<String>> list = poi.read("d:/aab.xlsx");   
  419.   
  420. if(list != null){   
  421.   
  422. for(int i = 0, ilen = list.size(); i < ilen; i++){   
  423.   
  424. System.out.println("第" + (i + 1) + "行");   
  425.   
  426. List<String> cellList = list.get(i);   
  427.   
  428. for(int j = 0, jlen = cellList.size(); j < jlen; j++){   
  429.   
  430. System.out.print("    第" + (j + 1) + "列值:");   
  431.   
  432. System.out.println(cellList.get(j));   
  433.   
  434. }   
  435.   
  436. }   
  437.   
  438. }   
  439.   
  440. }   
  441.   
  442. }   
  443.   
  444.     
  445.   
  446.     
  447.   
  448. /**  
  449.  
  450.  * @描述:工具类  
  451.  
  452.  * @时间:2011-8-9 下午04:28:43  
  453.  
  454.  */  
  455.   
  456. class WDWUtil{   
  457.   
  458.   
  459. /**  
  460.  
  461. * @描述:是否是2003的excel,返回true是2003  
  462.  
  463. * @时间:2011-8-9 下午03:20:49  
  464.  
  465. * @参数:@param fileName  
  466.  
  467. * @参数:@return  
  468.  
  469. * @返回值:boolean  
  470.  
  471. */  
  472.   
  473. public static boolean isExcel2003(String fileName){   
  474.   
  475. return fileName.matches("^.+\\.(?i)(xls)$");   
  476.   
  477. }   
  478.   
  479. /**  
  480.  
  481. * @描述:是否是2007的excel,返回true是2007  
  482.  
  483. * @时间:2011-8-9 下午03:21:37  
  484.  
  485. * @参数:@param fileName  
  486.  
  487. * @参数:@return  
  488.  
  489. * @返回值:boolean  
  490.  
  491. */  
  492.   
  493. public static boolean isExcel2007(String fileName){   
  494.   
  495. return fileName.matches("^.+\\.(?i)(xlsx)$");   
  496.   
  497. }   
  498.   
  499. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值