package com.example.yygoods.controller;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.core.io.Resource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.apache.poi.ss.usermodel.*;
import java.io.*;
import java.util.*;
/**
* 把当前项目下file.txt文件,以二进制流的形式返回给客户端,主要类ResponseEntity
* ResponseEntity用于封装HTTP响应的相关信息,包括状态码、响应头和响应体。它通常用于控制器方法中返回一个包含特定数据的HTTP响应
*/
@RestController
public class DownController {
/**
* 文本文件下载
*/
@GetMapping("/downloadFile")
public ResponseEntity<Resource> downloadFile() {
String projectDir = System.getProperty("user.dir");
System.out.println(projectDir);
// 假设我们有一个文件位于我们的文件系统中
File file = new File(projectDir + "\\file.txt");
HttpHeaders header = new HttpHeaders();
// 添加响应头,以文本的形式展示
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.txt");
// 构建文件响应实体
ResponseEntity<Resource> entity = ResponseEntity.ok()
.headers(header)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
return entity;
}
/**
* 图片文件下载
*/
@GetMapping("/downloadImage")
public ResponseEntity<Resource> downloadImage() {
String projectDir = System.getProperty("user.dir");
System.out.println(projectDir);
// 假设我们有一个文件位于我们的文件系统中
File file = new File(projectDir + "\\file.jpg");
HttpHeaders header = new HttpHeaders();
// 添加响应头,以图片的形式展示
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.jpg");
// 构建文件响应实体
ResponseEntity<Resource> entity = ResponseEntity.ok()
.headers(header)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
return entity;
}
/**
* xls文本文件下载,并插入内容
*/
@GetMapping("/downloadXls")
public ResponseEntity<Resource> downloadXls() {
String projectDir = System.getProperty("user.dir");
System.out.println(projectDir);
// 假设我们有一个文件位于我们的文件系统中
File file = new File(projectDir + "\\file.xls");
File template = new File(projectDir + "\\template.xls");
DownController.modifyXls(file, template);
HttpHeaders header = new HttpHeaders();
// 添加响应头,以xls文件的形式展示
header.add(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=file.xls");
// 构建文件响应实体
ResponseEntity<Resource> entity = ResponseEntity.ok()
.headers(header)
.contentType(MediaType.parseMediaType("application/octet-stream"))
.body(new FileSystemResource(file));
return entity;
}
/**
* 修改文件内容
* 准备一个模版文件,这个模版始终不会变
* 读取模版文件的IO流
* 内容遍历,找到每列中的每行,插入对应数据内容
*/
public static void modifyXls(File file, File template) {
try {
// 模拟动态文档数据
HashMap<Integer, String> hashmap1= new HashMap<Integer, String>();
hashmap1.put(1, "序列");
hashmap1.put(2, "种类");
hashmap1.put(3, "型号");
HashMap<Integer, String> hashmap2= new HashMap<Integer, String>();
hashmap2.put(1, "1");
hashmap2.put(2, "榴莲");
hashmap2.put(3, "A果");
HashMap<Integer, String> hashmap3= new HashMap<Integer, String>();
hashmap3.put(1, "2");
hashmap3.put(2, "西瓜");
hashmap3.put(3, "C果");
List<HashMap> fruits = new ArrayList<>();
fruits.add(hashmap1);
fruits.add(hashmap2);
fruits.add(hashmap3);
System.out.println(fruits);
// 读取输入流
InputStream inputStream = new FileInputStream(template);
Workbook workbook = new HSSFWorkbook(inputStream);
// 获取几个sheet表
Sheet sheet = workbook.getSheetAt(0);
for (int i = 0; i < fruits.size(); i++) {
HashMap map = fruits.get(i);
Row row = sheet.getRow(i);
if (row == null) {
row = sheet.createRow(i);
}
// Map转为集合类型,在进行遍历直到不存在下一个迭代器为止
Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator();
Integer idx = 0;
while (iterator.hasNext()) {
Map.Entry<Integer, String> entry = iterator.next();
Cell cell = row.getCell(idx); // 修改第一列
if (cell == null) {
cell = row.createCell(idx);
}
cell.setCellValue(entry.getValue());
idx++;
System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
}
}
// 保存修改后的文件到磁盘
FileOutputStream fos = new FileOutputStream(file);
workbook.write(fos);
fos.close();
workbook.close();
// 可能发生异常的语句
} catch(Exception e) {
// 处理异常语句
System.out.println(e);
}
}
}
如果是是动态内容,需要在pom.xml配置依赖包
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.3</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.3</version>
</dependency>