您现在的位置是:首页 > java技术交流java技术交流
springboot 使用Apache POI Excel文件读取和写入
上善若水2021-01-16 11:29:49【java技术交流】 3188人已围观
简介Excel作为常用的办公文件,存储数据十分便捷,工作中也是大范围使用.在搜索找到最广泛的处理excel文件的包还Apache的工具包!试试excel在web项目中的基本使用吧! 基本工作 准备一个基本
Excel作为常用的办公文件,存储数据十分便捷,工作中也是大范围使用.在搜索找到最广泛的处理excel文件的包还Apache的工具包!
试试excel在web项目中的基本使用吧!
基本工作 准备一个基本的springboot项目 略
导包
导入最新org.apache.poi 目前最新的是4.1.2
<!-- https://mvnrepository.com/artifact/org.apache.poi/poi -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.2</version>
</dependency>
编写简单页excel.html面测试上传和下载
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>测试上传demo</title>
</head>
<body>
<form action="/uploadExcel" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="提交">
</form>
<a href="/downExcel">导出excel</a>
</body>
</html>
编写控制器方法
@GetMapping("/excel")
public String excel() {
return "excel";
}
@GetMapping("/downExcel")
public String downExcel(HttpServletResponse response) throws IOException {
String[] head = {"头1", "头1", "头3"};
List<String[]> values = new ArrayList<>();
for (int i = 0; i < 10; i++) {
values.add(new String[]{"php最好的语言" + i, "java天下第一" + i, "python我年轻我骄傲" + i});
}
HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.createSheet("Sheet1");
HSSFRow row = sheet.createRow(0);
row.createCell(0).setCellValue(head[0]);
row.createCell(1).setCellValue(head[1]);
row.createCell(2).setCellValue(head[2]);
for (int i = 1; i <= values.size(); i++) {
row = sheet.createRow(i);
for (int j = 0; j < values.get(i - 1).length; j++) {
HSSFCell cell = row.createCell(j);
cell.setCellValue(values.get(i - 1)[j]);
}
}
workbook.setActiveSheet(0);
response.setCharacterEncoding("utf-8");
response.setContentType("multipart/form-data");
response.setHeader("Content-Disposition", "attachment;fileName=temp.xlsx");
ServletOutputStream outputStream = response.getOutputStream();
workbook.write(outputStream);
return null;
}
@PostMapping("/uploadExcel")
@ResponseBody
public List uploadExcel(@RequestParam(value = "file") MultipartFile file) throws IOException {
HSSFWorkbook workbook = new HSSFWorkbook(file.getInputStream());
//获取工作表
HSSFSheet sheet = workbook.getSheetAt(0);
List<List<String>> data=new ArrayList<>();
int lastRowNum = sheet.getLastRowNum();
for (int i = 0; i < lastRowNum; i++) {
//获取指定行
HSSFRow row = sheet.getRow(i);
if (row == null) {
continue;
}
List<String> rowList=new ArrayList<>();
short lastCellNum = row.getLastCellNum();
for (int j = 0; j < lastCellNum; j++) {
HSSFCell cell = row.getCell(j);
rowList.add(cell.getStringCellValue());
}
data.add(rowList);
}
workbook.close();
return data;
}
最后实现效果.
很赞哦! (3)
相关文章
随机图文
-
那些好听音乐的神评论比笑话还搞笑
1.这首歌救过我的命,当年我的腿被石头压住,等了好久都没有人来救,我掏出手机放了这首歌,腿就自觉的抖了起来,把石头抖得粉碎,后来我得救了2.昨天戴着耳机听这首歌睡着了,第二天起来发现右耳机没声音了,到医院一检查,还好是耳朵聋了,耳机没事,吓死我了。3.自从听了这首歌,喝了几瓶啤酒,打了鸡血一样,把路边的宝马咂了稀巴烂,醒了才知道这么不怕死4.我一手扛着收音机放着此歌,蹬着三轮车就往高速 -
js中使用 encodeURI "#"井号出错 #号后参数被截取
js中使用 encodeURI将请求参数转码试,,#井号出错,#后的字符串不能正常进行转码,后台无法正确处理参数 解决方法:将encodeURI 替换成encodeURIComponent函数即可. -
'com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor' is deprecated idea提示被弃用
‘com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor’isdeprecatedidea提示被弃用,今天升级了myba -
php技术提升心得与方法
现在的PHP市场虽然充斥了大量的的PHP开发人员,但这些人当中真正能称得上高手的却寥寥无几。很多公司虽然招聘了一些PHP开发人员,但是由于技术水平不高,导致公司的项目一直堆积。这不仅另公司无奈也让已经入职的PHP开发人员着急,他们也想要在PHP领域更近一步,但却苦于找不到提高自己的方法,下面我们的鸥仔收集了一些PHP大神的一些工作方式、习惯,让大家看看PHP大神们是如何工作,也希望这些方法能帮助到那些想要在PHP领域更近一步的人。