java如何实现对Excel内容读写,如何将Excel内容导入导出数据库,让我们一起来看看 您所在的位置:网站首页 如何将数据导入excel中数据库 java如何实现对Excel内容读写,如何将Excel内容导入导出数据库,让我们一起来看看

java如何实现对Excel内容读写,如何将Excel内容导入导出数据库,让我们一起来看看

2024-01-24 04:50| 来源: 网络整理| 查看: 265

文章目录 前言一、什么是POI?二、引入依赖三、用POI读取Excel文件内容四、用POI向Excel文件写入五、从Excel读取内容写入数据库六、从数据库读取内容写入Excel表中

前言

最近遇到这样一个需求:需要读取Excel内容(里面含有许多文章),查询指定关键字在各个文章中出现的次数,将结果返回到excel中。因为我不知道怎么用java操作excel,于是上网四处寻找解决办法,结果要么是只能对excel2003或excel2007版的才有用,要么是copy过来的代码压根运行不了!不过最后总算找到了方法~

所以我在这里出一篇文章,为大家避避雷,不用特意去花积分下载资源,到头来反而运行不了,而且该文方法适合07版本以后的excel。

长话短说,从这篇文章你可以学习到以下内容: 1、读取Excel文件内容 2、向Excel写入内容(包括设置样式) 3、从Excel读取内容写入数据库中 4、从数据库读取内容写入Excel中

一、什么是POI?

Apache POI是用Java编写的免费开源的跨平台的 Java API,Apache POI提供API给Java程式对Microsoft Office格式档案读和写的功能。POI为“Poor Obfuscation Implementation”的首字母缩写,意为“简洁版的模糊实现”。

开门见山,java处理Excel就需要用到POI这个java API! 点击快速查看POI使用手册:POI中文API使用文档

二、引入依赖

这里包含了本文中全部实例需要用到的依赖,如果只是进行对Excel的读写,只需引入poi依赖即可

readExcel org.example 1.0-SNAPSHOT 4.0.0 POI_EXCEL jar org.apache.poi poi 3.16 org.apache.poi poi-ooxml 3.14 mysql mysql-connector-java 8.0.16 compile com.alibaba druid 1.0.9 org.springframework spring-jdbc 5.0.2.RELEASE 8 8

引入依赖的时候爆红是正常的,刷新一下maven工程,然后等待下载好就行了

三、用POI读取Excel文件内容

需要用到的类及方法:

类方法XSSFWorkbook:获取工作簿需要的类(工作簿即excel文档)getSheetAt(index):获取工作簿中的工作表,参数即选择第几个工作表XSSFSheet:获取工作表需要的类(excel左下角)getLastRowNum:获得工作表中有效行数XSSFRow:获取行需要的类getRow(int rownum):获取工作表中第rownum行XSSFCell:获取列需要的类getLastCellNum():获取每行中有效列数getCell(int cellnum):获取当前行的第cellnum列setCellType(int cellType):设置读取内容格式(一般为String)getStringCellValue():读取当前行当前列的值

代码如下

