SpringBoot Excel导入模板下载
可以配置国际化
跨服务读取文件pom配置
serviceImpl所在服务
<build>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
文件所在服务
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<skip>true</skip>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<version>3.1.0</version>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsm</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
@GetMapping("/downModuleXls")
@ApiOperation(value = "设备导入模板下载", notes = "设备导入模板下载")
public R<Void> downModuleXls(HttpServletRequest request, HttpServletResponse response) {
return R.success(deviceInfoService.downModuleResourceXls(request, response));
}
@Override
public Void downModuleResourceXls(HttpServletRequest request, HttpServletResponse response) {
String downLoadFileName = messageSourceUtil.getMessage(I18nConstant.PlatformImportModule.FILE_NAME);
String sheetName = messageSourceUtil.getMessage(I18nConstant.PlatformImportModule.SHEET_NAME);
String pileName = messageSourceUtil.getMessage(I18nConstant.PlatformImportModule.title.PILE_NAME);
String pileSn = messageSourceUtil.getMessage(I18nConstant.PlatformImportModule.title.SN);
String pilePin = messageSourceUtil.getMessage(I18nConstant.PlatformImportModule.title.PIN);
String[] titleArray = {pileName, pileSn, pilePin};
log.info("下载文件名称:{}, sheet名称:{}, 表头数组:{}", downLoadFileName, sheetName, JSON.toJSONString(titleArray));
FileInputStream fileInputStream = null;
Workbook workbook = null;
OutputStream outputStream = null;
try {
String resourceFileName = "设备导入模板.xlsx";
String resourceFilePath = "xls/" + resourceFileName;
InputStream inputStream = DeviceInfoExcelEntity.class.getClassLoader().getResourceAsStream(resourceFilePath);
File tempFile = File.createTempFile("test", ".xlsx");
FileUtils.writeToFile(tempFile, inputStream);
//sheet名称、标题国际化
fileInputStream = new FileInputStream(tempFile.getPath());
workbook = new XSSFWorkbook(fileInputStream);
outputStream = new FileOutputStream(tempFile.getPath());
Sheet sheet = workbook.getSheetAt(0);
workbook.setSheetName(0, sheetName);
sheet.setActiveCell(new CellAddress(0, 0));
Row row = sheet.getRow(0);
for (int i = 0; i < titleArray.length; i++) {
Cell cell = row.getCell(i);
cell.setCellValue(titleArray[i]);
}
workbook.write(outputStream);
//下载
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
downLoadFileName = URLEncoder.encode(downLoadFileName, "UTF-8").replace("+", "%20");
response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + downLoadFileName + ".xlsx");
byte[] fileToByteArray = FileUtils.readFileToByteArray(tempFile);
//加了这句Excel下载下来就不用修复了
response.setContentLength(fileToByteArray.length);
response.getOutputStream().write(fileToByteArray);
} catch (IOException e) {
log.error("downModuleResourceXls:" + e);
} finally {
if (workbook != null) {
try {
workbook.close();
} catch (IOException e) {
log.error("DeviceInfoServiceImpl.downModuleResourceXls close workbook exception = ", e);
}
}
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
log.error("DeviceInfoServiceImpl.downModuleResourceXls close fileInputStream exception = ", e);
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
log.error("DeviceInfoServiceImpl.downModuleResourceXls close outputStream exception = ", e);
}
}
}
return null;
}