当前位置:网站首页>jg-文件上传代码-以及导出excel
jg-文件上传代码-以及导出excel
2022-06-09 22:10:00 【qq_40711092】
文件上传
/** * 文件上传 * @param str * @param file * @return */
@RequestMapping(path = "/add_drug", method = {
RequestMethod.POST})
public String add_drug(String str, MultipartFile file) {
System.out.println("add_drug:" + str);
if (!file.isEmpty()) {
String upload = FileUploadUtil.fileUpload(file);
System.out.println(upload);
return "添加成功!";
}
return "文件为空!";
}
文件上传工具类
package com.dxy.demo_05_08.util;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
public class FileUploadUtil {
private static String FILEPATH = "E:\\other\\file\\";
public static String fileUpload(MultipartFile file){
if (file.isEmpty()) {
System.out.println("文件为空!");
}
String fileName = file.getOriginalFilename(); // 文件名
String suffixName = fileName.substring(fileName.lastIndexOf(".")); // 后缀名
fileName = UUID.randomUUID() + suffixName; // 新文件名
File dest = new File(FILEPATH + fileName);
if (!dest.getParentFile().exists()) {
dest.getParentFile().mkdirs();
}
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
}
return dest.getPath();
}
}
前端
<form id="editForm" action="${path}/drug/add_drug" method="post" enctype="multipart/form-data">
<input type="file" class="form-control" name="file" placeholder="图片" required>
</form>
导出excel
@RequestMapping(value = "export_consumeMonth", method = RequestMethod.GET)
public void export_consumeMonth(HttpServletResponse response, String month) throws IOException {
XSSFWorkbook workbook = new XSSFWorkbook();
XSSFSheet sheet = workbook.createSheet(month+"消费信息表");
// List<Consume> all = consumeService.selectByMonth(month);
List<String> lists = new ArrayList<>();
lists.add("你好");
//String fileName = month+ "consume你好.xls";//设置要导出的文件的名字
String fileName = URLEncoder.encode(month+ "consume你好.xlsx","UTF-8");
//新增数据行,并且设置单元格数据
int rowNum = 1;
//String[] headers = { "学号", "姓名", "身份类型", "登录密码"};
//headers表示excel表中第一行的表头
String[] headers = {
"部门","用户名","早餐","午餐","晚餐","总计","时间",month+"日总和"};
XSSFRow row = sheet.createRow(0);
//在excel表中添加表头
for(int i=0;i<headers.length;i++){
XSSFCell cell = row.createCell(i);
XSSFRichTextString text = new XSSFRichTextString(headers[i]);
cell.setCellValue(text);
}
Double a=0.0;
//在表中存放查询到的数据放入对应的列
for (String str : lists) {
XSSFRow row1 = sheet.createRow(rowNum);
row1.createCell(0).setCellValue(str+0);
row1.createCell(1).setCellValue(str+1);
row1.createCell(2).setCellValue(str+1);
row1.createCell(3).setCellValue(str+1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日");
row1.createCell(4).setCellValue(sdf.format(new Date()));
rowNum++;
}
response.setContentType("application/octet-stream");
response.setHeader("Content-disposition", "attachment;filename=" + fileName);
response.flushBuffer();
workbook.write(response.getOutputStream());
}
边栏推荐
- C language 0 length array (variable array / flexible array) explanation
- 【滤波器】基于时变维纳滤波器实现语音去噪含Matlab源码
- 【刷题篇】布尔运算
- M-arch (yateli M4) [at-start-f425 evaluation] No.04 CRC
- 【图像分割】基于各向异性热扩散方程的图像分割附matlab代码
- The original tree array can be so simple?
- M-arch (fanwai 10) gd32l233 evaluation -spi drive DS1302
- Expérience de l'optimisation des bases de données
- YUV format and RGB format
- Continuous integration, continuous delivery and continuous deployment (ci/cd) details
猜你喜欢

2022年最系统的自动化测试,测试开发面试题,10k以下不建议看

Find my technology | in the era of Internet of things, apple find my realizes real intelligent loss prevention

lua学习笔记(4)-- 搭建mobdebug 远程开发环境

CMD命令

How to protect personal rights and interests when the universe is not an illegal place?

GUI user login

【轴承故障分解】基于 ITD实现轴承故障信号分解含Matlab源码

Im instant messaging development: Practice of offline messages and historical messages

知乎关注热度25K的问题,自学软件测试,要学到什么程度才能找到工作?
![[BP prediction] BP neural network based on AdaBoost realizes data regression prediction with matlab code](/img/4d/d69ba951123e4f78c4b664ceea59f3.png)
[BP prediction] BP neural network based on AdaBoost realizes data regression prediction with matlab code
随机推荐
ffmpeg加opencv的人脸采集并做出识别的实战项目!
初识WebSocket
[image reconstruction] regularization based image super-resolution reconstruction with matlab code
M-arch (fanwai 14) gd32l233 evaluation - driving segment code LCD
centos+mysql报:Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.socket
Pi of C language test question 162
先睹为快!Benji Bananas 第一季奖励活动数据一览!
Technology sharing behind NFT and opensea transactions
【滤波器】基于时变维纳滤波器实现语音去噪含Matlab源码
【刷题篇】跳跃游戏
抓包工具Fiddler的使用
Lua learning notes (4) -- building mobdebug remote development environment
M-Arch(雅特力M4)【AT-START-F425测评】No.06 驱动段码LCD
The most systematic automated test in 2022, test development interview questions, below 10K, are not recommended
Huawei device configuration hub and spoke
cadence SPB17.4 - allegro - Move With Dynamic Alignment ON/OFF
Find My产品|新款TWS耳机支持Find My功能,苹果Find My在第三方厂家应用更广泛
Information leakage and computational complexity of EMD like methods in time series prediction
数据库优化方面的经验
Find My技术|物联网时代,苹果Find My实现真正的智能防丢