当前位置:网站首页>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());
    }
原网站

版权声明
本文为[qq_40711092]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_40711092/article/details/125155217