好得很程序员自学网

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

java导出包含多个sheet的Excel代码示例

本文实例为大家分享了java导出包含多个sheet的excel的具体代码,供大家参考,具体内容如下

要导出多个sheet,关键就是excel导出的时间设定,在执行导出文件之前,创建多个工作表

?

1

hssfsheet sheet = workbook.createsheet(sheettitle);

这样每创建一个工作表,便会生成一个新的sheet表,在最后导出excel的时候一次性导出。

示例:
java类:

?

CopyRight:2016-2025好得很程序员自学网 备案ICP:湘ICP备09009000号-16 http://www.haodehen.cn
本站资讯不构成任何建议,仅限于个人分享,参考须谨慎!
本网站对有关资料所引致的错误、不确或遗漏,概不负任何法律责任。
本网站刊载的所有内容(包括但不仅限文字、图片、LOGO、音频、视频、软件、程序等)版权归原作者所有。任何单位或个人认为本网站中的内容可能涉嫌侵犯其知识产权或存在不实内容时,请及时通知本站,予以删除。

网站内容来源于网络分享,如有侵权发邮箱到:kenbest@126.com,收到邮件我们会即时下线处理。
网站框架支持:HDHCMS   51LA统计 百度统计
Copyright © 2018-2025 「好得很程序员自学网
[ SiteMap ]

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

