java批量导入excel_java实现的Excel批量导入

@Transactional(readOnly = false)

public void importPsd(MultipartFile file) throws Exception {

List list = new ArrayList();

Map rsultMap = new HashMap();

AutoCode auto =new AutoCode();

Workbook workbook = null;

String fileName = file.getOriginalFilename();

if(fileName.endsWith("xls")) {

//2003

try {

workbook = new HSSFWorkbook(file.getInputStream());

} catch (Exception e) {

e.printStackTrace( );

}

}else if(fileName.endsWith("xlsx")) {

try {

//2007

workbook = new XSSFWorkbook(file.getInputStream());

} catch (Exception e) {

e.printStackTrace( );

}

}else {

try {

throw new Exception("文件不是Excel文件");

} catch (Exception e) {

e.printStackTrace();

}

}

Sheet sheet = workbook.getSheet("export");

int rows = sheet.getLastRowNum();//指定行数。一共多少+

if(rows==0) {

try {

throw new Exception("请填写行数");

} catch (Exception e) {

e.printStackTrace();

}

}

for (int i = 2; i < rows+1; i++) {

//读取左上端单元格

Row row = sheet.getRow(i);

//行不为空

if(row != null) {

//创建实体类

SyPsyhPsd syPsyhPsd = new SyPsyhPsd();

//读取第一个内容

String rq = getCellValue(row.getCell(0));

syPsyhPsd.setRq(rq);

//读取第二个内容

/*String khpk = getCellValue(row.getCell(1));

syPsyhPsd.setKhpk(khpk);;*/

//读取第三个内容

String zzsj = getCellValue(row.getCell(1));

syPsyhPsd.setZzsj(zzsj);

//读取第四个内容

String zwsj = getCellValue(row.getCell(2));

syPsyhPsd.setZwsj(zwsj);

//读取第五个内容

String hwsx = getCellValue(row.getCell(3));

syPsyhPsd.setHwsx(hwsx);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值