码迷,mamicode.com
首页 > 其他好文 > 详细

POI 解析xls

时间:2016-05-09 12:29:49      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

1、所需jar包

poi-3.6.jar

poi-ooxml-3.6.jar

 

2、M.java

package junit;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

public class M {
    public static void main(String[] args) throws IOException {
        readXls("C:\\Users\\taop\\Desktop\\dhm.xls");
    }

    public static void readXls(String path) throws IOException {
        List<Map<String, Object>> lMap = new ArrayList<Map<String, Object>>();
        InputStream is = new FileInputStream(path);
        
        HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
        // Read the Sheet
        for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
            HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
            if (hssfSheet == null) {
                continue;
            }
            // Read the Row
            for (int rowNum = 1; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                if (hssfRow != null) {
                    Map<String, Object> map = new HashMap<String, Object>();
                    int no = (int)hssfRow.getCell(0).getNumericCellValue();
                    String name = hssfRow.getCell(1).getStringCellValue();
                    map.put("no", no);
                    map.put("name", name);
                    lMap.add(map);
                }
            }
        }
    }

}

 

POI 解析xls

标签:

原文地址:http://www.cnblogs.com/tplovejava/p/5473248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!