csv出力

/** 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; " />

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值