Silverlight程序之:简单的Excel报表导出方法
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Silverlight程序之:简单的Excel报表导出方法相关的知识,希望对你有一定的参考价值。
Silverlight程序之:简单的Excel报表导出方法
概述
介绍一种简单的Excel报表导出方法。
页面效果
导出效果:
首先我们创建一个Excel表格,将我们的基本信息格式都设置好,如下图所示:
将创建好的Excel表格另存为网页。
打开VS将创建好的报表模版网页文件拖到Web项目新建的Report文件夹
修改其后缀名为aspx
打开文件
添加头部代码
<%@ Page Language="C#" ContentType="application/vnd.ms-excel" %>
<%@ Import Namespace="MMISUI.Dao.Report" %>
<%@ Import Namespace="MMISUI.Models.Report" %>
<%@ Import Namespace="MMISUI.Dao.Report" %>
<%@ Import Namespace="MMISUI.Models.Report" %>
找到有汉字的表头<tr>
修改并添加代码
在里面可以获取数据集然后循环绑定到单元格中。形成一张表格。
<tr class=xl9719632 height=25 style=‘mso-height-source:userset;height:18.75pt‘>
<td height=25 class=xl8919632 style=‘height:18.75pt‘>物料编码</td>
<td class=xl8919632>物料名称</td>
<td class=xl8919632>规格型号</td>
<td class=xl8919632>计量单位</td>
<td class=xl8919632>物料类型</td>
<td class=xl8919632>年度</td>
<td class=xl8919632>时间</td>
<td class=xl9019632>数量</td>
<td class=xl9119632>金额</td>
<td class=xl9019632>数量</td>
<td class=xl9119632>金额</td>
<td class=xl9019632>数量</td>
<td class=xl9119632>金额</td>
</tr>
<%
string s = Request.QueryString["username"];
string s1 = Request.QueryString["email"];
decimal StockLedger_BAmount = 0,
StockLedger_BMoney = 0,
StockLedger_IAmount = 0,
StockLedger_IMoney = 0,
StockLedger_OAmount = 0,
StockLedger_OMoney = 0;
var dao = "SupplyDetailReportDao".Instance<ISupplyDetailReportDao>();
var list= dao.GetMaterialDispatchList();
for (int i = 0; i < list.Count; i++)
{
var mat = list[i] as MaterialDispatchReport;
if (mat==null)continue;
StockLedger_BAmount += mat.StockLedger_BAmount;
StockLedger_BMoney += mat.StockLedger_BMoney;
StockLedger_IAmount += mat.StockLedger_IAmount;
StockLedger_IMoney += mat.StockLedger_IMoney;
StockLedger_OAmount += mat.StockLedger_OAmount;
StockLedger_OMoney += mat.StockLedger_OMoney;
%>
<tr class=xl7019632 height=17 style=‘mso-height-source:userset;height:12.75pt‘>
<td height=17 class=xl7119632 style=‘height:12.75pt‘><%=i %></td>
<td class=xl7219632 width=129 style=‘width:97pt‘><%=mat.Mat_Code%></td>
<td class=xl7319632><%=mat.Mat_Name%></td>
<td class=xl7319632><%=mat.Mat_Model%></td>
<td class=xl7319632><%=mat.Units_Name%></td>
<td class=xl7319632><%=mat.MaterielCls_Name%></td>
<td class=xl7319632><%=mat.StockLedger_Year%></td>
<td class=xl7319632><%=mat.StockLedger_Period%></td>
<td class=xl7419632><%=mat.StockLedger_BAmount%></td>
<td class=xl7519632><%=mat.StockLedger_BMoney%></td>
<td class=xl7619632><%=mat.StockLedger_IAmount%></td>
<td class=xl7519632><%=mat.StockLedger_IMoney%></td>
<td class=xl7619632><%=mat.StockLedger_OAmount%></td>
<td class=xl7519632><%=mat.StockLedger_OMoney%></td>
</tr>
<%
}%>
<tr class=xl7719632 height=24 style=‘mso-height-source:userset;height:18.0pt‘>
<td height=24 class=xl7819632 style=‘height:18.0pt‘>合计:</td>
<td class=xl7919632 width=129 style=‘width:97pt‘> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8119632><%=StockLedger_BAmount%></td>
<td class=xl8219632><%=StockLedger_BMoney%></td>
<td class=xl8319632><%=StockLedger_IAmount%></td>
<td class=xl8219632><%=StockLedger_IMoney%></td>
<td class=xl8319632><%=StockLedger_OAmount%></td>
<td class=xl8219632><%=StockLedger_OMoney%></td>
</tr>
<td height=25 class=xl8919632 style=‘height:18.75pt‘>物料编码</td>
<td class=xl8919632>物料名称</td>
<td class=xl8919632>规格型号</td>
<td class=xl8919632>计量单位</td>
<td class=xl8919632>物料类型</td>
<td class=xl8919632>年度</td>
<td class=xl8919632>时间</td>
<td class=xl9019632>数量</td>
<td class=xl9119632>金额</td>
<td class=xl9019632>数量</td>
<td class=xl9119632>金额</td>
<td class=xl9019632>数量</td>
<td class=xl9119632>金额</td>
</tr>
<%
string s = Request.QueryString["username"];
string s1 = Request.QueryString["email"];
decimal StockLedger_BAmount = 0,
StockLedger_BMoney = 0,
StockLedger_IAmount = 0,
StockLedger_IMoney = 0,
StockLedger_OAmount = 0,
StockLedger_OMoney = 0;
var dao = "SupplyDetailReportDao".Instance<ISupplyDetailReportDao>();
var list= dao.GetMaterialDispatchList();
for (int i = 0; i < list.Count; i++)
{
var mat = list[i] as MaterialDispatchReport;
if (mat==null)continue;
StockLedger_BAmount += mat.StockLedger_BAmount;
StockLedger_BMoney += mat.StockLedger_BMoney;
StockLedger_IAmount += mat.StockLedger_IAmount;
StockLedger_IMoney += mat.StockLedger_IMoney;
StockLedger_OAmount += mat.StockLedger_OAmount;
StockLedger_OMoney += mat.StockLedger_OMoney;
%>
<tr class=xl7019632 height=17 style=‘mso-height-source:userset;height:12.75pt‘>
<td height=17 class=xl7119632 style=‘height:12.75pt‘><%=i %></td>
<td class=xl7219632 width=129 style=‘width:97pt‘><%=mat.Mat_Code%></td>
<td class=xl7319632><%=mat.Mat_Name%></td>
<td class=xl7319632><%=mat.Mat_Model%></td>
<td class=xl7319632><%=mat.Units_Name%></td>
<td class=xl7319632><%=mat.MaterielCls_Name%></td>
<td class=xl7319632><%=mat.StockLedger_Year%></td>
<td class=xl7319632><%=mat.StockLedger_Period%></td>
<td class=xl7419632><%=mat.StockLedger_BAmount%></td>
<td class=xl7519632><%=mat.StockLedger_BMoney%></td>
<td class=xl7619632><%=mat.StockLedger_IAmount%></td>
<td class=xl7519632><%=mat.StockLedger_IMoney%></td>
<td class=xl7619632><%=mat.StockLedger_OAmount%></td>
<td class=xl7519632><%=mat.StockLedger_OMoney%></td>
</tr>
<%
}%>
<tr class=xl7719632 height=24 style=‘mso-height-source:userset;height:18.0pt‘>
<td height=24 class=xl7819632 style=‘height:18.0pt‘>合计:</td>
<td class=xl7919632 width=129 style=‘width:97pt‘> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8019632> </td>
<td class=xl8119632><%=StockLedger_BAmount%></td>
<td class=xl8219632><%=StockLedger_BMoney%></td>
<td class=xl8319632><%=StockLedger_IAmount%></td>
<td class=xl8219632><%=StockLedger_IMoney%></td>
<td class=xl8319632><%=StockLedger_OAmount%></td>
<td class=xl8219632><%=StockLedger_OMoney%></td>
</tr>
删除多余的表格。。
在Silverlight程序里面调用
var uri = new Uri(App.Current.Host.Source, "../Report/物料收发存报表.aspx?username={0}&email={1}");
htmlPage.PopupWindow(uri, "_blank", new HtmlPopupWindowOptions());
htmlPage.PopupWindow(uri, "_blank", new HtmlPopupWindowOptions());
运行程序,点击导出按钮,提示你打开 保存 取消。
大功告成!!!
作者:记忆逝去的青春
出处:http://www.cnblogs.com/lukun/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过http://www.cnblogs.com/lukun/ 联系我,非常感谢。
以上是关于Silverlight程序之:简单的Excel报表导出方法的主要内容,如果未能解决你的问题,请参考以下文章
System.ServiceModel.CommunicationException:Silverlight 应用程序通过 excel 服务读取 excel 数据