随笔 - 97  文章 - 20  评论 - 94  阅读 - 22万

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   关寒融冰  阅读(833)  评论(2编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2009年5月 >
26 27 28 29 30 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6

鲁ICP备07018066号-1
点击右上角即可分享
微信分享提示