最菜的Java导入excel
poi Maven
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>3.14</version>
</dependency>
基础代码
package com.peas.mdp.module.util;
import com.peas.mdp.dto.Teacher;
import org.apache.poi.ss.usermodel.*;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
public class ExcelUtil {
public static List<?> getBankListByExcel(InputStream in) throws Exception {
List<Teacher> list = new ArrayList<>();
//创建Excel工作薄
Workbook work = getWorkbook(in);
if (null == work) {
throw new Exception("创建Excel工作薄为空!");
}
Sheet sheet = null;
Row row = null;
Cell cell = null;
//遍历Excel中所有的sheet
for (int i = 0; i < work.getNumberOfSheets(); i++) {
sheet = work.getSheetAt(i);
if (sheet == null) {
continue;
}
//遍历当前sheet中的所有行
for (int j =1; j <= sheet.getLastRowNum(); j++) {
row = sheet.getRow(j);
if (row == null ) {
continue;
}
Teacher teacher = new Teacher();
//把每个单元格的值付给对象的对应属性
if (row.getCell(0)!=null){
teacher.setAccount(String.valueOf(getCellValue(row.getCell(0))));
}
if (row.getCell(1)!=null){
teacher.setName(String.valueOf(getCellValue(row.getCell(1))));
}
if (row.getCell(2)!=null){
teacher.setSex(Integer.valueOf((String) getCellValue(row.getCell(2))));
}
if (row.getCell(3)!=null){
teacher.setAge(Integer.valueOf((String) getCellValue(row.getCell(3))));
}
if (row.getCell(4)!=null){
teacher.setEducation(String.valueOf(getCellValue(row.getCell(4))));
}
if (row.getCell(5)!=null){
teacher.setJobTitle(String.valueOf(getCellValue(row.getCell(5))));
}
if (row.getCell(6)!=null){
teacher.setPosition(String.valueOf(getCellValue(row.getCell(6))));
}
if (row.getCell(7) != null) {
teacher.setRole(String.valueOf(getCellValue(row.getCell(7))));
}
if (row.getCell(8) != null) {
teacher.setPhone(String.valueOf(getCellValue(row.getCell(8))));
}
if (row.getCell(9) != null) {
teacher.setEmail(String.valueOf(getCellValue(row.getCell(9))));
}
if (row.getCell(10) != null) {
teacher.setKeyWords(String.valueOf(getCellValue(row.getCell(10))));
}
if (row.getCell(11) != null) {
teacher.setIntroduction(String.valueOf(getCellValue(row.getCell(11))));
}
if (row.getCell(12) != null) {
teacher.setIdCard(String.valueOf(getCellValue(row.getCell(12))));
}
if (row.getCell(13) != null) {
teacher.setType(Integer.valueOf((String)getCellValue(row.getCell(13))));
}
//遍历所有的列(把每一行的内容存放到对象中)
list.add(teacher);
}
}
return list;
}
/**
*
* @param inStr
* @return
* @throws Exception
*/
public static Workbook getWorkbook(InputStream inStr) throws Exception {
Workbook wb = null;
wb = WorkbookFactory.create(inStr);
return wb;
}
/**
* 描述:对表格中数值进行格式化
*
* @param cell
* @return
*/
public static Object getCellValue(Cell cell) {
Object value = null;
DecimalFormat df = new DecimalFormat("0"); //格式化number String字符
SimpleDateFormat sdf = new SimpleDateFormat("yyy-MM-dd"); //日期格式化
DecimalFormat df2 = new DecimalFormat("0"); //格式化数字
switch (cell.getCellType()) {
case Cell.CELL_TYPE_STRING:
value = cell.getRichStringCellValue().getString();
break;
case Cell.CELL_TYPE_NUMERIC:
if ("General".equals(cell.getCellStyle().getDataFormatString())) {
value = df.format(cell.getNumericCellValue());
} else if ("m/d/yy".equals(cell.getCellStyle().getDataFormatString())) {
value = sdf.format(cell.getDateCellValue());
} else {
value = df2.format(cell.getNumericCellValue());
}
break;
case Cell.CELL_TYPE_BOOLEAN:
value = cell.getBooleanCellValue();
break;
case Cell.CELL_TYPE_BLANK:
value = "";
break;
default:
break;
}
return value;
}
}
Control
@ApiOperation("excel导入")
@GET
@Path("excelFile")
public Map<String, String> readExcel(@QueryParam("path") File path) throws Exception {
HashMap<String, String> map = new HashMap<>();
//读取excel中的内容
InputStream inputStream = new FileInputStream(path);
List<Teacher> teachers = (List<Teacher>) ExcelUtil.getBankListByExcel(inputStream);
for (Teacher teacher : teachers) {
teacherService.saveOrUpdate(teacher);
}
return map;
}
/**
* 读取excel更新数据
*
* @param upfile
* @param
* @param
* @throws Exception
*/
/*
@RequestMapping(value = "/readExcel")
@Transactional
public Map<String, String> readExcel(MultipartFile upfile) throws Exception {
HashMap<String, String> map = new HashMap<>();
InputStream in = upfile.getInputStream();
//读取excel中的内容
List<LineDownOrder> lineDownOrders = ExcelUtils.getBankListByExcel(in);
String s = orderService.updateByExcel(lineDownOrders);
map.put("status", s);
return map;
}
*/

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
-
上一篇
开启Golang编程第一章
Go is an open source programming language that makes it easy to build simple,reliable, and effcient software. Try Go package main import "fmt" func main() { fmt.Println("Hello, 世界") } https://tour.golang.org/welcome/1 体会:和C/C++、C#、java类似的语言,好基友推荐学习的语言,emmm第一步还是蛮简单的。 好吧第一个花括号不能换行,哈哈哈哈。网名:浩秦; 邮箱:root#landv.pw; 只要我能控制一個國家的貨幣發行,我不在乎誰制定法律。金錢一旦作響,壞話隨之戛然而止。
-
下一篇
好好说说Java中的常量池之Class常量池
在Java中,常量池的概念想必很多人都听说过。这也是面试中比较常考的题目之一。在Java有关的面试题中,一般习惯通过String的有关问题来考察面试者对于常量池的知识的理解,几道简单的String面试题难倒了无数的开发者。所以说,常量池是Java体系中一个非常重要的概念。 谈到常量池,在Java体系中,共用三种常量池。分别是字符串常量池、Class常量池和运行时常量池。 本文是《好好说说Java中的常量池》系列的第一篇,先来介绍一下到底什么是Class常量池。 什么是Class文件 在Java代码的编译与反编译那些事儿中我们介绍过Java的编译和反编译的概念。我们知道,计算机只认识0和1,所以程序员写的代码都需要经过编译成0和1构成的二进制格式才能够让计算机运行。 我们在深入分析Java的编译原理中提到过,为了让Java语言具有良好的跨平台能力,Java独具匠心的提供了一种可以在所有平台上都能使用的一种中间代码——字节码(ByteCode)。 有了字节码,无论是哪种平台(如Windows、Linux等),只要安装了虚拟机,都可以直接运行字节码。 同样,有了字节码,也解除了Java虚拟机...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- SpringBoot2整合MyBatis,连接MySql数据库做增删改查操作
- Dcoker安装(在线仓库),最新的服务器搭配容器使用
- MySQL数据库在高并发下的优化方案
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS8编译安装MySQL8.0.19
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- CentOS7,8上快速安装Gitea,搭建Git服务器
- SpringBoot2整合Thymeleaf,官方推荐html解决方案