Springboot如何通过jxls导入导出excel

转载自
一、必须的maven依赖(可在jxls的官网上去找最新版本):

   <dependency>
  
    <groupId>org.jxls</groupId>
    <artifactId>jxls</artifactId>
    <version>2.4.6</version>
</dependency>
 
<dependency>

    <groupId>org.jxls</groupId>
    <artifactId>jxls-poi</artifactId>
    <version>1.0.15</version>
</dependency>   

二、设置Excel模板(新建一个Excel文档,按如下步骤设置),将新建的文档放到项目中(本案例是放到项目的resource目录下,其他目录不知道可以不)在这里插入图片描述
在这里插入w描述
在这里插入图片描述
在这里插入图片描述
三、代码编写:
@RequestMapping(value = “exportDeviceModelMsg”,method = RequestMethod.GET)
@ResponseBody
public void exportDeviceModelMsg(HttpServletRequest request, HttpServletResponse response) {

   try {
       List<MyTest> myTestList = myTestService.findAll(); //获取列表数据
       InputStream in = this.getClass().getClassLoader().getResourceAsStream("excel/test.xls");   //得到文档的路径
       //列表数据将存储到指定的excel文件路径,这个路径是在项目编译之后的target目录下
       FileOutputStream out = new FileOutputStream("target/classes/excel/aaaa.xls");
       //这里的context是jxls框架上的context内容
       org.jxls.common.Context context = new org.jxls.common.Context();
       //将列表参数放入context中
       context.putVar("myTestList", myTestList);
       //将List<Exam>列表数据按照模板文件中的格式生成到scoreOutput.xls文件中
       JxlsHelper.getInstance().processTemplate(in, out, context);

       //下面步骤为浏览器下载部分
       //指定数据生成后的文件输入流(将上述out的路径作为文件的输入流)
       FileInputStream fileInputStream = new FileInputStream("target/classes/excel/aaaa.xls");
       //导出excel文件,设置文件名
       String filename = URLEncoder.encode("test信息.xls", "UTF-8");
       //设置下载头
       response.setHeader("Content-Disposition", "attachment;filename=" + filename);
       ServletOutputStream outputStream = response.getOutputStream();
       //将文件写入浏览器
       byte[] bys = new byte[fileInputStream.available()];
       fileInputStream.read(bys);
       outputStream.write(bys);
       outputStream.flush();
       outputStream.close();
       fileInputStream.close();
       if (file.exists()) {
          file.delete();
        }
   } catch (Exception e) {
       e.printStackTrace();
   }

}

转载自添加链接描述
导入excel数据功能的话
第一步:先确定好Excel导入的格式以及各表格字段值的含义
在这里插入图片描述
第二步:定义好解析的XML–videoConfig.xml;

<?xml version="1.0" encoding="UTF-8"?> 
<workbook> 
   <worksheet name="Sheet1"> 
    <section startRow="0" endRow="0"/> 
    <loop startRow="1" endRow="1" items="videoInfoList" var="videoInfo" varType="com.iflytek.weike.job.bo.VideoInfo"> 
    <section startRow="1" endRow="1"> 
    <mapping row="1" col="0">

    videoInfo.index</mapping> 
        <mapping row="1" col="1">videoInfo.videoName</mapping> 
        <mapping row="1" col="2">videoInfo.resourceId</mapping> 
        <mapping row="1" col="3">videoInfo.upload</mapping> 
        <mapping row="1" col="4">videoInfo.content</mapping> 
        <mapping row="1" col="5">videoInfo.schoolName</mapping>
       </section> 
        <loopbreakcondition> 
         <rowcheck offset="0">
            <cellcheck offset="0"></cellcheck> 
          </rowcheck> 
        </loopbreakcondition> 
        </loop> 
        </worksheet> 
    </workbook>

第三步:生成一下解析的实体类PipSysAccount(这个需要根据excel文件的列去手工写一个)
第四步:Controller 接受一个excel文件,然后返回excel的数据