try {

   hssfworkbook workbook = new hssfworkbook();

   outputstream out = response.getoutputstream();

   for ( int j= 0 ;j<n;j++){

     baseresult<list<t>> teasallist = service.select(teasal);

     //接下来循环list放到excel表中

     if (teasallist.issuccess()&&teasallist.getresult().size()> 0 ){

       //文件标题

       simpledateformat formatter1 = new simpledateformat( "yyyy-mm-dd" );

       string nowdate = formatter1.format( new date());

       string java" id="highlighter_950979">

?

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

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

public class exportexceldownfee {

 

   //导出表的列名

   private string[] rowname ;

   //导出表的小标题

   private string oneheaders;

   //导出表的日期

   private string dateheaders;

   //sheet表表名

   private string sheettitle;

 

   private list<object[]> datalist = new arraylist<object[]>();

 

   httpservletresponse response;

   

   //构造方法2,传入要导出的数据

   public exportexceldownfee( string sheettitle, string oneheaders, string dateheaders, string[] rowname,list<object[]> datalist){

     this .datalist = datalist;

     this .oneheaders = oneheaders;

     this .dateheaders = dateheaders;

     this .rowname = rowname;

     this .sheettitle = sheettitle;

   }

   

   /*

    * 导出数据

    * */

   public void export(hssfworkbook workbook,outputstream out) throws exception{

     try{

       hssfsheet sheet = workbook.createsheet(sheettitle);         // 创建工作表

 

       hssfcellstyle columntopstyle = this.getcolumntopstyle(workbook);//获取列头样式对象

       hssfcellstyle style = this.getstyle(workbook);         //单元格样式对象

 

       //第一行

       hssfrow rowfirstname = sheet.createrow(0);

       hssfcell onecellrowname = rowfirstname.createcell(0);        //创建列头对应个数的单元格

       onecellrowname.setcelltype(hssfcell.cell_type_string);       //设置列头单元格的数据类型

       hssfrichtextstring onetext = new hssfrichtextstring(oneheaders);

       onecellrowname.setcellvalue(onetext);                 //设置列头单元格的值

       //合并单元格cellrangeaddress构造参数依次表示起始行,截至行,起始列, 截至列 

       sheet.addmergedregion(new cellrangeaddress(0,0,0,3)); 

       onecellrowname.setcellstyle(columntopstyle);            //设置列头单元格样式

       

       //第二行

       hssfrow rowdatename = sheet.createrow(1);

       hssfcell datecellrowname = rowdatename.createcell(3);

       datecellrowname.setcellvalue(dateheaders);

       datecellrowname.setcellstyle(columntopstyle); 

       

       // 定义所需列数

       int columnnum = rowname.length;

       hssfrow rowrowname = sheet.createrow(2);        // 在索引2的位置创建行(最顶端的行开始的第二行)

 

       // 将列头设置到sheet的单元格中

       for(int n=0;n<columnnum;n++){

         hssfcell cellrowname = rowrowname.createcell(n);        //创建列头对应个数的单元格

         cellrowname.setcelltype(hssfcell.cell_type_string);       //设置列头单元格的数据类型

         hssfrichtextstring text = new hssfrichtextstring(rowname[n]);

         cellrowname.setcellvalue(text);                 //设置列头单元格的值

         cellrowname.setcellstyle(style);            //设置列头单元格样式

       }

 

       //将查询出的数据设置到sheet对应的单元格中

       for(int i=0;i<datalist.size();i++){

 

         object[] obj = datalist.get(i);//遍历每个对象

         hssfrow row = sheet.createrow(i+3);//创建所需的行数(从第二行开始写数据)

 

         for(int j=0; j<obj.length; j++){

           hssfcell cell = null;  //设置单元格的数据类型

           if(j == 0){

             cell = row.createcell(j,hssfcell.cell_type_numeric);

             cell.setcellvalue(i+1); 

           }else{

             cell = row.createcell(j,hssfcell.cell_type_string);

             if(!"".equals(obj[j]) && obj[j] != null){

               cell.setcellvalue(obj[j].tostring());            //设置单元格的值

             }

           }

           cell.setcellstyle(style);                  //设置单元格样式

         }

       }

       

       //让列宽随着导出的列长自动适应

       for (int colnum = 0; colnum < columnnum; colnum++) {

         int columnwidth = sheet.getcolumnwidth(colnum) / 256;

         for (int rownum = 0; rownum < sheet.getlastrownum(); rownum++) {

           hssfrow currentrow;

           //当前行未被使用过

           if (sheet.getrow(rownum) == null) {

             currentrow = sheet.createrow(rownum);

           } else {

             currentrow = sheet.getrow(rownum);

           }

           if (currentrow.getcell(colnum) != null) {

             hssfcell currentcell = currentrow.getcell(colnum);

             if (currentcell.getcelltype() == hssfcell.cell_type_string) {

               int length = 0;

               try {

                 length = currentcell.getstringcellvalue().getbytes().length;

               } catch (exception e) {

                 e.printstacktrace();

               }

               if (columnwidth < length) {

                 columnwidth = length;

               }

             }

           }

 

         }

         if(colnum == 0){

           sheet.setcolumnwidth(colnum, (columnwidth-2) * 256);

         }else{

           sheet.setcolumnwidth(colnum, (columnwidth+4) * 256);

         }

       }

 

     }catch(exception e){

       e.printstacktrace();

     }

   }

 

   /*

    * 列头单元格样式

    */  

   public hssfcellstyle getcolumntopstyle(hssfworkbook workbook) {

 

      // 设置字体

      hssffont font = workbook.createfont();

      //设置字体大小

      font.setfontheightinpoints((short)11);

      //字体加粗

      //font.setboldweight(hssffont.boldweight_bold);

      //设置字体名字 

      font.setfontname("宋体");

      //设置样式; 

      hssfcellstyle style = workbook.createcellstyle();

      //在样式用应用设置的字体; 

      style.setfont(font);

      //设置自动换行; 

      style.setwraptext(false);

      //设置水平对齐的样式为居中对齐; 

      style.setalignment(hssfcellstyle.align_center);

      //设置垂直对齐的样式为居中对齐; 

      style.setverticalalignment(hssfcellstyle.vertical_center);

 

      return style;

 

   }

 

   /* 

    * 列数据信息单元格样式

    */

   public hssfcellstyle getstyle(hssfworkbook workbook) {

      // 设置字体

      hssffont font = workbook.createfont();

      //设置字体名字 

      font.setfontname( "宋体" );

      //设置样式; 

      hssfcellstyle style = workbook.createcellstyle();

      //设置底边框; 

      style.setborderbottom(hssfcellstyle.border_thin);

      //设置底边框颜色; 

      style.setbottombordercolor(hssfcolor.black.index);

      //设置左边框;  

      style.setborderleft(hssfcellstyle.border_thin);

      //设置左边框颜色; 

      style.setleftbordercolor(hssfcolor.black.index);

      //设置右边框; 

      style.setborderright(hssfcellstyle.border_thin);

      //设置右边框颜色; 

      style.setrightbordercolor(hssfcolor.black.index);

      //设置顶边框; 

      style.setbordertop(hssfcellstyle.border_thin);

      //设置顶边框颜色; 

      style.settopbordercolor(hssfcolor.black.index);

      //在样式用应用设置的字体; 

      style.setfont(font);

      //设置自动换行; 

      style.setwraptext( false );

      //设置水平对齐的样式为居中对齐; 

      style.setalignment(hssfcellstyle.align_center);

      //设置垂直对齐的样式为居中对齐; 

      style.setverticalalignment(hssfcellstyle.vertical_center);

 

      return style;

 

    }

}

以上所述是小编给大家介绍的java导出包含多个sheet的excel代码示例详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!

原文链接:https://blog.csdn.net/linyuxb123/article/details/85987682

查看更多关于java导出包含多个sheet的Excel代码示例的详细内容...

  阅读:36次