java 读取Excel文件

来源:互联网 发布:开通淘宝店铺要钱吗 编辑:程序博客网 时间:2024/06/01 23:31
基于POI   将POI所需的jar包引入工程
import java.io.FileInputStream;import java.util.ArrayList;import java.util.List;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.xssf.usermodel.XSSFWorkbook;import model.HospIntroduceModel;public class Main {public static void main(String[] args) throws Exception {List<HospIntroduceModel> retList = new ArrayList<HospIntroduceModel>();FileInputStream fileIn = new FileInputStream("D://1.xlsx");// 根据指定的文件输入流导入Excel从而产生Workbook对象Workbook wb0 = new XSSFWorkbook(fileIn);// 获取Excel文档中的第一个表单org.apache.poi.ss.usermodel.Sheet sht0 = wb0.getSheetAt(0);// 对Sheet中的每一行进行迭代for (Row r : sht0) {System.out.println(r.getRowNum());// 如果当前行的行号(从0开始)未达到2(第三行)则从新循环// if (r.getRowNum() < 1) {// continue;// }/// System.out.println(r.getCell(0).getNumericCellValue());HospIntroduceModel hospIntroduceModel = new HospIntroduceModel();hospIntroduceModel.setYyid(r.getCell(0).getStringCellValue());hospIntroduceModel.setYyintro(r.getCell(1).getStringCellValue());hospIntroduceModel.setYymc(r.getCell(2).getStringCellValue());retList.add(hospIntroduceModel);}fileIn.close();for (HospIntroduceModel row : retList) {System.out.println(row.getYyid() + row.getYymc() + row.getYyintro());}}}