全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:2458
推到 Plurk!
推到 Facebook!

JSP用setHeader輸出CSV會有空白?

缺席
b7307024
一般會員


發表:9
回覆:21
積分:6
註冊:2005-07-31

發送簡訊給我
#1 引用回覆 回覆 發表時間:2007-04-25 15:40:24 IP:220.135.xxx.xxx 訂閱
各位大大好!小弟之前有用FileWriter成功輸出csv,但後來想用response.setHeader來輸出
因為想要能夠使用另存新檔,儲存到自己想要的地方。
我使用的方法在輸出Excel是沒問題,但是輸出csv就會多出空格
請各位大大幫我看看吧,程式碼如下!

<textarea class="java" rows="10" cols="60" name="code"> <%@ page language="java" contentType="text/html;charset=MS950"%> <%@ include file="../include/import.jsp"%> <%@ include file="../include/func.jsp"%> <%@ include file="../common/consqlserv.jsp"%> <% String f_name = "表單查詢CSV報表.csv"; response.setContentType("application/msexcel;charset=MS950"); response.setHeader("Content-disposition","inline; filename=" new String( f_name.getBytes("MS950"), "ISO8859_1" )); response.setStatus(response.SC_OK); response.flushBuffer(); title = "文件編號,姓名,身分證(護照)號碼,出生年月日,航班,入境日期,居住縣市,居住鄉鎮市區,在臺聯絡地址"; out.println(title); while (rs.next()) { //抓取資料庫欄位資料 docid = rs.getInt("DocID"); name = rs.getString("FieldName1"); passport = rs.getString("FieldName3"); birth = rs.getDate("FieldName4"); schedul = rs.getString("FieldName6"); indate = rs.getDate("FieldName5"); add_tmp = rs.getString("FieldName7"); address = add_tmp.replace("郵遞區號", ""); //判斷縣市 if (address.indexOf("縣") != -1) { city = address.split("縣"); country = city[0] "縣"; if (country.length() >= 3) { country1 = country.substring(country.length() - 3,country.length()); } } else if (address.indexOf("市") != -1) { city = address.split("市"); country = city[0] "市"; if (country.length() >= 3) { country1 = country.substring(country.length() - 3,country.length()); } } else { country1 = ""; } //判斷鄉鎮里區 if (address.indexOf("鄉") != -1) { area1 = address.split("鄉"); area2 = area1[0] "鄉"; if (area2.length() >= 3) { area3 = area2.substring(area2.length() - 3, area2.length()); } } else if (address.indexOf("里") != -1) { area1 = address.split("里"); area2 = area1[0] "里"; if (area2.length() >= 3) { area3 = area2.substring(area2.length() - 3, area2.length()); } } else if (address.indexOf("區") != -1) { area1 = address.split("區"); area2 = area1[0] "區"; if (area2.length() >= 3) { area3 = area2.substring(area2.length() - 3, area2.length()); } } else { area3 = ""; } //輸出csv格式 full = docid "," name "," passport "," birth "," schedul "," indate "," country1 "," area3 "," address ; out.println(full); } rs.close(); %> </textarea>
編輯記錄
b7307024 重新編輯於 2007-04-25 15:44:30, 註解 無‧
b7307024 重新編輯於 2007-04-25 15:46:06, 註解 無‧
b7307024 重新編輯於 2007-04-25 15:52:58, 註解 無‧
b7307024
一般會員


發表:9
回覆:21
積分:6
註冊:2005-07-31

發送簡訊給我
#2 引用回覆 回覆 發表時間:2007-04-27 16:53:30 IP:220.135.xxx.xxx 訂閱
各位大大好!我解決了!
在所有的out.println的最前面
加上out.clear; 就可以了!
系統時間:2024-12-04 1:22:08
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!