关于导出到excel

21-Guns 2014-10-22 11:35:03
这是getdata_md.aspx.cs


protected void alldata_Click1(object sender, EventArgs e)//查询
{
string type = rep_type.Value;//类型
var con = condition.Value;//条件
var st_date = st_time.Value;//开始时间
var ed_date = ed_time.Value;//结束时间
StringBuilder html = new StringBuilder();
DataTable dt = new DataTable();
if (type == "1")
{
if (con == "1")
{
string sql_1 = "";
if (hos_no.Value != "37" && (fillid.Value != "151" || fillid.Value != "153"))//
{
sql_1 = @"select a.flag,a.hos_name, a.sort_id as a_sort_id,b.sort_id as b_sort_id,a.m_name,a.card_id,a.get_hiv_time,a.l_menstruation,
b.pregnancy,b.brithday,b.perinatal,b.u_medication,
b.pregnancy_time+'('+ltrim(b.pregnancy_week)+')' as pregnancy_time,
b.s_situation,b.HIV_medication,isnull(b.children_id,'/') as children_id
from tab_3_I a,tab_3_II b
where a.hos_no*=b.hos_no
and a.card_id*=b.card_id
and a.filldate between '" + st_date + "' and '" + ed_date + "' and a.hos_no='" + hos_no.Value + "' and a.fillid='" + fillid.Value + "' order by a.hos_no asc";
}
else
{
sql_1 = @"select a.flag,a.hos_name, a.sort_id as a_sort_id,b.sort_id as b_sort_id,a.m_name,a.card_id,a.get_hiv_time,a.l_menstruation,
b.pregnancy,b.brithday,b.perinatal,b.u_medication,
b.pregnancy_time+'('+ltrim(b.pregnancy_week)+')' as pregnancy_time,
b.s_situation,b.HIV_medication,isnull(b.children_id,'/') as children_id
from tab_3_I a,tab_3_II b
where a.hos_no*=b.hos_no
and a.card_id*=b.card_id
and a.filldate between '" + st_date + "' and '" + ed_date + "' order by a.hos_no asc";
}

dt = sqlHelper.getSqlHelper().Excute(sql_1);
countdata.InnerText = Convert.ToString(dt.Rows.Count);
}
if (con == "2")//末次月经时间
{
string sql_1 = "";
if (hos_no.Value != "37" && (fillid.Value != "151" || fillid.Value != "153"))//不是保健院的统计员
{
sql_1 = @"select a.flag,a.hos_name,a.sort_id as a_sort_id,b.sort_id as b_sort_id,a.m_name,a.card_id,a.get_hiv_time,a.l_menstruation,
b.pregnancy,b.brithday,b.perinatal,b.u_medication,
b.pregnancy_time+'('+ltrim(b.pregnancy_week)+')' as pregnancy_time,
b.s_situation,b.HIV_medication,isnull(b.children_id,'/') as children_id
from tab_3_I a,tab_3_II b
where a.hos_no*=b.hos_no
and a.card_id*=b.card_id
and a.l_menstruation between '" + st_date + "' and '" + ed_date + "' and a.hos_no='" + hos_no.Value + "' and a.fillid='" + fillid.Value + "' order by a.hos_no asc";
}
else
{
sql_1 = @"select a.flag,a.hos_name, a.sort_id as a_sort_id,b.sort_id as b_sort_id,a.m_name,a.card_id,a.get_hiv_time,a.l_menstruation,
b.pregnancy,b.brithday,b.perinatal,b.u_medication,
b.pregnancy_time+'('+ltrim(b.pregnancy_week)+')' as pregnancy_time,
b.s_situation,b.HIV_medication,isnull(b.children_id,'/') as children_id
from tab_3_I a,tab_3_II b
where a.hos_no*=b.hos_no
and a.card_id*=b.card_id
and a.l_menstruation between '" + st_date + "' and '" + ed_date + "' order by a.hos_no asc";
}

dt = sqlHelper.getSqlHelper().Excute(sql_1);
countdata.InnerText = Convert.ToString(dt.Rows.Count);
}
html.Append("<table id='data' style='width:140%;border-collapse:collapse; border:1px solid gray;font-size:11px;' border='1'>");
html.Append("<tr style='background-color:#FFCC33'><th>医院名称</th><th>产妇姓名</th><th>孕妇编号</th><th>儿童编号</th><th>确认感染时间</th><th>未次月经</th><th>妊娠结局</th>");
html.Append("<th>分娩时间</th><th>围产儿转归</th><th>药物应用情况</th><th>开始用药时间</th>");
html.Append("<th>新生儿存活情况</th><th>新生婴儿药物应用情况</th><th>状态</th><th>表3-I</th><th>表3-II</th><th>表3-III</th></tr>");
html.Append("<tbody>");
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i % 2 == 0)
{
html.Append("<tr style='text-align:center;cursor:pointer; background-color:#ccff99;font-size:11px;'>");
}
else
{
html.Append("<tr style='text-align:center;cursor:pointer;font-size:11px;'>");
}
html.Append("<td>" + dt.Rows[i]["hos_name"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["m_name"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["card_id"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["children_id"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["get_hiv_time"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["l_menstruation"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["pregnancy"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["brithday"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["perinatal"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["u_medication"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["pregnancy_time"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["s_situation"].ToString().Trim() + "</td>");
html.Append("<td>" + dt.Rows[i]["HIV_medication"].ToString().Trim() + "</td>");
}
这是getdata_md.aspx文件


</head>
<body>
<form id="form1" runat="server">
<div id="header">
报表类型:<select runat="server" id="rep_type" style="width: 80px;">
<option value="2">梅毒</option>
</select>
        
条件:<select runat="server" id="condition" style="width: 100px;">
<option value="1">上报时间</option>
<option value="2">末次月经时间</option>
</select>
        
时间:从<input runat="server" type="text" id="st_time" onfocus="WdatePicker();" onclick="WdatePicker();"
style="width: 75px;" />至<input type="text" runat="server" id="ed_time" onfocus="WdatePicker();"
onclick="WdatePicker();" style="width: 75px;" />        
<asp:Button ID="alldata" runat="server" Text="统计" Style="width: 80px;" OnClick="alldata_Click1" />  
<div>
总数量:<label id="countdata" runat="server"></label>
过滤:<input type="text" id="content_txt" style="width: 90px;" /></div>
</div>
<div id="show_detail" runat="server" style="margin-top: 5px; border-top: 2px solid gray;">
</div>
<input type="hidden" runat="server" id="fillid" />
<input type="hidden" runat="server" id="hos_no" />
</form>
</body>
</html>


如何在getdata_md.aspx文件中添加一个导出到excel按钮的功能? 或者能否将getdata_md.aspx.cs文件中table id的值取到getdata_md_aspx
...全文
221 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
余生Her 2014-10-23
  • 打赏
  • 举报
回复
导入导出Excel 2003 2007建议使用NPOI,非常简单
gwygwl_2285662869 2014-10-23
  • 打赏
  • 举报
回复
asp.net关于导出excel问题 //aspx页面定制导出excel按钮事件 <asp:Button ID="Button1" runat="server" Font-Size="9pt" OnClick="Button1_Click" Text="导出Excel" style="border-left-color: #3333ff; border-bottom-color: #3333ff; border-top-style: inset; border-top-color: #3333ff; border-right-style: inset; border-left-style: inset; border-right-color: #3333ff; border-bottom-style: inset"/> //导出Excel并显示 SqlConnection sqlcon = new SqlConnection(ConfigurationManager.AppSettings["conStr"]); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { bind(); } } protected void Button1_Click(object sender, EventArgs e) { Export("application/ms-excel", "商品信息表.xls"); } private void Export(string FileType, string FileName) { Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF7; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); Response.ContentType = FileType; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); } private void bind() { SqlDataAdapter myda = new SqlDataAdapter("select top 10 GoodsID as 商品ID,GoodsName as 商品名称,GoodsIntroduce as 商品介绍,GoodsPrice as 商品价格 from tb_GoodsInfo", sqlcon); DataSet myds = new DataSet(); sqlcon.Open(); myda.Fill(myds); sqlcon.Close(); GridView1.DataSource = myds; GridView1.DataBind(); }
活在现实0 2014-10-22
  • 打赏
  • 举报
回复
点击按钮,调用下面的JS方法,这里给出了JS代码,后台的C#代码自己可以搞定吧
function outPutExcel() {
            var data = { method: "BY_OrderToExcel",xxx:xxx};
            $.ajax({
                url: '../../../Data.aspx?assemby=xxxxxxxxxxxxxxxxx',
                dataType: 'text',
                async: false,
                data: data,
                type: 'POST',
                success: function (result) {
                    if (result == "False")
                        $.messager.alert('', '导出失败!', 'info');
                    else {
                         //iframe1 随便选的一个页面元素;
                         //"../../Temp/" + result  是后台方法生成的EXCEL并保存在服务器上的EXCEL文件路径
                        $("#Iframe1").attr("src", "../../Temp/" + result);
                    }
                }

            });
        }
qzyf1992 2014-10-22
  • 打赏
  • 举报
回复
把table的值转化为json,json转datatable,然后用Npoi,com,openxml,epplus,aspose,spire.xls等三方框架转化为excel
save4me 2014-10-22
  • 打赏
  • 举报
回复
你的table id不是在cs文件里面写死的吗,就是data。在getdata_md_aspx如果需要获取table的内容,直接用javascript的document.getElementById('data')就可以。
21-Guns 2014-10-22
  • 打赏
  • 举报
回复
引用 5 楼 yangmingxing980 的回复:
http://blog.csdn.net/yangmingxing980/article/details/39048795
无法访问?
21-Guns 2014-10-22
  • 打赏
  • 举报
回复
大神也说下后台代码吧 我没学过C# 被硬拉过来做这个 还不太熟悉

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