国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java使用poi導出Excel的方法

java使用poi導出Excel的方法

2021-05-26 11:18輸入密碼 Java教程

這篇文章主要為大家詳細介紹了java使用poi導出Excel的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了java使用poi導出excel的具體代碼,供大家參考,具體內容如下

?
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package hyss.util.common;
 
import java.io.ioexception;
import java.io.outputstream;
import java.net.urlencoder;
import java.util.arraylist;
import java.util.list;
 
import org.apache.poi.hssf.usermodel.hssfcell;
import org.apache.poi.hssf.usermodel.hssfcellstyle;
import org.apache.poi.hssf.usermodel.hssffont;
import org.apache.poi.hssf.usermodel.hssfrichtextstring;
import org.apache.poi.hssf.usermodel.hssfrow;
import org.apache.poi.hssf.usermodel.hssfsheet;
import org.apache.poi.hssf.usermodel.hssfworkbook;
import org.apache.poi.hssf.util.hssfcolor;
import org.apache.poi.ss.util.cellrangeaddress;
 
/**
 * excel導出工具類
 * @author ts
 */
 
public class exportexcelutil {
 //顯示的導出表的標題
 private string title;
 //導出表的列名
 private string[] rowname ;
 
 private list<object[]> datalist = new arraylist<object[]>();
 
// httpservletresponse response;
 
 //構造方法,傳入要導出的數據
 public exportexcelutil(string title,string[] rowname,list<object[]> datalist){
  this.datalist = datalist;
  this.rowname = rowname;
  this.title = title;
 }
   
 /*
  * 導出數據
  * */
 public void export() throws exception{
  try{
   hssfworkbook workbook = new hssfworkbook();      // 創建工作簿對象
   hssfsheet sheet = workbook.createsheet(title);      // 創建工作表
   
   // 產生表格標題行
   hssfrow rowm = sheet.createrow(0);
   hssfcell celltiltle = rowm.createcell(0);
   rowm.setheightinpoints(25);           //設置標題行默認行高
   
   //sheet樣式定義【getcolumntopstyle()/getstyle()均為自定義方法 - 在下面 - 可擴展】
   hssfcellstyle columntitlestyle = this.gettitletopstyle(workbook);//獲取標題行樣式
   hssfcellstyle columntopstyle = this.getcolumntopstyle(workbook); //獲取列頭樣式對象
   hssfcellstyle style = this.getstyle(workbook);     //單元格樣式對象
   
   sheet.addmergedregion(new cellrangeaddress(0, 1, 0, (rowname.length-1)));
   celltiltle.setcellstyle(columntitlestyle);
   celltiltle.setcellvalue(title);
   
   // 定義所需列數
   int columnnum = rowname.length;
   hssfrow rowrowname = sheet.createrow(2);    // 在索引2的位置創建行(最頂端的行開始的第二行)
   rowrowname.setheightinpoints(25);       //將列頭設置默認行高
   // 將列頭設置到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(columntopstyle);      //設置列頭單元格樣式
   }
   
   //將查詢出的數據設置到sheet對應的單元格中
   for(int i=0;i<datalist.size();i++){
    
    object[] obj = datalist.get(i);//遍歷每個對象
    hssfrow row = sheet.createrow(i+3);//創建所需的行數
    row.setheightinpoints(20); //將創建出的行設置默認行高
    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 = currentcell.getstringcellvalue().getbytes().length;
       if (columnwidth < length) {
        columnwidth = length;
       }
      }
     }
    }
    if(colnum == 0){
     sheet.setcolumnwidth(colnum, (columnwidth-2) * 256);
     
    }else{
     sheet.setcolumnwidth(colnum, (columnwidth+4) * 256);
    }
   }
   
   if(workbook !=null){
    try
    {
     string filename = title + datetime.getsystemdatetime("yyyy-mm-dd") + ".xls";
             //因 response已經封裝成工具類所以下面這段代碼注釋掉
//     response =servletactioncontext.getresponse();
//     response.setcontenttype("application/octet-stream");
//     response.setheader("content-disposition", headstr);
//     outputstream out = response.getoutputstream();
     
     //解決中文亂碼
     struts2util.getresponse().setcharacterencoding("utf-8");
     //解決中文亂碼
     struts2util.getresponse().setheader("content-disposition", "attachment;filename*=utf-8''" + urlencoder.encode(filename,"utf-8"));
     //文件下載
     struts2util.getresponse().setcontenttype("application/octet-stream");
     outputstream out = struts2util.getresponse().getoutputstream();
     workbook.write(out);
    }
    catch (ioexception e)
    {
     e.printstacktrace();
    }
   }
 
  }catch(exception e){
   e.printstacktrace();
  }
  
 }
 
 /*
  * 設置標題樣式
  */
  public hssfcellstyle gettitletopstyle(hssfworkbook workbook) {
   
   // 設置字體
   hssffont font = workbook.createfont();
   //設置字體大小
   font.setfontheightinpoints((short)24);
   //字體加粗
   font.setboldweight(hssffont.boldweight_bold);
   //設置字體名字
   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;
   
  }
 
 
 /*
  * 列頭單元格樣式
  */
  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.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;
   
  }
  
  /*
  * 列數據信息單元格樣式
  */
  public hssfcellstyle getstyle(hssfworkbook workbook) {
   // 設置字體
   hssffont font = workbook.createfont();
   //設置字體大小
   //font.setfontheightinpoints((short)10);
   //字體加粗
   //font.setboldweight(hssffont.boldweight_bold);
   //設置字體名字
   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;
  
  }
 
}

