读取excel到数据库
来源:互联网 发布:moodle网络课程平台 编辑:程序博客网 时间:2024/06/05 14:51
lz使用poi的jar,maven依赖见lz的 poi读取数据写入excel:
http://blog.csdn.net/imthemostshuaiin626/article/details/75103353
主要解决了长数字在excel中为科学记数法转化为原数据的问题(具体为手机号码)
@SuppressWarnings("resource") public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring_bean.xml"); JdbcTemplate jdbcTemplate = (JdbcTemplate) applicationContext.getBean("jdbcTemplate"); String path="X:\\Users\\xb\\Desktop\\excel\\xx号段.xlsx"; int i = 1;//从第几行开始 excel2sqlDate(path, jdbcTemplate,i); }
@SuppressWarnings("deprecation") private static void excel2sqlDate(String path,JdbcTemplate jdbcTemplate,int r){ DecimalFormat df = new DecimalFormat("0"); List<User> users = new ArrayList<>(); try { // 构造 XSSFWorkbook 对象,strPath 传入文件路径 XSSFWorkbook xwb = new XSSFWorkbook(path); // 读取第一章表格内容 XSSFSheet sheet = xwb.getSheetAt(0); // 定义 row、cell XSSFRow row; //XSSFCell cell; // 循环输出表格中的内容 for (int i = sheet.getFirstRowNum()+r; i < sheet.getPhysicalNumberOfRows(); i++) { row = sheet.getRow(i); for (int j = row.getFirstCellNum(); j < row.getPhysicalNumberOfCells(); j++) { // 通过 row.getCell(j).toString() 获取单元格内容, if (j==0) { if (row.getCell(0)!=null) { row.getCell(0).setCellType(Cell.CELL_TYPE_STRING); } if (row.getCell(2) != null) { row.getCell(2).setCellType(Cell.CELL_TYPE_NUMERIC);//科学记数法的转化 } if (row.getCell(3) != null) { row.getCell(3).setCellType(Cell.CELL_TYPE_STRING); } User user = new User(); user.setId(row.getCell(0).toString()); user.setName(row.getCell(1).toString()); user.setPhoneNum(df.format(row.getCell(2).getNumericCellValue())); user.setCount(Integer.parseInt(row.getCell(3).toString())); users.add(user); System.out.println(row.getCell(0)+"-"+row.getCell(1)+"-"+df.format(row.getCell(2).getNumericCellValue())+"-"+row.getCell(3)); } } } } catch (IOException e) { e.printStackTrace(); } String sql="insert into xxx(name,phone,count) values(?,?,?) "; for(User user :users){ jdbcTemplate.update(sql,user.getName(),user.getPhoneNum(),user.getCount()); } }
阅读全文
0 0
- 读取Excel到数据库
- 数据库读取到excel
- 读取excel到数据库
- 读取excel到数据库表
- 读取Excel文件到数据库
- 读取excel内容到数据库
- POI读取Excel到数据库
- java读取数据库并导出到Excel
- 读取Excel数据到SQL数据库
- java读取数据库保持到Excel
- 读取Excel中的文件加入到数据库
- 从数据库中读取数据到Excel
- 读取Excel数据,保存到数据库
- Matlab读取Access数据库保存到Excel
- 上传EXCEL文件并读取到数据库
- EXCEL:读取ACCESS数据库中指定表数据到EXCEL
- 读取excel文件到数据库 和 下载 excel文件
- java从Excel文件读取数据到数据库和从读取数据库数据写入Excel
- 64-ia-32架构优化手册(4)
- Esxi中CentOS7 扩展磁盘容量
- session和cookie
- 2017.07.13 一点训练部分。
- TCP服务器通信
- 读取excel到数据库
- gpio子系统和pinctrl子系统(三)
- POJ 1328 Radar Installation(贪心)
- popUpWindow响应返回键并关闭
- 首页文章分类 关于 Search private(私有)方法单元测试无法覆盖?那就用反射调用来测试private(私有)方法
- easyui combobox默认设置第一个行数据
- jQuery+bootstrap实现美化警告/确认/提示对话框插件
- RMQ算法
- Centos7开机启动自己的脚本