public List<PipSysAccount> setExcel(MultipartFile file) {
    List<PipSysAccount> videoInfoList = new ArrayList<PipSysAccount>();
    try {
        InputStream inputXML = new BufferedInputStream(getClass().getClassLoader().getResourceAsStream("videoConfig.xml"));
        XLSReader mainReader = ReaderBuilder.buildFromXML(inputXML);
        InputStream inputXLS = new BufferedInputStream(file.getInputStream());
        PipSysAccount videoInfo = new PipSysAccount();
        Map<String, Object> beans = new HashMap<String, Object>();
        beans.put("videoInfo", videoInfo);
        beans.put("videoInfoList", videoInfoList);
        XLSReadStatus readStatus = mainReader.read(inputXLS, beans);
        if (readStatus.isStatusOK()) {
            System.err.println("jxls读取Excel成功!");
        }
    } catch (Exception e) {
        System.err.println("格式错误");
    }
    return videoInfoList;
}

xml读取excel有可能会涉及的一些问题

<?xml version="1.0" encoding="UTF-8"?>
<workbook>
    <worksheet name="基础基213">
        <section startRow="0" endRow="5"/>    标题等有6行
       
        <loop startRow="1" endRow="2" items="videoInfoList" var="videoInfo" varType="com.lmbx.edu.data.common.domain.PipSysAccount">  两行数据对应一个对象时将endRow改成2`在这里插入代码片`
            <section startRow="1" endRow="2">  
              <mapping row="1" col="0">videoInfo.id</mapping>
                <mapping row="1" col="1">videoInfo.loginName</mapping>
                <mapping row="1" col="2">videoInfo.status</mapping>
                <mapping row="1" col="3">videoInfo.createDatetime</mapping>
                <mapping row="1" col="5">videoInfo.loginName</mapping>
                 <mapping row="2" col="5">videoInfo.pwd</mapping>
            </section>
            <loopbreakcondition>
                <rowcheck offset="0">
                    <cellcheck offset="0"></cellcheck>
                </rowcheck>
            </loopbreakcondition>
        </loop>
    </worksheet>
  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Jxls 是一个开源的 Java 库,用于导出 Excel 文件,它可以在 Java 中非常方便地进行使用。下面是使用 Jxls 导出 Excel 的步骤: 1. 添加依赖 在 pom.xml 文件中添加以下依赖: ```xml <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-core</artifactId> <version>1.0.15</version> </dependency> ``` 2. 准备 Excel 模板 在 Excel 文件中准备好要导出的内容,包括表头和数据部分。可以在表格中使用 ${} 来标记需要动态替换的数据。 3. 准备数据 在 Java 代码中准备好要导出的数据,可以使用 List 或者 Map 等类型来保存数据。 4. 创建模板引擎 使用 Jxls 提供的模板引擎创建一个模板,可以使用以下代码: ```java InputStream is = new FileInputStream(new File("template.xls")); Workbook workbook = WorkbookFactory.create(is); Transformer transformer = TransformerFactory.createTransformer(workbook, outputStream); ``` 其中,“template.xls”是你准备好的 Excel 模板文件名,outputStream 是导出文件的输出流。 5. 填充数据 使用 Jxls 提供的 API 填充数据,可以使用以下代码: ```java Map<String, Object> beans = new HashMap<>(); beans.put("dataList", dataList); transformer.transformXLS(new HashMap<>(), beans); ``` 其中,“dataList”是你准备好的数据,transformer.transformXLS() 方法将会把数据填充到模板中。 6. 输出文件 使用 Jxls 提供的 API 输出文件,可以使用以下代码: ```java transformer.flush(); outputStream.close(); ``` 这样就可以将 Excel 文件导出到 outputStream 中了。 以上是使用 Jxls 导出 Excel 的基本步骤,你可以根据自己的需求进行更多的调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值