實現方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/*
* 實現方法
*/
public void exportexcel() throws exception{
  string title = "測試";
  string[] rowsname = new string[]{"序號","列頭1","列頭2","列頭3","列頭4","列頭5"};
  list<object[]> datalist = new arraylist<object[]>();
  object[] objs = null;
  for (int i = 0; i < 10; i++) {
   objs = new object[rowsname.length];
   objs[0] = i;
   objs[1] = "測試1";
   objs[2] = "測試2";
   objs[3] = "測試3";
   objs[4] = "測試4";
   objs[5] = "測試5";
   datalist.add(objs);
  }
  exportexcelutil ex = new exportexcelutil(title, rowsname, datalist);
  ex.export();
  
 }

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://www.cnblogs.com/jiaobaobao/archive/2018/08/16/9485905.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产乱码精品一区二区三区av | 亚洲第一视频 | 97在线观看 | 日韩精品一区二区三区在线播放 | 成人精品久久久 | 日韩一区精品 | 欧美亚洲高清 | 日本免费在线视频 | 亚洲天堂久久 | 精品一区二区久久久久黄大片 | 国产精品久久久久久亚洲调教 | 国产精品毛片久久久久久久 | 久久久女女女女999久久 | 久久九九国产精品 | 国产人妖在线 | 日韩视频在线一区二区 | 亚洲国产成人av好男人在线观看 | 欧美精品亚洲精品 | 久久久精品网 | 久久久久久久久久久高潮 | 日韩精品av一区二区三区 | 午夜精品久久久久久久99黑人 | 国产成人精品一区二 | 高清av一区 | 亚洲欧美精品一区二区 | 亚洲精品成人在线 | 日韩精品小视频 | 精品一区二区不卡 | 中文字幕一区二区三区四区五区 | 欧美日韩一区二区三区免费视频 | 在线观看国产视频 | 欧美大片一区二区 | 日本一区二区在线视频 | 亚洲第1页| 日日摸夜夜添夜夜添精品视频 | 99热这里有| 亚洲天堂中文字幕 | 亚洲福利 | 亚洲伊人伊色伊影伊综合网 | 亚洲欧美一区二区三区四区 | 伊人在线|