JAVA报表开发-POI处理EXCEL-导入
1、实现用户数据的导入
把以下数据导入到数据库里面
2、实现用户数据的导入
public void uploadExcel(MultipartFile file) throws Exception { if(file.isEmpty()){ return; } InputStream inputStream = file.getInputStream(); //用户名 手机号 省份 城市 工资 入职日期 出生日期 现住地址 //有内容的workbook工作薄 org.apache.poi.ss.usermodel.Workbook workbook = new XSSFWorkbook(inputStream); //获取到第一个工作表(sheet) Sheet sheet = workbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum();//当前sheet的最后一行的索引值 Row row=null; SimpleDateFormat simpleDateFormat =new SimpleDateFormat("yyyy-MM-dd"); // List<User> userList = new ArrayList<>(); String phone=""; //读取工作表里的内容 for (int i=1;i<=lastRowNum;i++){ User user = new User(); row = sheet.getRow(i); String userName = row.getCell(0).getStringCellValue();//用户名 try { phone = row.getCell(1).getStringCellValue(); } catch (IllegalStateException e) { phone = row.getCell(1).getNumericCellValue()+""; } String province = row.getCell(2).getStringCellValue();//省份 String city = row.getCell(3).getStringCellValue();//城市 Integer salary = ((Double) row.getCell(4).getNumericCellValue()).intValue();//工资 Date hireDate = simpleDateFormat.parse( row.getCell(5).getStringCellValue());//入职日期 Date birthDay = simpleDateFormat.parse(row.getCell(6).getStringCellValue()) ;//出生日期 String address = row.getCell(7).getStringCellValue();//现住地址 user.setUserName(userName); user.setPhone(phone); user.setProvince(province); user.setCity(city); user.setSalary(salary); user.setHireDate(hireDate); user.setBirthday(birthDay); user.setAddress(address); // userList.add(user); userMapper.insert(user); System.out.println(user.toString()); } // System.out.println(userList.toString()); }
如果导入中文乱码 需设置数据库连接串后加
?serverTimezone=GMT%2B8&characterEncoding=utf-8
本文作者:KwFruit
本文链接:https://www.cnblogs.com/mangoubiubiu/p/16320920.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
分类:
Java报表一站式解决方案
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步