zoukankan      html  css  js  c++  java
  • 后台导出GridView到Excel,可选择要导出的列

    前端的GridView不要用自动生成列,否则取不到列的资料,不知自动生成列有什么办法?

    前端增加一个CheckBoxList用来显示GridView列,导出的时候选择:

        <asp:Panel ID="panel1" runat="server" Style="display: none; text-align: center;  500px;">
            <div style=" 500px; margin: 0 auto; border: 2px solid green; text-align: center;
                background-color: Silver;">
                <asp:Panel ID="Panel2" runat="server">
                    <div class="thead" style="line-height: 30px; height: 30px; cursor:move;">
                        <span style="float: left; padding-left: 5px;">请选择要导出的列</span> <span style="float: right;
                            clear: both; padding-right: 5px;">
                            <img src="../icon/cancel.png" onclick="document.getElementById('panel1').style.display='none';"
                                style="vertical-align: middle; cursor:pointer;" alt="单击关闭" />
                        </span>
                    </div>
                </asp:Panel>
                <asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="4" CellPadding="5"
                    CellSpacing="0" RepeatDirection="Horizontal" Width="500px">
                </asp:CheckBoxList>
                <asp:Button ID="Button1" runat="server" Text="确定导出" CssClass="SmallButton" Style="margin: 5px 10px 5px 10px;
                    float: right;" OnClick="Button1_Click" />
            </div>
        </asp:Panel>
        <asp:AlwaysVisibleControlExtender ID="AlwaysVisibleControlExtender1" runat="server"
            TargetControlID="panel1" HorizontalSide="Center" VerticalOffset="100">
        </asp:AlwaysVisibleControlExtender>
        <asp:DragPanelExtender ID="DragPanelExtender1" runat="server" TargetControlID="panel1" DragHandleID="Panel2">
        </asp:DragPanelExtender>

    后端在加载时填充CheckBoxList:

    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
     
                for (int i = 0; i < GridView1.Columns.Count; i++)
                {
                    if(GridView1.Columns[i] is BoundField)
                    {
                        if (GridView1.Columns[i].Visible && GridView1.Columns[i].HeaderStyle.CssClass!="hide")
                        {
                            CheckBoxList1.Items.Add(new ListItem(GridView1.Columns[i].HeaderText, ((BoundField)this.GridView1.Columns[i]).DataField));
                        }
                    }
                }
                for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                {
                    CheckBoxList1.Items[i].Selected = true;
                }
            }
        }
    

    导出按钮:

        protected void Button1_Click(object sender, EventArgs e)
        {
            string text = "";
            string value="";
            for (int i = 0; i < CheckBoxList1.Items.Count; i++)
            {
                if (CheckBoxList1.Items[i].Selected)
                {
                    text += CheckBoxList1.Items[i].Text + ",";
                    value += CheckBoxList1.Items[i].Value + ",";
                }
            }
            if (text != "")
            {
                text = text.Substring(0, text.Length - 1);
                value = value.Substring(0, value.Length - 1);
                string[] oldColName = value.Split(',');
                string[] newColName = text.Split(',');
                //获取数据,代码根据需要自行编写
                System.Data.DataTable dt = getData().ToTable() ;
                ExcelHelper.ExportByWeb(dt, date1.Text + "~" + date2.Text + "工作表标题", "文件名.xls", "工作表名称",oldColName,newColName);
            }
        }



    导出时调用的代码见另外一篇文章:

    http://blog.csdn.net/apollokk/article/details/8025611


  • 相关阅读:
    清除浮动的方法
    网页滚动到顶部或底部加载
    任意两个数之间的随机数
    每五个一行
    js放大镜
    HTML5本地存储
    jquery checkbox radio 标签 选中的3种方法
    jquery mouseout mouseover 多次执行
    CentOS6.5安装MySQL5.7(也适合其他版本安装)
    ubuntu下ganglia3.7.2编译安装
  • 原文地址:https://www.cnblogs.com/apollokk/p/6713930.html
Copyright © 2011-2022 走看看