import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.util.List;
/**
* @author
* @version 1.0
* @description:
* @date
*/
@RestController
@RequestMapping("excel")
public class ExcelController {
@GetMapping()
public void excel(HttpServletResponse response) throws FileNotFoundException {
FileInputStream fis = new FileInputStream("D:\\test.xlsx");
try {
List<List<Object>> lists = importExcel(null, fis);
exportExcel(lists, response);
} catch (IOException e) {
e.printStackTrace();
}
}
public static List<List<Object>> importExcel(MultipartFile file, InputStream in) throws IOException {
ExcelReader excelReader = ExcelUtil.getReader(in);
return excelReader.read();
}
public static void exportExcel(List<List<Object>> list, HttpServletResponse response) throws IOException {
ExcelWriter writer = ExcelUtil.getWriter();
List<Object> rowHead = list.get(0);
//rowHead.add(4, "herd");
writer.writeHeadRow(rowHead);
writer.write(list);
response.setContentType("application/vnd.ms-excel;charset=utf-8");
response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode("test-" + DateUtil.today() + ".xls", "utf-8"));
ServletOutputStream out = response.getOutputStream();
writer.flush(out, true);
writer.close();
IoUtil.close(out);
}
public static String rule(String cvsString) {
String one = cvsString.substring(0, 10);
String two = cvsString.substring(13, 16);
String three = cvsString.substring(16);
int i1 = Integer.parseInt(three);
if (i1 > 99) {
return "数据异常";
}
int i;
if (i1 > 9) {
i = i1;
} else {
i = i1 * 10;
}
return one + "132" + two + i + "**";
}
}
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.6.6</version>
</dependency>