读取excel文件内容代码

    最近工作需要批量添加映射excel文件字段的代码  于是通过读取excel2007实现了批量生成代码,记录下代码

   需要引入poi的jar包

import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class AutoMakeMap {

    public static void readFromXLSX2007(String filePath) {  
        File excelFile = null;// Excel文件对象  
        InputStream is = null;// 输入流对象  
        String cellStr = null;// 单元格,最终按字符串处理    
        try {  
            excelFile = new File(filePath);  
            is = new FileInputStream(excelFile);// 获取文件输入流  
            XSSFWorkbook workbook2007 = new XSSFWorkbook(is);// 创建Excel2007文件对象  
            XSSFSheet sheet = workbook2007.getSheetAt(0);// 取出第一个工作表,索引是0  
            // 开始循环遍历行,表头不处理,从1开始  
            for (int i = 1; i <= sheet.getLastRowNum(); i++) {   
          //  for (int i = 0; i <= 490; i++) { 
                XSSFRow row = sheet.getRow(i);// 获取行对象  
                if (row == null) {// 如果为空,不处理  
                    continue;  
                }  
                // 循环遍历单元格  
                for (int j = 0; j < row.getLastCellNum(); j++) { 
               // for (int j = 0; j < 2; j++) {
                    XSSFCell cell = row.getCell(j);// 获取单元格对象  
                    if (cell == null) {// 单元格为空设置cellStr为空串  
                        cellStr = "";  
                    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_BOOLEAN) {// 对布尔值的处理  
                        cellStr = String.valueOf(cell.getBooleanCellValue());  
                    } else if (cell.getCellType() == HSSFCell.CELL_TYPE_NUMERIC) {// 对数字值的处理  
                        cellStr = cell.getNumericCellValue() + "";  
                    } else {// 其余按照字符串处理  
                        cellStr = cell.getStringCellValue();  
                    }  
                    // 下面按照数据出现位置封装到bean中  
                    if (j == 0) {  
                       System.out.print("commandMap["" + cellStr + ""] = "");  
                    } else if (j == 1) {  
                        System.out.println(cellStr + """);  
                    } 
                }  
            }  
        } catch (IOException e) {  
            e.printStackTrace();  
        } finally {// 关闭文件流  
            if (is != null) {  
                try {  
                    is.close();  
                } catch (IOException e) {  
                    e.printStackTrace();  
                }  
            }  
        }    
    }  
    public static void main(String[] args){
        AutoMakeMap amm = new AutoMakeMap();
        amm.readFromXLSX2007("D://aaa.xlsx");
        
    }
}

运行结果:

commandMap["command_01"] = "del-确认信息-在校学生-博士-4000以下-父母-建行-北京-申请成功"
commandMap["command_02"] = "del-确认信息-在校学生-硕士-4000以下-父母-建行-北京-申请成功"
commandMap["command_03"] = "del-确认信息-在校学生-本科-4000以下-父母-建行-北京-申请成功"
commandMap["command_04"] = "del-确认信息-在校学生-专科-4000以下-父母-建行-北京-申请成功"
commandMap["command_05"] = "del-确认信息-在校学生-高中以下-4000以下-父母-建行-北京-申请成功"
commandMap["command_06"] = "del-确认信息-在校学生-博士-4000-6000-父母-建行-北京-申请成功"
commandMap["command_07"] = "del-确认信息-在校学生-博士-6000~8000-父母-建行-北京-申请成功"

 
原文地址:https://www.cnblogs.com/wangcp-2014/p/5557335.html