/** Application/csv */
private static final String CCONTENTTYPE = "Application/csv";
/** Content-Disposition */
private static final String CCONTENTDISPOSITION = "Content-Disposition";
/** attachment;filename= */
private static final String CATTACHMENTFILENAME = "attachment;filename=";
/** csvファイル */
private static final String CCSVFILENAME = "VesselMasterCSV.csv";
/** Shift-JIS */
private static final String CSHIFTJIS = "Shift-JIS";
/** empty */
private static final String CEMPTY = "";
/** ヘッダの設定 */
private static final String CCSVHEADER = "区分,No,ロケーション名(Jp),ロケーション名(English),ロケーション名(Chinese),A地点緯度,A地点経度";
/** /r/n */
private static final String CSEPERATOR = "/r/n";
/** CCOMMA */
private static final String CCOMMA = ",";
/**
* CSV出力処理
*
* @return 処理フラグ
* @throws SystemException
* システム異常の場合、SystemExceptionをスローします。
*/
public String csvOutput() throws SystemException {
try {
List<CoastalPositionBean> tmpList = new ArrayList<CoastalPositionBean>();
tmpList = coastalPositionDao.searchAll();
// CSVファイルを出力する
outPutCsvFile(tmpList);
} catch (SystemException se) {
throw se;
}
return RESULT_SUCCESS;
}
/**
* CSVファイルを出力する<BR>
*
* @param contents
* csvファイル内容
* @return なし
* @throws SystemException
* システム異常の場合、SystemExceptionをスローします。
*/
private void outPutCsvFile(List<CoastalPositionBean> contents)
throws SystemException {
HttpServletResponse response = ServletActionContext.getResponse();
try {
response.setContentType(CCONTENTTYPE);
response.setHeader(CCONTENTDISPOSITION, CATTACHMENTFILENAME
+ CCSVFILENAME);
response.setCharacterEncoding(CSHIFTJIS);
StringBuffer cont = new StringBuffer(CEMPTY);
// ヘッダの設定
cont.append(CCSVHEADER);
cont.append(CSEPERATOR);
// データの設定
for (int i = 0; i < contents.size(); i++) {
CoastalPositionBean CoastalPositionBean = new CoastalPositionBean();
CoastalPositionBean = contents.get(i);
cont.append(CoastalPositionBean.getAreaRank());
cont.append(CCOMMA + CoastalPositionBean.getLocationNo());
cont.append(CCOMMA + CoastalPositionBean.getFullName());
cont.append(CCOMMA + CoastalPositionBean.getFullNameEn());
cont.append(CCOMMA + CoastalPositionBean.getFullNameCn());
cont.append(CCOMMA + CoastalPositionBean.getLatitudeAPt());
cont.append(CCOMMA + CoastalPositionBean.getLongitudeAPt());
if (i == contents.size() - 1)
break;
cont.append(CSEPERATOR);
}
response.getWriter().println(cont.toString());
response.getWriter().close();
} catch (Exception e) {
try {
response.getWriter().close();
} catch (IOException e2) {
}
throw new SystemException(e.getMessage());
}
}
//jsp页面
<s:submit name="goCsvOutput" value="CSV出力" method="csvOutput"
disabled="%{disabledBtnCsvOutput}"
style="background-color:#FFFF99; " />