java导入excel

依赖

<dependency>
    <groupId>net.sourceforge.jexcelapi</groupId>
    <artifactId>jxl</artifactId>
    <version>2.6.12</version>
</dependency>

通用controller

package com.olio;
 
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
 
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLDecoder;
 
public class TestDemo {
	public void readExcel(String path,String strURL) {
		try {
			/*// 如果需要通过URL获取资源的加上以下的代码,不需要的省略就行
			URL url = new URL(strURL);
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			// 设置超时间为3秒
			conn.setConnectTimeout(3*1000);
			// 防止屏蔽程序抓取而返回403错误
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
			// 获取输入流
			InputStream inputStream = conn.getInputStream();
			Workbook workbook  = Workbook.getWorkbook(inputStream);
			......*/
			
			// 解析路径的file文件
			Workbook workbook = Workbook.getWorkbook(new File(path));
			// 获取第一张工作表
			Sheet sheet = workbook.getSheet(0);
			// 循环获取每一行数据 因为默认第一行为标题行,我们可以从 1 开始循环,如果需要读取标题行,从 0 开始
			// sheet.getRows() 获取总行数
			for (int i = 1; i < sheet.getRows(); i++) {
				// 获取第一列的第 i 行信息 sheet.getCell(列,行),下标从0开始
				String id = sheet.getCell(0, i).getContents();
				// 获取第二列的第 i 行信息
				String name = sheet.getCell(1,i).getContents();
				// 获取第三列的第 i 行信息
				String sex = sheet.getCell(2, i).getContents();
				// 获取第四列的第 i 行信息
				String grade = sheet.getCell(3, i).getContents();
 
				// 存入本地或者是存入对象等根据给人需求自己定就行,创建对象存储,然后加入集合中
				// ......
			}
		} catch (IOException e) {
			e.printStackTrace();
		} catch (BiffException e) {
			e.printStackTrace();
		}
	}
 
}

具体实操

@PostMapping("/addlist")
    @ResponseBody
    public List<CommonDict> addlist( MultipartFile file) {
        try {
            Workbook workbook = Workbook.getWorkbook(file.getInputStream());
            Sheet sheet = workbook.getSheet(0);
            //List<CommonDict> list = new ArrayList<>();
            for (int i = 0; i < sheet.getRows(); i++) {
                CommonDict commonDict = new CommonDict();
                String code = sheet.getCell(0, i).getContents();
                String name = sheet.getCell(1, i).getContents();
                String sex = sheet.getCell(2, i).getContents();
                commonDict.setDictCode(code);
                commonDict.setDictName(name);
                List<CommonDict> commonDicts = commonDictService.selectCommonDictByCode(sex);
                if (commonDicts!=null&&commonDicts.size()>0){
                    if(commonDicts.get(0)!=null){
                        commonDict.setParentId( commonDicts.get(0).getId());
                        commonDict.setOrderNum(0);
                        //list.add(commonDict);
                        commonDictService.insertCommonDict(commonDict);

                    }
                }


            }
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        return null;
    }

excel只能xls格式!!!!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值