java下载本地文件xlsx

最主要pom的加上这个段,不然会出现文件损坏

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <version>2.6</version>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <nonFilteredFileExtensions>
                        <nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
                    </nonFilteredFileExtensions>
                </configuration>
            </plugin>

代码

    @Override
    public void download(HttpServletResponse response) throws IOException {
        InputStream inputStream = null;
        ServletOutputStream servletOutputStream = null;
        try {
            String filename = "物价信息模板.xlsx";
            String path = "templates/物价信息模板.xlsx";
            org.springframework.core.io.Resource resource = resourceLoader.getResource("classpath:"+path);

            response.setContentType("application/vnd.ms-excel");
            response.addHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            response.addHeader("charset", "utf-8");
            response.addHeader("Pragma", "no-cache");
            String encodeName = URLEncoder.encode(filename, StandardCharsets.UTF_8.toString());
            response.setHeader("Content-Disposition", "attachment; filename=\"" + encodeName + "\"; filename*=utf-8''" + encodeName);

            inputStream = resource.getInputStream();
            servletOutputStream = response.getOutputStream();
            IOUtils.copy(inputStream, servletOutputStream);
            response.flushBuffer();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (servletOutputStream != null) {
                    servletOutputStream.close();
                    servletOutputStream = null;
                }
                if (inputStream != null) {
                    inputStream.close();
                    inputStream = null;
                }
                // 召唤jvm的垃圾回收器
                System.gc();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        
    }

Java解析Excel文件(.xlsx和.xls格式均适用)

Java中读写Excel文件(.xlsx)有多种方法,其中比较常用的是使用Apache POI库。以下是一个简单示例: 1. 导入Apache POI库 你可以在Maven项目中添加以下依赖项: ```xml <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> ``` 或者下载POI库的jar包并将其添加到项目中。 2. 读取Excel文件 ```java import java.io.File; import java.io.FileInputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class ReadExcel { public static void main(String[] args) { try { FileInputStream file = new FileInputStream(new File("example.xlsx")); // 创建工作簿 Workbook workbook = new XSSFWorkbook(file); // 选择第一个工作表 Sheet sheet = workbook.getSheetAt(0); // 迭代行 for (Row row : sheet) { // 迭代单元格 for (Cell cell : row) { // 获取单元格的值 String cellValue = cell.getStringCellValue(); System.out.print(cellValue + "\t"); } System.out.println(); } // 关闭工作簿 workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 上面的代码打开名为“example.xlsx”的Excel文件并打印出所有单元格的值。 3. 写入Excel文件 ```java import java.io.FileOutputStream; import java.io.IOException; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFWorkbook; public class WriteExcel { public static void main(String[] args) { try { // 创建工作簿 Workbook workbook = new XSSFWorkbook(); // 创建工作表 Sheet sheet = workbook.createSheet("Sheet1"); // 创建行 Row row = sheet.createRow(0); // 创建单元格并设置值 Cell cell = row.createCell(0); cell.setCellValue("Hello, world!"); // 写入文件 FileOutputStream file = new FileOutputStream("example.xlsx"); workbook.write(file); // 关闭工作簿 workbook.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 上面的代码创建了一个名为“Sheet1”的工作表,并在第一行第一列写入了“Hello, world!”。最后将工作簿保存到名为“example.xlsx”的文件中。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值