Echarts之柱状图动态加载数据篇
文件上传下载篇
老规矩,先上效果:
模拟业务流程:1.在本地的excel模板表中填写好数据,点击“选择文件”→“上传Excel表格”,将Excel表解析,并展示在页面上,并持久化数据。
2.点击“导出成Excel表格”,将页面上的数据,导出成Excel表格。
前端使用H-ui修改的界面。
依赖:
<!--easyexcel依赖-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.2</version>
</dependency>
<!--poi依赖-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.0</version>
</dependency>
前端 部分 代码:
<div class="mt-20">
<table class="table table-border table-bordered table-bg">
<thead>
<tr class="text-c">
<th width="120">id</th>
<th width="70">药品名称</th>
<th width="80">销售件数</th>
<th width="200">药品价格</th>
<th>销售日期</th>
<th width="100">用户名</th>
</tr>
</thead>
<tbody>
<c:forEach items="${excels}" var="sale">
<tr class="text-c">
<td>${sale.id}</td>
<td>${sale.salmedicinename}</td>
<td>${sale.salamount}件</td>
<td>${sale.salprice}元</td>
<%--格式化日期--%>
<td><fmt:formatDate value="${sale.saldate}" pattern="yyyy-MM-dd hh:mm:ss"/></td>
<td>${sale.salname}</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
</div>
Controller:
/**
* 解析上传的excel表格,并显示到页面上
* @param file
* @param request
* @return
* @throws IOException
*/
List<Excel> ExcelList = null;
@RequestMapping("/excel/import")
public String ImportExcel(@RequestParam MultipartFile file , HttpServletRequest request) throws IOException {
ExcelExample example = new ExcelExample();
//检查数据库excel表已经存在数据
List<Excel> nullornot = tssi.ExcelIsNull(example);
//存在的话,清空excel表数据
if(nullornot.size() != 0){
tssi.DeleteExcel();
}
InputStream inputStream = file.getInputStream();
ExcelList = EasyExcel.read(inputStream)
.head(Excel.class)
// 设置sheet,默认读取第一个
.sheet()
// 设置标题所在行数
.headRowNumber(1)
.doReadSync();
//持久化excel表
for (Excel excel : ExcelList) {
tssi.InsertExcel(excel);
}
HttpSession session = request.getSession();
session.setAttribute("excels" , ExcelList);
return "redirect:/excel";
}
/**
* 导出页面上的excel表
* @param response
* @throws IOException
*/
@RequestMapping("/excel/export")
public void ExportExcel(HttpServletResponse response) throws IOException {
// 获取页面上的Excel
List<Excel> excels = new ArrayList<>();
//将页面上的数据导出成excel
for ( Excel excel : ExcelList) {
excels.add(excel);
}
//设置JSP页面导出为excel表
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
// 防止中文乱码
String fileName = URLEncoder.encode("导出", "UTF-8");
response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), Excel.class)
.sheet("sheet0")
.doWrite(excels);
}