首页 新闻 搜索 专区 学院

asp.net导入不规则数据到excel中

0
悬赏园豆:5 [已关闭问题]

谁能救救我啊,能将这个表格导入到excel中吗?

这里是两个Repeater嵌套,班级编号、班级名 ,根据班级编号 获取到学生id 和学生名

前两列是班级 后两列是 学生,

现在是要将这份表格用asp.net 导入到excel中。。。跪求答案了~

like%'远远'%的主页 like%'远远'% | 小虾三级 | 园豆:656
提问于:2010-03-26 23:21
< >
分享
其他回答(1)
0

直接将页面导出

html

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2"
    ValidateRequest="false" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>

    <script language="javascript" type="text/javascript">
    function ToExcel()
    {
      var html=document.getElementById("txt_html");
      var val=document.getElementById("div_repeat1").innerHTML;
      html.value=val;
    }
   
    </script>

</head>
<body>
    <form id="form1" runat="server">
        <div>
            <div id='div_repeat1'>
                <asp:Repeater ID="Repeater1" runat="server">
                    <ItemTemplate>
                        <table>
                            <tr>
                                <td>
                                    aa</td>
                                <td>
                                    <table>
                                        <tr>
                                            <td>
                                                bbb</td>
                                        </tr>
                                    </table>
                                </td>
                            </tr>
                        </table>
                    </ItemTemplate>
                </asp:Repeater>
                <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" OnClientClick="javascript:ToExcel(this)" /></div>
        </div>
        <asp:TextBox ID="txt_html" runat="server" Style="display: none"></asp:TextBox>
    </form>
</body>
</html>

 

 

===cs

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string mm = "ASAAAAAAAAAAAAAAAAAAAAVEWR";
        Repeater1.DataSource = mm.ToCharArray();
        Repeater1.DataBind();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Report.xls");
        HttpContext.Current.Response.Charset = "";
       
        HttpContext.Current.Response.Write(Regex.Replace(txt_html.Text  , @"(<(a|A)(.*)>)(.*)(<\/(a|A)>)", "$4").Replace("\"GridView1\"", "\"GridView1\" border=1"));
        HttpContext.Current.Response.End();
    }
}

 

2.利用这个导出。不用安装EXCEL。支持合并

[二合一]C#读取和导出EXCEL类库(可操作多个SHEET)

woody.wu | 园豆:3621 (老鸟四级) | 2010-03-27 08:47
0

页面有必要一定用导的吧。生成一个excel文件不久得了,就像生成xml差不多,省得麻烦

LittlePeng | 园豆:3395 (老鸟四级) | 2010-03-27 18:33
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册