SpringBoot Excel导入模板下载

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;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值