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); } } } } }
时间: 2024-10-07 20:10:42