Castle-从页面上导出数据到Excel上

此用例在Kingdom.scm--Controllers\ProductAdminController.cs文件中


从页面上导出数据到Excel上,要先从数据库中取出数据才导出
页面上会自动出对话框来提示保存文件的位置.
public void ExportExcel(string categoryId)
{
 int pageCount,recordCount;
 //获得查询的数据
 IList products = ProductService.Find(SiteContext.Current.User.Name,categoryId,string.Empty,
    string.Empty,1,65000.out pageCount,out recordCount);
 //用于表的标题输出
 StringBuilder columnHeaderText=new StringBuilder();
 //用于表的数据输出
 StringBuilder columnText=new StringBuilder();
 //写入表的标题,\t:TAB鍵跳到下一格子上,\n:ENTRY换行符跳到下一行
 columnHeaderText.Append("产品编码\t");
 columnHeaderText.Append("产品中文名\t");
 columnHeaderText.Append("型号\t");
 columnHeaderText.Append("成本单价\t");
 columnHeaderText.Append("零售单价\t");
 columnHeaderText.Append("详细描述\t");
 columnHeaderText.Append("计量单位\n");
 //写入表的数据Product是一个类
 foreach(Product p in products)
 {
  columnText.Append(string.Format("{0}\t",p.SerialNumber));
  columnText.Append(string.Format("{0}\t",p.ProductName));
  columnText.Append(string.Format("{0}\t",p.ModelNumber));
  columnText.Append(string.Format("{0}\t",p.UnitPrice));
  columnText.Append(string.Format("{0}\t",p.SalePrice));
  columnText.Append(string.Format("{0}\t",p.Details));
  columnText.Append(string.Format("{0}\n",p.MeasurementUnit));
 }
 //以流的方式输出
 this.HttpContext.Current.Response.Clear();
 //设置输出的编码方式
 this.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("GB2312");
 //设置输出的类型,输出的类型为EXCEL样式
 this.HttpContext.Current.Response.ContentType="application/vnd.ms-excel";
 //以附件的方式来导出attachment;filename=products{0}.xls--xls是Excele文件,doc--是WORD文档,GetDateString():以当前的时间为文件名.
 this.HttpContext.Current.Response.AddHeader("Content-Disponsition",string.Format("attachment;filename=products{0}.xls",GetDateString()));
 //以输出流来保存文件.xls
 this.HttpContext.Current.Response.Write(columnHeaderText.ToString()+columnText.ToString());
 //
 this.HttpCOntext.Current.Response.End();
}
//获得文件的名,用的是当前的时间
private string GetDateString()
{
 return string.Format("{0}{1}{2}{3}{4}{5}",new object[]{DateTime.Now.Year,DateTime.Now.Month,DateTime.Now.Day
    ,DateTime.Now.Hour,DateTime.Now.Minute,DateTime.Now.Second});
}
==========================================================================================================================
<a href="ExportExcel.page" onclick="javascript:this.href=this.href">导出</a>
当有脚本连接时,就不会跳出一个页面窗口,直接到一个提示对话框。
<a href="ExportExcel.page" >导出</a>
无脚本时,会跳出一个页面窗口后,才到提示对话框。
==========================================================================================================================
//当有这样时就会在不跳出Export页面就完成数据的导出。
<form action="Export.page">
 <input type="submit" value="submit"/>
</form>

---------------------------Export.vm页面----------------------------------------
<table>
 <tr>
  <td>编号</td>
  <td>姓名</td>
 </tr>
 #foreach($row in $dt.rows)
 <tr>
  <td>$row.id</td>
  <td>$row.name</td>
 </tr>
 #end
</table>

public void Export()
  {
   DataTable dt = new DataTable();
   dt.Columns.Add("id",typeof(string));
   dt.Columns.Add("name",typeof(string));
   for(int i=0;i<4;i++)
   {
    DataRow row = dt.NewRow();
    row["id"]   = i.ToString();
    row["name"] = "chen"+i.ToString();
    dt.Rows.Add(row);
   }
   this.PropertyBag.Add("dt",dt);
   System.Web.HttpContext.Current.Response.Clear();
   System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
   System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
   System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", string.Format("attachment;filename=Member{0}.xls", "GetDateString"));
   //在Export页面上显示。
   this.RenderView("Export");
  }

------------------------------------------------------------------------------------------------------
http://biz2.myking.cn:90/cards/member/memberlist.page
这个页面上的导入和导出的例子,可以参考。
 <table border="1">
  <tr>
   <th>客户姓名</th>
   <th>卡号</td>
   <th>手机号码</th>
   <th>开卡日期</th>   
  </tr>
  #foreach($m in $members) 
   <tr>
    <td style="vnd.ms-excel.numberformat:@">$!{m.Membername}</td>
    //在页面上导出时,要加这个样式,是因为Excel中当数字大于15个时多的就用0来填充
    //加样式就可以显示出16位的卡号来
    <td style="vnd.ms-excel.numberformat:@">$!{m.Cardid}</td>
    <td>$!{m.Moblie}</td>
    <td>#if($!m.Isusedate.ToShortDateString() == "0001-1-1") "-------" #else $!m.Isusedate.ToShortDateString() #end</td>
   </tr>
  #end
 </table>
-------------------------------------------------------------------------------------
//这样写比较标准,
<html>
 <head>
  <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  //保证输出的是中文格式。
  //th:显示输出是会以粗体字  td:显示输出是普通的字体。
 </head>
 <body>
  <table id="datalist" border="1">
   <thead>
    <tr align="left">
     <th width="135">会员卡号</th>
     <th width="135">会员姓名</th>
     <th width="135">手机号码</th>
     <th width="135">次数</th>
     <th width="135">总金额</th>
     <th width="135">总积分</th>
     <th width="135">关注会员</th>
    </tr>
   </thead>
   #foreach ($memberInfo in $memberInfoDt.Rows)
   <tr align="left">
    <td style="vnd.ms-excel.numberformat:@">$memberInfo.CardId.ToString()</td>
    //保证当卡号长过15时依然可以显示完整的卡号。
    <td style="vnd.ms-excel.numberformat:@">$!{memberInfo.CardName}</td>
    <td style="vnd.ms-excel.numberformat:@">$!{memberInfo.PhoneNumber}</td>
    <td>$!{memberInfo.ConsumeCount}</td>
    <td>$!{memberInfo.ConsumeMoney}</td>
    <td>$!{memberInfo.PointTotal}</td>
    <td>#if($memberInfo.IsVip == 1) 是 #else 否 #end</td>
   </tr>
   #end
  </table>
 </body>
</html>

posted on 2009-05-06 21:57  关寒融冰  阅读(826)  评论(2编辑  收藏  举报

鲁ICP备07018066号-1