質問編集履歴

1

文言修正

2016/06/14 01:52

投稿

退会済みユーザー
test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,34 @@
1
- Excelのダウンロードを行った際以下のようなヘッダーをセットして
1
+ PoiでExcelのダウンロードを行っているのですが
2
2
 
3
+
4
+
5
+ 以下のようにサーブレット側、サーブレットから呼び出される関数を作ると、
6
+
7
+ ダウンロード処理完了後に自動でタブが閉じられません。
8
+
9
+
10
+
11
+ ```Java サーブレット側
12
+
13
+
14
+
15
+ protected void doDownload(HttpServletResponse response) {
16
+
17
+
18
+
19
+ response.setContentType("application/octet-stream");
20
+
21
+ String fileName = null;
22
+
3
- ```
23
+ try {
24
+
25
+ fileName = new String(getExcelFileName().getBytes("Windows-31J"), "ISO-8859-1");
26
+
27
+ } catch (UnsupportedEncodingException e1) {
28
+
29
+ e1.printStackTrace();
30
+
31
+ }
4
32
 
5
33
  response.setHeader("Content-Disposition", "attachments; filename=\"" + fileName + "\"");
6
34
 
@@ -8,92 +36,82 @@
8
36
 
9
37
  response.setHeader("Pragma", "public");
10
38
 
11
- ```
12
39
 
13
40
 
41
+ try {
14
42
 
15
- 以下のように、wb.outを関数内でコードを組むと、ダウンロード実行後に、タブが自動で閉じられません。
43
+ OutputStream out = new BufferedOutputStream(response.getOutputStream());
16
44
 
17
-
45
+ wb.write(out);
18
46
 
19
- ```Java
47
+ } catch (IOException e) {
20
48
 
21
- public static void writeXLSXFile() throws IOException {
49
+ e.printstactrace();
22
50
 
23
-
51
+ } finally {
24
52
 
25
- String excelFileName = "C:/Test.xlsx";//name of excel file
53
+ try {
26
54
 
55
+ wb.close();
27
56
 
57
+ } catch (IOException e) {
28
58
 
29
- String sheetName = "Sheet1";//name of sheet
30
-
31
-
32
-
33
- XSSFWorkbook wb = new XSSFWorkbook();
34
-
35
- XSSFSheet sheet = wb.createSheet(sheetName) ;
36
-
37
-
38
-
39
- //iterating r number of rows
40
-
41
- for (int r=0;r < 5; r++ )
42
-
43
- {
44
-
45
- XSSFRow row = sheet.createRow(r);
59
+ e.printstactrace();
46
-
47
-
48
-
49
- //iterating c number of columns
50
-
51
- for (int c=0;c < 5; c++ )
52
-
53
- {
54
-
55
- XSSFCell cell = row.createCell(c);
56
-
57
-
58
-
59
- cell.setCellValue("Cell "+r+" "+c);
60
60
 
61
61
  }
62
62
 
63
63
  }
64
64
 
65
-
66
-
67
- FileOutputStream fileOut = new FileOutputStream(excelFileName);
68
-
69
-
70
-
71
- //write this workbook to an Outputstream.
72
-
73
- wb.write(fileOut);
74
-
75
- fileOut.flush();
76
-
77
- fileOut.close();
78
-
79
65
  }
80
66
 
81
67
 
82
68
 
83
- public static void main(String[] args) throws IOException {
69
+ ```
84
70
 
85
-
71
+
86
72
 
87
- writeXLSXFile();
73
+ ```Java サーブレットから呼び出される関数
74
+
75
+ protected void doUpdateExcel(HttpServletRequest request, HttpServletResponse response)
76
+
77
+ throws ServletException, IOException {
88
78
 
89
79
 
90
80
 
81
+ --- エクセルを生成する処理 ---
82
+
83
+
84
+
85
+ doDownload(); // ここで上記のエクセルダウンロードを呼び出すと、タブが自動で閉じられない
86
+
87
+
88
+
91
- }
89
+ }
92
90
 
93
91
  ```
94
92
 
95
93
 
96
94
 
97
- servlet側でwb.outするとダウンロド後にタが自動閉じらるのですが
95
+ 一方関数を使わずに、関数の処理をそっくりそのまま、サーブレット実行す
98
96
 
97
+ タブは自動で閉じられます。
98
+
99
+
100
+
101
+
102
+
103
+ おそらく原因は、関数内で、wb.write(out)を行うと、この内部処理でreturnするような処理になっていると思うのですが、
104
+
105
+ 関数でそれを実行すると、まだ、サーブレット側の処理がのこっていることになり、
106
+
107
+ 一方、サーブレットでwb.write(out)とすれば、内部処理でreturn後、サーブレットがおわることになり、
108
+
109
+ タブが自動に閉じられるような挙動になっているのではと思っています。
110
+
111
+
112
+
113
+
114
+
115
+ 同じコードをサーブレット単位で書くのは面倒なので、
116
+
99
- 関数内でもタブ自動閉じられる方法はありでしょうか。
117
+ 関数内で実行したいです、なかよい方法はございせんでしょうか。