好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

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测试数据mons.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:

总结

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

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

查看更多关于Java中实现简单的Excel导出的详细内容...

  阅读:17次