public class ReadExcel { public static void main(String[] args) throws Exception { //1、获取工作簿 XSSFWorkbook workbook = new XSSFWorkbook("E:\\hello.xlsx"); //2、获取工作表 XSSFSheet sheet = workbook.getSheetAt(0); //3、获取行 int lastRowNum = sheet.getLastRowNum(); //得到有效行 for (int i = 0; i short cellNum = row.getLastCellNum(); //获取有效列 for (int j = 0; j cell.setCellType(Cell.CELL_TYPE_STRING); //设置格式为string String stringCellValue = cell.getStringCellValue(); System.out.println(stringCellValue); } } } //释放资源 workbook.close(); } } }

Excel表格内容内容如下 在这里插入图片描述 读取excel内容 运行结果: 在这里插入图片描述

四、用POI向Excel文件写入

需要用到的方法:

方法createSheet(String sheetname):创建名为sheetnane的工作表createRow(int rownum):创建索引为rownum行createCell(int columnIndex):创建索引为columnIndex列setCellValue(@Nullable String str):设置当前单元格值,允许为空createCellStyle():创建单元格样式setFillForegroundColor(short fg):设置单元格背景色setFillPattern(short fp):自定义颜色填充规格createFont():创建字体样式setFontName(String name):设置字体样式名称setColor(short color):设置字体颜色setFont(Font font):将字体样式放进单元格样式中setCellStyle(CellStyle style):将自定义单元格样式设置到当前单元格中

代码如下

public class WriteExcel { public static void main(String[] args) throws Exception{ //1、创建工作簿 XSSFWorkbook workbook = new XSSFWorkbook(); //2、创建工作表 XSSFSheet sheet = workbook.createSheet("工作表一"); //单元格样式 XSSFCellStyle cellStyle = workbook.createCellStyle(); cellStyle.setFillForegroundColor(IndexedColors.PINK.getIndex()); //粉色背景 cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); //颜色填充规格,设置实心的一种颜色 //字体样式 XSSFFont font = workbook.createFont(); font.setFontName("黑体"); font.setColor(IndexedColors.BLUE.getIndex()); //字体蓝色 cellStyle.setFont(font); //把字体样式放入到单元格样式里去 //3、创建行 XSSFRow row = sheet.createRow(0); //创建初始化列,设置其格式 XSSFCell cell = row.createCell(0); cell.setCellValue("商品编号"); cell.setCellStyle(cellStyle); XSSFCell cell1 = row.createCell(1); cell1.setCellValue("商品名称"); cell1.setCellStyle(cellStyle); XSSFCell cell2 = row.createCell(2); cell2.setCellValue("商品价格(单位:元/斤)"); cell2.setCellStyle(cellStyle); XSSFCell cell3 = row.createCell(3); cell3.setCellValue("商品库存(单位:吨)"); cell3.setCellStyle(cellStyle); //输出流 FileOutputStream out = new FileOutputStream("E:\\result.xlsx"); workbook.write(out); out.flush(); //释放资源 out.close(); workbook.close(); System.out.println("写入成功!"); } }

查看写入的excel结果: 在这里插入图片描述

五、从Excel读取内容写入数据库

读取excel内容写入数据库核心代码

public static List read(String path) throws Exception { List productList = new ArrayList(); //这个集合是用来存放多个产品的 //1、获取个作簿 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(path); //2、获取工作表 XSSFSheet sheet = xssfWorkbook.getSheetAt(0); int lastRowNum = sheet.getLastRowNum(); for (int i = 1; i List list = new ArrayList(); //将读取的内容弄放到list集合中 for (Cell cell : row) { if (cell != null) { cell.setCellType(Cell.CELL_TYPE_STRING); //设置格式为string String value = cell.getStringCellValue(); //读取数据 if (value!=null &&!"".equals(value)) { //内容不为空才放进list里 list.add(value); } } } if (list.size() > 0) { Product product = new Product(Integer.parseInt(list.get(0)), list.get(1), Double.parseDouble(list.get(2)), Integer.parseInt(list.get(3))); //放进实体类中 productList.add(product); //将实体类放进专门存储多个产品的表 } } } return productList; }

读取excel表中的数据

//1.1读取excel表中的数据 System.out.println("请输入您要读取的文件位置(不包含空格)"); String path = sc.next(); List productList = read(path); //获得excel表中的内容 System.out.println(productList);

将数据写入数据库

//1.2将数据写入到数据库中 productService.save(productList); System.out.println("数据已存入数据库中!");

其中save方法是通过三层模式其中的Service和dao来实现的 Service层

public void save(List productList) { for (Product product : productList) { productDao.save(product); } }

Dao层

public void save(Product product) { String sql = "insert into product values(?,?,?,?)"; jdbcTemplate.update(sql,product.getPid(),product.getPname(),product.getPrice(),product.getPstock()); }

运行测试: 在这里插入图片描述 插入成功!

六、从数据库读取内容写入Excel表中

读取数据库内容写入Excel核心代码

public static void write(List productList,String path) throws Exception{ //1、创建一个工作簿 XSSFWorkbook xssfWorkbook = new XSSFWorkbook(); //2、创建工作表 XSSFSheet sheet = xssfWorkbook.createSheet("商品"); //单元格样式 XSSFCellStyle cellStyle = xssfWorkbook.createCellStyle(); cellStyle.setFillForegroundColor(IndexedColors.PINK.getIndex()); //粉色背景 cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND); //颜色填充规格,设置实心的一种颜色 //字体样式 XSSFFont font = xssfWorkbook.createFont(); font.setFontName("黑体"); font.setColor(IndexedColors.BLUE.getIndex()); //字体蓝色 cellStyle.setFont(font); //把字体样式放入到单元格样式里去 //3、创建行 XSSFRow row = sheet.createRow(0); //创建初始化列,设置其格式 XSSFCell cell = row.createCell(0); cell.setCellValue("商品编号"); cell.setCellStyle(cellStyle); XSSFCell cell1 = row.createCell(1); cell1.setCellValue("商品名称"); cell1.setCellStyle(cellStyle); XSSFCell cell2 = row.createCell(2); cell2.setCellValue("商品价格(单位:元/斤)"); cell2.setCellStyle(cellStyle); XSSFCell cell3 = row.createCell(3); cell3.setCellValue("商品库存(单位:吨)"); cell3.setCellStyle(cellStyle); for (int i = 0; i return productDao.findAll(); }

Dao层

@Override public List findAll() { String sql="select * from product"; return jdbcTemplate.query(sql,new BeanPropertyRowMapper(Product.class)); }

运行结果: 在这里插入图片描述 在这里插入图片描述 项目已经打包好了放在下面链接中,亲测可用! 本项目所有源码 如果需要免费的请私信我留“excel”关键字~



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有