首页 > 其他分享 >excel导出模板,导入数据 后端代码

excel导出模板,导入数据 后端代码

时间:2023-12-08 19:32:20浏览次数:29  
标签:outputStream template excel person 导入 file response 模板 row

依赖如下

<!-- poi3.9:导出excel -->

  <dependency>

   <groupId>org.apache.poi</groupId>

   <artifactId>poi</artifactId>

   <version>3.9</version>

  </dependency>

  <dependency>

   <groupId>org.apache.poi</groupId>

   <artifactId>poi-ooxml</artifactId>

   <version>3.9</version>

  </dependency>

运行后防止读取excel报错,加上以下配置

<resource>

    <directory>src/main/resources</directory>

    <includes>

        <include>**/*</include>

    </includes>

    <excludes>

        <exclude>template/*.xlsx</exclude>

    </excludes>

    <filtering>true</filtering>

</resource>

<resource>

    <directory>src/main/resources</directory>

    <filtering>false</filtering>

    <includes>

        <include>template/*.xlsx</include>

    </includes>

</resource>

模板文件放入template文件夹中


代码如下

@RequestMapping(value = "downloadExcel")//method = RequestMethod.GET将数据传递给前端

public void downloadExcel(HttpServletResponse response, HttpServletRequest request) throws IOException {

    //获取输入流,原始模板位置

    String filePath = "template/person.xlsx";

    InputStream inputStream = null;

    OutputStream outputStream = null;

    // 获取文件对象

    try {

        inputStream = this.getClass().getClassLoader().getResourceAsStream(filePath);

        XSSFWorkbook wb = new XSSFWorkbook(inputStream);

        // 当客户端请求的资源是一个可下载的资源(这里的“可下载”是指浏览器会弹出下载框或者下载界面)时,对这个可下载资源的描述(例如下载框中的文件名称)就是来源于该头域。

        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + "外包人员信息登记.xlsx");

        // 服务器告诉浏览器它发送的数据属于什么文件类型,也就是响应数据的MIME类型

        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

        response.setCharacterEncoding("utf-8");

        // 关闭缓存(HTTP/1.1)

        response.setHeader("Cache-Control", "no-store");

        // 关闭缓存(HTTP/1.0)

        response.setHeader("Pragma", "no-cache");

        // 缓存有效时间

        response.setDateHeader("Expires", 0);

        outputStream = response.getOutputStream();

        wb.write(outputStream);

        outputStream.flush();

    } catch (Exception e) {

        e.printStackTrace();

    } finally {

        IoUtil.close(inputStream);

        IoUtil.close(outputStream);

    }

}


@PostMapping("/uploadExcel")

public void uploadExcel(

        @RequestParam("file") MultipartFile file) throws Exception {

    this.service.uploadExcel(file);

    ResponseResult.output(ResponseResult.success());

}


public void uploadExcel(MultipartFile file) throws IOException { 

    Workbook workbook = WorkbookFactory.create(file.getInputStream());

    Sheet sheet = workbook.getSheetAt(0);

    int startRow = 3; 

    List<Trainee> list = new ArrayList<>();

    for (Row row : sheet) {

        if (row.getRowNum() >= startRow - 1) {

            // 需要去除空格

            int rowNum = row.getRowNum() + 1;

            String name = row.getCell(1).getStringCellValue().trim();

            CheckFlow.start("表中姓名有误,错误行" + rowNum).isNotNull(name);

            String card = row.getCell(3).getStringCellValue().trim();

            Trainee person = new Trainee();

            person.setName(name);

            person.setIdNo(card); 

            list.add(person);

        }

    }  

    this.saveBatch(list);

}

标签:outputStream,template,excel,person,导入,file,response,模板,row
From: https://blog.51cto.com/u_15266301/8741488

相关文章

  • 私域运营:12个朋友圈经营模板
    做私域运营的各位,想必大家都会烦恼朋友圈要发什么才能保证最高效吧!首先,我们需要明确,朋友圈是什么?朋友圈是我们打造信任感的地方,也是我们的信息能够及时触达用户的重要渠道。很多人都有一个习惯,每当添加一个新好友微信后会去翻一下ta的朋友圈。因为除了添加后说的第一句话,我们要了解......
  • 使用freemarker,导出制作好的ftl模板,并写入数据
    使用freemarker,导出制作好的ftl模板,并写入数据一、背景1.1项目背景最近在开发一个项目,需要导出一些数据,然后写入到word文档中,然后再导出到本地,这个需求是比较常见的,但是我在网上找了很多资料,都没有找到一个比较好的解决方案,所以就自己写了一个,这里分享给大家,希望能帮助到大家......
  • 打印模板学习笔记
     运算1、打印模板多则运算(相当于C#if、switch)//方法1嵌套iif函数=IIF(Fields!盈亏标志.Value="1","盘平",IIF(Fields!盈亏标志.Value="2","盘赢","盘亏"))=Switch((Fields.Item("盘点盈亏标志(黑-盘平,红-盘盈,蓝-盘亏,粗体-停用)").Value)="1","......
  • 在gitlab中如何导出、导入某个项目(提交记录不变),对某个项目进行迁移
    1、需求说明 在项目的迁移中遇到,需要将gitlab中的某个项目进行迁移的过程。关键点是,需要保持提交记录不变。 本文档下面的内容,就介绍这个过程。 2、项目导出、导入的过程 2.1、导出项目 a、登录到gitlab页面  b、点击要迁移的项目   可以看到项目......
  • pageoffice 6 实现数据区域填充(插入文本、图片、word、excel等)
    在实际的Word文档开发中,经常需要自动填充数据到Word模板中,以生成动态的Word文档。例如:1、我们可以根据数据库表中已保存的个人信息,设计好一个简历模板docx文件,然后通过代码将这些个人信息填充到Word模板中,从而自动生成一份简历。2、如果需要将图片插入到Word模板指定位置,比如......
  • 已解决:若依更换日志EasyExcel框架导出报错 java.lang.NoClassDefFoundError: org/apac
    先描述一下当时的场景回忆看到出错了,我就想可能是哪个路径写错了,或者导依赖名字写对,或者说是多了少了标点符号什么的。然而,还是想简单了,检查重启后发现问题并没有解决。于是就把所有我改过的地方检查了一遍,检查和这个依赖相关的代码。发现还是没啥关系后来去找百度、百度给的......
  • Python将列表数据保存为excel
    一、需求背景工作需要将列表数据写入到excel中,方便运营同学查看,数据示例如下:data_0=[[['Name','Age','Gender'],['Jack',22,'Male'],['Tom',34,'Female']],[['id&#......
  • python如何提取excel表格中的超链接
    importxlrddefget_wb(path):wb=xlrd.open_workbook(path)returnwbdefget_wb(path):wb=xlrd.open_workbook(path)returnwbdefget_hyperlink(wb,sheet_name,cell):worksheet=wb.sheet_by_name(sheet_name)hyperlink=worksheet.......
  • python将图片写入excel
    importjsonimportpandasaspdfromopenpyxlimportWorkbookfromopenpyxl.drawing.imageimportImageexcel_col_map={1:"A",2:"B",3:"C",4:"D",5:"E",6:"F&quo......
  • Python-xlrd读取Excel指定列a~b行数据并绘图
    importxlrd#读取Excel文件wb=xlrd.open_workbook(r"E:\PythonStudyAll\TestD20231130\1111.xlsx")data=wb.sheet_by_name('Sheet1')#定义要读取的列和行范围column_index_x=0#第一列的索引为0column_index_y=1#第一列的索引为0start_row=2#起始行索......