【JAVA】poi操作,excel读取数据。
生活随笔
收集整理的這篇文章主要介紹了
【JAVA】poi操作,excel读取数据。
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這幾天改個 問題,讀取excel97的沒問題,讀取07的出現(xiàn)報錯。后來網(wǎng)上查了下原因是07是用什么xml技術(shù)。。。只能從新找方法。
以前用
HSSFWorkbook(new FileInputStream(target));//轉(zhuǎn)03版本
現(xiàn)在又加了個
new XSSFWorkbook(target);//轉(zhuǎn)07版本
POI3.7-版本jar包及源碼地址:
http://download.csdn.net/detail/hateson/3946267
public String getExportExcleList(String target, List<Object> list,String[] condition) {StringBuilder strBuilder = new StringBuilder("");Workbook workbook;if (StringUtils.isEmpty(strBuilder.toString())) {} else {strBuilder.delete(0, strBuilder.length() - 1);}try {try {workbook = (Workbook) new XSSFWorkbook(target);//轉(zhuǎn)07版本} catch (Exception ex) {workbook = (Workbook) new HSSFWorkbook(new FileInputStream(//轉(zhuǎn)03版本target));}Sheet sheet = workbook.getSheetAt(0);Cell excleTitle = sheet.getRow(1).getCell((short) 0);int rows = sheet.getPhysicalNumberOfRows();if (rows < 10) {strBuilder.append("導入表的記錄行數(shù)少于為10行");}for (int r = 3; r < rows; r++) {Row row = sheet.getRow(r);if (row == null)break;SplBean bean = new SplBean();bean.setNd(condition[0]);bean.setYf(condition[1]);Cell jhhCell = row.getCell((short) COLMNSINDEX[0]);if (jhhCell == null) {break;}bean.setDw(getString(jhhCell));jhhCell = row.getCell((short) COLMNSINDEX[1]);if (!"".equals(getString(jhhCell))) {bean.setJsdw(getString(jhhCell));}jhhCell = row.getCell((short) COLMNSINDEX[2]);if (!"".equals(getString(jhhCell))) {bean.setBywc(Double.valueOf(getString(jhhCell, 4)));}jhhCell = row.getCell((short) COLMNSINDEX[3]);if (!"".equals(getString(jhhCell))) {bean.setByzlj_jh(Double.valueOf(getString(jhhCell, 4)));}jhhCell = row.getCell((short) COLMNSINDEX[4]);if (!"".equals(getString(jhhCell))) {bean.setByzlj_sj(Double.valueOf(getString(jhhCell, 4)));}jhhCell = row.getCell((short) COLMNSINDEX[5]);if (!"".equals(getString(jhhCell))) {bean.setByzlj_bfb(Double.valueOf(getString(jhhCell, 4)));}list.add(bean);}strBuilder.append("year=" + condition[0]);} catch (FileNotFoundException e) {e.printStackTrace();strBuilder.append("文件無法找到錯誤");} catch (IOException e) {e.printStackTrace();strBuilder.append("文件無法讀寫錯誤");} catch (Exception e) {e.printStackTrace();strBuilder.append("系統(tǒng)出現(xiàn)異常");}return strBuilder.toString();}
注意jar包沖突,如果出現(xiàn)org.apache.poi.POIXMLException: java.lang.reflect.InvocationTargetException。檢查是否有xbean.xml,刪除一般都可以解決。
其他幫助學習地址:
http://tayuexiaotian.iteye.com/blog/1072248總結(jié)
以上是生活随笔為你收集整理的【JAVA】poi操作,excel读取数据。的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: oracle常用查询语句
- 下一篇: C语言实现通讯录初版