GridView 导出 Excel时,某几列不要显示出来。有什么好办法(在不修改gridview里面的数据情况下)

时间:2021-05-28 11:00:50
GridView 导出 Excel时,某几列不要显示出来。有什么好办法(在不修改gridview里面的数据情况下)
因为Gridview 里面有些图片。删掉又不好看。导出来的excel又有××, 故不想让 那列显示。

9 个解决方案

#1


自己构建格式呗
拿去用吧
自己看的改  
Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            Page.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=Order.xls");
            int total = 0;
            #region 取商品分类
            IDictionary kind = QueryUtil.GetConfigItems(ConfigType.CommodityKind);
            SortedList sl = new SortedList();
            foreach (DictionaryEntry entry in kind)
            {
                sl.Add(entry.Value, entry.Key);
            }
            #endregion
            DataTable dt = QueryUtil.GetDataResult(QueryEnum.MerchantPosjsmx, 1, (int)ViewState["CalTotal"], (Hashtable)ViewState["Query"], false, out total);
            IDictionary colDic = GetColumn();
            StringBuilder sb = new StringBuilder();
            sb.Append("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"></head><body><center>");
            sb.Append("<table>");
            sb.Append("<tr>");
            foreach (DataColumn col in dt.Columns)
            {
                if (colDic.Contains(col.ColumnName.ToUpper()))
                    sb.AppendFormat("<td>{0}</td>", colDic[col.ColumnName.ToUpper()].ToString());
            }
            sb.Append("</tr>");
            foreach (DataRow row in dt.Rows)
            {
                sb.Append("<tr>");
                foreach (DataColumn col in dt.Columns)
                {
                    if (colDic.Contains(col.ColumnName.ToUpper()))
                    {
                        if (col.ColumnName.Equals("CUST_TYPE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", CustType(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CLASS", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", CardClass(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["MERCHANT_PROVINCE"].ToString())));

                        else if (col.ColumnName.Equals("CUST_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["CUST_PROVINCE"].ToString())));
                       
                        else
                            sb.AppendFormat("<td>{0}</td>", row[col.ColumnName].ToString());
                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            sb.Append("</center></body></html>");
            Response.Write(sb.ToString());
            Response.End();

#2


引用 1 楼 jieyi1106 的回复:
自己构建格式呗
拿去用吧
自己看的改 
Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            Page.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=Order.xls");
            int total = 0;
            #region 取商品分类
            IDictionary kind = QueryUtil.GetConfigItems(ConfigType.CommodityKind);
            SortedList sl = new SortedList();
            foreach (DictionaryEntry entry in kind)
            {
                sl.Add(entry.Value, entry.Key);
            }
            #endregion
            DataTable dt = QueryUtil.GetDataResult(QueryEnum.MerchantPosjsmx, 1, (int)ViewState["CalTotal"], (Hashtable)ViewState["Query"], false, out total);
            IDictionary colDic = GetColumn();
            StringBuilder sb = new StringBuilder();
            sb.Append(" <html> <head> <meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"> </head> <body> <center>");
            sb.Append(" <table>");
            sb.Append(" <tr>");
            foreach (DataColumn col in dt.Columns)
            {
                if (colDic.Contains(col.ColumnName.ToUpper()))
                    sb.AppendFormat(" <td>{0} </td>", colDic[col.ColumnName.ToUpper()].ToString());
            }
            sb.Append(" </tr>");
            foreach (DataRow row in dt.Rows)
            {
                sb.Append(" <tr>");
                foreach (DataColumn col in dt.Columns)
                {
                    if (colDic.Contains(col.ColumnName.ToUpper()))
                    {
                        if (col.ColumnName.Equals("CUST_TYPE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", CustType(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CLASS", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", CardClass(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["MERCHANT_PROVINCE"].ToString())));

                        else if (col.ColumnName.Equals("CUST_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["CUST_PROVINCE"].ToString())));
                     
                        else
                            sb.AppendFormat(" <td>{0} </td>", row[col.ColumnName].ToString());
                    }
                }
                sb.Append(" </tr>");
            }
            sb.Append(" </table>");
            sb.Append(" </center> </body> </html>");
            Response.Write(sb.ToString());
            Response.End();

感觉好复杂的哦!有简便点的么

#3


我前几天也遇见过这个情况,我后来想了2个办法,一个就是在查询的时候不查询那个字段
第二个就是用水晶报表弄的,里面有自带的导入导出,格式很多

#4


引用 3 楼 yupengcheng_net 的回复:
我前几天也遇见过这个情况,我后来想了2个办法,一个就是在查询的时候不查询那个字段
第二个就是用水晶报表弄的,里面有自带的导入导出,格式很多

自己再up下!!

#5


在点击  导出按钮的时候把 不需要导出的那几列 隐藏掉即可。。
//导出Excel,此为导出按钮 
    protected void Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", "学生成绩报表.xls");
    }
    //导出Excel函数
    private void Export(string FileType, string FileName)
    {
      //在这里将你不想要的列隐藏掉即可。比如隐藏 6,7行 
        gridview1.Rows[5].visable=false;
         gridview1.Rows[6].visable=false;
        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();
    }
    //导出方法,必须有!!!!!!!!
    public override void VerifyRenderingInServerForm(Control control)
    {
    }

#6


同意楼上,隐藏列即可

#7


5楼已经解决

#8


引用 5 楼 chenjianyong94 的回复:
在点击  导出按钮的时候把 不需要导出的那几列 隐藏掉即可。。
C# code//导出Excel,此为导出按钮protectedvoid Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel","学生成绩报表.xls");
    }//导出Excel函数privatevoid Export(string FileType,string FileName)
    {//在这里将你不想要的列隐藏掉即可。比如隐藏 6,7行        gridview1.Rows[5].visable=false;
         gridview1.Rows[6].visable=false;
        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();
    }//导出方法,必须有!!!!!!!!publicoverridevoid VerifyRenderingInServerForm(Control control)
    {
    }

不错不错的!!

#9


用第三方得导出怎样设置像这样的隐藏啊   

#1


自己构建格式呗
拿去用吧
自己看的改  
Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            Page.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=Order.xls");
            int total = 0;
            #region 取商品分类
            IDictionary kind = QueryUtil.GetConfigItems(ConfigType.CommodityKind);
            SortedList sl = new SortedList();
            foreach (DictionaryEntry entry in kind)
            {
                sl.Add(entry.Value, entry.Key);
            }
            #endregion
            DataTable dt = QueryUtil.GetDataResult(QueryEnum.MerchantPosjsmx, 1, (int)ViewState["CalTotal"], (Hashtable)ViewState["Query"], false, out total);
            IDictionary colDic = GetColumn();
            StringBuilder sb = new StringBuilder();
            sb.Append("<html><head><meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"></head><body><center>");
            sb.Append("<table>");
            sb.Append("<tr>");
            foreach (DataColumn col in dt.Columns)
            {
                if (colDic.Contains(col.ColumnName.ToUpper()))
                    sb.AppendFormat("<td>{0}</td>", colDic[col.ColumnName.ToUpper()].ToString());
            }
            sb.Append("</tr>");
            foreach (DataRow row in dt.Rows)
            {
                sb.Append("<tr>");
                foreach (DataColumn col in dt.Columns)
                {
                    if (colDic.Contains(col.ColumnName.ToUpper()))
                    {
                        if (col.ColumnName.Equals("CUST_TYPE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", CustType(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CLASS", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", CardClass(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["MERCHANT_PROVINCE"].ToString())));

                        else if (col.ColumnName.Equals("CUST_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat("<td>{0}</td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["CUST_PROVINCE"].ToString())));
                       
                        else
                            sb.AppendFormat("<td>{0}</td>", row[col.ColumnName].ToString());
                    }
                }
                sb.Append("</tr>");
            }
            sb.Append("</table>");
            sb.Append("</center></body></html>");
            Response.Write(sb.ToString());
            Response.End();

#2


引用 1 楼 jieyi1106 的回复:
自己构建格式呗
拿去用吧
自己看的改 
Response.Clear();
            Response.ContentType = "application/vnd.ms-excel";
            Response.Charset = "";
            Page.EnableViewState = false;
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.AppendHeader("Content-Disposition", "attachment;filename=Order.xls");
            int total = 0;
            #region 取商品分类
            IDictionary kind = QueryUtil.GetConfigItems(ConfigType.CommodityKind);
            SortedList sl = new SortedList();
            foreach (DictionaryEntry entry in kind)
            {
                sl.Add(entry.Value, entry.Key);
            }
            #endregion
            DataTable dt = QueryUtil.GetDataResult(QueryEnum.MerchantPosjsmx, 1, (int)ViewState["CalTotal"], (Hashtable)ViewState["Query"], false, out total);
            IDictionary colDic = GetColumn();
            StringBuilder sb = new StringBuilder();
            sb.Append(" <html> <head> <meta http-equiv=Content-Type content=\"text/html; charset=GB2312\"> </head> <body> <center>");
            sb.Append(" <table>");
            sb.Append(" <tr>");
            foreach (DataColumn col in dt.Columns)
            {
                if (colDic.Contains(col.ColumnName.ToUpper()))
                    sb.AppendFormat(" <td>{0} </td>", colDic[col.ColumnName.ToUpper()].ToString());
            }
            sb.Append(" </tr>");
            foreach (DataRow row in dt.Rows)
            {
                sb.Append(" <tr>");
                foreach (DataColumn col in dt.Columns)
                {
                    if (colDic.Contains(col.ColumnName.ToUpper()))
                    {
                        if (col.ColumnName.Equals("CUST_TYPE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", CustType(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CLASS", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", CardClass(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("MERCHANT_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["MERCHANT_PROVINCE"].ToString())));

                        else if (col.ColumnName.Equals("CUST_PROVINCE", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertProvince(row[col.ColumnName].ToString()));
                        else if (col.ColumnName.Equals("CUST_CITY", StringComparison.InvariantCultureIgnoreCase))
                            sb.AppendFormat(" <td>{0} </td>", QueryUtil.ConvertCity(row[col.ColumnName].ToString(), QueryUtil.ConvertProvince(row["CUST_PROVINCE"].ToString())));
                     
                        else
                            sb.AppendFormat(" <td>{0} </td>", row[col.ColumnName].ToString());
                    }
                }
                sb.Append(" </tr>");
            }
            sb.Append(" </table>");
            sb.Append(" </center> </body> </html>");
            Response.Write(sb.ToString());
            Response.End();

感觉好复杂的哦!有简便点的么

#3


我前几天也遇见过这个情况,我后来想了2个办法,一个就是在查询的时候不查询那个字段
第二个就是用水晶报表弄的,里面有自带的导入导出,格式很多

#4


引用 3 楼 yupengcheng_net 的回复:
我前几天也遇见过这个情况,我后来想了2个办法,一个就是在查询的时候不查询那个字段
第二个就是用水晶报表弄的,里面有自带的导入导出,格式很多

自己再up下!!

#5


在点击  导出按钮的时候把 不需要导出的那几列 隐藏掉即可。。
//导出Excel,此为导出按钮 
    protected void Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel", "学生成绩报表.xls");
    }
    //导出Excel函数
    private void Export(string FileType, string FileName)
    {
      //在这里将你不想要的列隐藏掉即可。比如隐藏 6,7行 
        gridview1.Rows[5].visable=false;
         gridview1.Rows[6].visable=false;
        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();
    }
    //导出方法,必须有!!!!!!!!
    public override void VerifyRenderingInServerForm(Control control)
    {
    }

#6


同意楼上,隐藏列即可

#7


5楼已经解决

#8


引用 5 楼 chenjianyong94 的回复:
在点击  导出按钮的时候把 不需要导出的那几列 隐藏掉即可。。
C# code//导出Excel,此为导出按钮protectedvoid Button1_Click(object sender, EventArgs e)
    {
        Export("application/ms-excel","学生成绩报表.xls");
    }//导出Excel函数privatevoid Export(string FileType,string FileName)
    {//在这里将你不想要的列隐藏掉即可。比如隐藏 6,7行        gridview1.Rows[5].visable=false;
         gridview1.Rows[6].visable=false;
        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();
    }//导出方法,必须有!!!!!!!!publicoverridevoid VerifyRenderingInServerForm(Control control)
    {
    }

不错不错的!!

#9


用第三方得导出怎样设置像这样的隐藏啊