JAVA-poi-Excel数据读取

JAVA-需求描述:从表格中读取信息导入到数据库

这里写图片描述

代码:

package com.test;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.tj.api.hospital.TjHospitalApi;
import com.tj.api.hospital.TjHospitalDepartmentApi;
import com.tj.api.hospital.TjHospitalDepartmentRelationApi;
import com.tj.api.hospital.TjHospitalProfessorApi;
import com.tj.mapper.apdcn.model.hospital.TjHospitalProfessorModel;

@RunWith(SpringJUnit4ClassRunner.class) // 使用junit4进行测试
@ContextConfiguration(“classpath*:spring/applicationContext.xml”)
public class test {

@Resource
private TjHospitalProfessorApi tjHospitalProfessorApi;

@Resource
private TjHospitalApi tjHospitalApi;

@Resource
private TjHospitalDepartmentApi tjHospitalDepartmentApi;

@Resource
private TjHospitalDepartmentRelationApi tjHospitalDepartmentRelationApi;

@org.junit.Test
public void readProfessiorExcel() {
    //excel文件路径
    String filepath = "C:\\Users\\Lenovo\\Desktop\\TOB产品SKU汇总12-5.xls";
    try {
        // 1.得到Excel工作簿对象
        @SuppressWarnings("resource")
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(filepath));
        // 2.得到Excel工作表对象
        HSSFSheet sheet = wb.getSheetAt(0);
        // 总行数
        int trLength = sheet.getLastRowNum();
        // 3.得到Excel工作表的行
        //HSSFRow row = sheet.getRow(0);
        // 总列数
        //int tdLength = row.getLastCellNum();
        // 4.得到Excel工作表指定行的单元格
        // HSSFCell cell = row.getCell(0);
        // 5.得到单元格样式
        // CellStyle cellStyle = cell.getCellStyle();
        List<TjHospitalProfessorModel> professorModelList = new ArrayList<>();
        for (int i = 0; i < trLength; i++) {
            // 得到Excel工作表的行
            HSSFRow row1 = sheet.getRow(i + 1);
            TjHospitalProfessorModel professorModel = new TjHospitalProfessorModel();
            professorModel.setHospitalName(row1.getCell(0).getStringCellValue());
            professorModel.setDeptName(row1.getCell(1).getStringCellValue());
            professorModel.setName(row1.getCell(2).getStringCellValue());
            //处理读取Excel空单元格时,报空指针错误 
            if (row1.getCell(3) == null ) {
                professorModel.setField("");
            } else {
                professorModel.setField(row1.getCell(3).getStringCellValue());
            }
            String serverName = row1.getCell(4).getStringCellValue();
            String serverCont = row1.getCell(5).getStringCellValue();
            //poi 读取excel 把 数字转换成 字符
            if (row1.getCell(7) != null) {
                row1.getCell(7).setCellType(Cell.CELL_TYPE_STRING);
            }
            String serverPrice = row1.getCell(7).getStringCellValue();
            String serverDetail = "";
            if (row1.getCell(8) != null) {
                serverDetail = row1.getCell(8).getStringCellValue();
            }
            String detail = "服务名称:" + serverName + "<br>"
                            + "服务内容:" + serverCont + "<br>"
                            + "服务价格(元):" + serverPrice + "<br>"
                            + "服务说明:" + serverDetail + "<br>";
            professorModel.setDetail(detail);
            professorModelList.add(professorModel);
        }
        for (TjHospitalProfessorModel tjModel : professorModelList) {
            //业务
            createProfessor(tjModel);
        }
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void createProfessor(TjHospitalProfessorModel professorModel) {

}

}

ps.关于遍历表格,推荐使用for (int i = 0; i < trLength; i++){} 的形式。在读取excel空行或者空单元格方便处理 。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值