play框架导出excel表格,解决zip文件问题

无需引入依赖,使用的是org.apache.poi

遇到的一些问题:
1、下载的excel文件为zip
zip文件目录如下,修改后缀为xls后为正确的表格

 


解决方法,加上配置信息或者renderBinary方法加上文件名称

我做的是访问接口后直接下载文件,下载结果:

执行代码:
 

package controllers;


import com.alibaba.fastjson2.JSON;
import models.mongodb_biddata.MysqlEnterprisesQichacha;
import okhttp3.*;
import org.apache.http.client.methods.HttpGet;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import play.Logger;
import play.libs.WS;
import play.mvc.Controller;


import java.io.*;
import java.net.URLEncoder;

import java.util.*;

/**
 * @Author: ZD
 * @Date: 2023/7/27
 */
public class ExcelUploading extends Controller {
    private static final String file_path = "C:\\guoxin\\mongodb-mysql/";

    public void getExcel() {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        File exportFile = null;
        FileInputStream fileInputStream = null;
        XSSFWorkbook workbook = new XSSFWorkbook();
        //查询出要插入excel的数据
        List<MysqlEnterprisesQichacha> mysqlEnterprisesQichachaList = MysqlEnterprisesQichacha.find("").fetch(10);
        String fileName = "test";
        try {
            XSSFSheet sheet = workbook.createSheet();

            // 创建头部信息行和单元格
            XSSFRow headerRow = sheet.createRow(0);
            headerRow.createCell(0).setCellValue("标题");
            headerRow.createCell(1).setCellValue("行业");
            headerRow.createCell(2).setCellValue("地区");
            headerRow.createCell(3).setCellValue("类型");
            headerRow.createCell(4).setCellValue("发布时间");
            headerRow.createCell(5).setCellValue("链接");
            headerRow.createCell(6).setCellValue("正文");
            mysqlEnterprisesQichachaList.stream().forEach(mysqlEnterprisesQichacha -> {

                XSSFRow dataRow = sheet.createRow(mysqlEnterprisesQichachaList.indexOf(mysqlEnterprisesQichacha)+1);
                dataRow.createCell(0).setCellValue(mysqlEnterprisesQichacha.getId_zbw());
                dataRow.createCell(1).setCellValue(mysqlEnterprisesQichacha.getComid_zbw());
                dataRow.createCell(2).setCellValue(mysqlEnterprisesQichacha.getComname());
                dataRow.createCell(3).setCellValue(mysqlEnterprisesQichacha.getCreate_time());
                dataRow.createCell(4).setCellValue(mysqlEnterprisesQichacha.getResult());
                dataRow.createCell(5).setCellValue(mysqlEnterprisesQichacha.getResult());
                dataRow.createCell(6).setCellValue(mysqlEnterprisesQichacha.getResult());
            });
            //写入表格数据
            workbook.write(byteArrayOutputStream);

            exportFile = new File(file_path + "app/controllers/"+fileName+".xlsx");
            if (!exportFile.exists()) {
                exportFile.createNewFile();
            }
            FileOutputStream fileOut = new FileOutputStream(exportFile);
            fileOut.write(byteArrayOutputStream.toByteArray());
            fileOut.close();
            fileInputStream = new FileInputStream(exportFile);

        } catch (IOException e) {
            Logger.info("文件转换异常:{}", e);
        }
        //如果导出文件为zip,可以加上以下注释代码,当renderBinary方法没有添加name参数时需要加上
//        response.setContentTypeIfNotSet("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        renderBinary(fileInputStream,fileName+".xls");

    }
}

 我在文件上传后某个空间,然后再进行下载时出现了zip问题未解决!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Play框架并没有内置对Redis的支持,但是你可以使用Java Redis客户端来在Play框架中集成Redis。 首先,你需要在Play框架的项目中添加Redis客户端的依赖。你可以通过在`build.sbt`文件中添加以下行来引入Redis客户端的依赖: ```scala libraryDependencies += "redis.clients" % "jedis" % "3.7.0" ``` 接下来,在Play框架的配置文件(`application.conf`)中配置Redis的连接信息。你可以添加以下配置项来指定Redis的主机、端口和其他相关信息: ``` redis.host=your_redis_host redis.port=your_redis_port ``` 然后,你可以在Play框架的控制器或服务中使用Redis客户端进行操作。下面是一个简单的示例代码: ```java import redis.clients.jedis.Jedis; public class RedisService { private Jedis jedis; public RedisService() { String host = Play.application().configuration().getString("redis.host"); int port = Play.application().configuration().getInt("redis.port"); jedis = new Jedis(host, port); } public void setValue(String key, String value) { jedis.set(key, value); } public String getValue(String key) { return jedis.get(key); } } ``` 在上面的示例中,`RedisService`类使用了Jedis客户端来连接Redis,并提供了向Redis设置值和获取值的方法。 请注意,这只是一个简单示例,实际使用时你可能需要根据自己的业务需求进行适当的封装和处理。另外,记得在使用完Redis后关闭连接,以避免资源泄漏。 希望这能帮到你!如果你有其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值