• 后台导出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


  • 相关阅读:
    C++中析构函数的作用,
    fp = fopen(s, "at") 中at 是啥意思,a 是append 追加的意思
    C++中 :: 的意思
    sed 指令
    make -e install ,,,make命令的-e选项!
    _AR="ar" _ARFLAGS="-ruv"
    gcc的-D和-U参数:宏的设置与取消 _CCFLAGS=" -w -enable-threads=posix -DLINUX -D_REENTRANT -DWORKONGN -Dlinux -D_GN_DETAIL_SDR_"
    GCC 编译详解
    RPC 编程 使用 RPC 编程是在客户机和服务器实体之间进行可靠通信的最强大、最高效的方法之一。它为在分布式计算环境中运行的几乎所有应用程序提供基础。
    vim插件ctags的安装和使用
  • 原文地址:https://www.cnblogs.com/apollokk/p/6713930.html
Copyright © 2020-2023  润新知