服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - Java中实现简单的Excel导出

Java中实现简单的Excel导出

2021-07-02 15:04Haozz_1994 Java教程

今天小编就为大家分享一篇关于Java中实现简单的Excel导出,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

简单介绍一下java中的excel文件导出功能(基于httpservletresponse实现下载)

首先,引入需要依赖的jar包:

?
1
2
3
4
5
6
7
8
9
10
<dependency>
 <groupid>org.apache.poi</groupid>
 <artifactid>poi</artifactid>
 <version>3.14</version>
</dependency>
<dependency>
 <groupid>org.apache.poi</groupid>
 <artifactid>poi-ooxml</artifactid>
 <version>3.14</version>
</dependency>

编写一个工具类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package exceloutput;
import org.apache.commons.lang3.stringutils;
import org.apache.poi.ss.usermodel.cell;
import org.apache.poi.ss.usermodel.row;
import org.apache.poi.ss.usermodel.workbook;
import org.apache.poi.xssf.streaming.sxssfsheet;
import org.apache.poi.xssf.streaming.sxssfworkbook;
import javax.servlet.http.httpservletresponse;
import java.io.ioexception;
import java.io.unsupportedencodingexception;
import java.util.list;
import java.util.uuid;
/**
 * @author haozz
 * @date 2018/6/6 9:57
 * @description excel导出抽象工具类
 **/
public abstract class exportabstractutil {
  public void write(httpservletresponse response, workbook workbook){
    string filename = uuid.randomuuid().tostring()+".xls";
    pwrite(response,workbook,filename);
  }
  public void write(httpservletresponse response,workbook workbook,string filename){
    if(stringutils.isempty(filename)){
      filename = uuid.randomuuid().tostring()+".xls";
    }
    pwrite(response,workbook,filename);
  }
  public void write(httpservletresponse response, list<list<string>> lists,string filename){
    if(stringutils.isempty(filename)){
      filename = uuid.randomuuid().tostring()+".xls";
    }
    sxssfworkbook workbook = new sxssfworkbook(lists.size());
    sxssfsheet sheet = workbook.createsheet(filename.substring(0,filename.indexof(".xls")));
    integer rowindex = 0;
    row row = null;
    cell cell = null;
    for(list<string> rowdata: lists ){
      integer columnindex = 0;
      row = sheet.createrow(rowindex++);
      for(string columnval:rowdata){
        cell = row.createcell(columnindex++);
        cell.setcellvalue(columnval);
      }
    }
    pwrite(response,workbook,filename);
  }
  private void pwrite(httpservletresponse response,workbook workbook,string filename){
    response.setcharacterencoding("utf-8");
    response.setcontenttype("application/vnd.ms-excel;charset=utf-8");
    try {
      response.addheader("content-disposition", "attachment; filename="+new string(filename.getbytes("utf-8"),"iso8859-1"));
    } catch (unsupportedencodingexception e) {
      e.printstacktrace();
      filename= uuid.randomuuid().tostring()+".xls";
      response.addheader("content-disposition", "attachment; filename="+filename);
    }
    try {
      workbook.write(response.getoutputstream());
    } catch (ioexception e) {
      e.printstacktrace();
    }
  }
}

有了这个工具类就可以实现excel导出了,代码不难,这里就不多解释了。

在springboot项目中编写一个导出excel的controller,并继承上面的exportabstractutil,给出一个接口用作测试:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package com.csdn.myboot.controller;
import com.csdn.myboot.utils.exportabstractutil;
import org.springframework.stereotype.controller;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.responsebody;
import javax.servlet.http.httpservletresponse;
import java.util.arraylist;
import java.util.arrays;
import java.util.list;
/**
 * @author haozz
 * @date 2018/6/6 10:14
 * @description
 **/
@controller
@requestmapping(value = "/index")
public class helloctrl extends exportabstractutil{
  @requestmapping(value = "/testexceloutput")
  @responsebody
  public void testexceloutput(httpservletresponse response){
    //拼接数据start
    list<list<string>> lists = new arraylist<list<string>>();
    string rows[] = {"year","month","day"};
    list<string> rowstitle = arrays.aslist(rows);
    lists.add(rowstitle);
    for(int i = 0; i<=9;i++){
      string [] rowss = {"1","2","3"};
      list<string> rowsslist = arrays.aslist(rowss);
      lists.add(rowsslist);
    }
    //拼接数据end
    write(response,lists,"导出excel.xls");
  }
}

浏览器输入链接:

http://localhost:8099/index/testexceloutput

即可自动下载测试数据组成的excel:

Java中实现简单的Excel导出

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对服务器之家的支持。如果你想了解更多相关内容请查看下面相关链接

原文链接:https://blog.csdn.net/hz_940611/article/details/80590488

延伸 · 阅读

精彩推荐