`
yyys8517750
  • 浏览: 140212 次
  • 性别: Icon_minigender_1
  • 来自: 岳阳
社区版块
存档分类
最新评论

csv文件打印

阅读更多

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForward;
import org.apache.struts.actions.DispatchAction;
import java.io.PrintWriter;
import javax.servlet.http.HttpSession;


public class CsvPrint extends DispatchAction {

public ActionForward exportCsv(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws Exception {

   HttpSession session = request.getSession();
   //抬头部分
   String data = "姓名,性别,年龄,电话,地址\n";
   //数据部分,可动态从数据库中获得
   data += "小一,男,14,034344343434,上海世纪大道\n";
   data += "微微,女,34,3432434334,香港测试\n";
   StringBuilder str = new StringBuilder();
   str.append(data);
   response.setContentType("text/csv");
   response.setCharacterEncoding("gb18030");
   response
     .setHeader("Content-Disposition",
       "attachment; filename=\""
         + new String("test.csv".getBytes("gb18030"),"iso8859-1") + "\"");
   PrintWriter out = response.getWriter();
   out.write(str.toString());
   out.flush();
   out.close();
   return null;
}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics