Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在gridview中显示数据库中的数据时删除特定标记_C#_Asp.net_Sql Server_Gridview - Fatal编程技术网

C# 在gridview中显示数据库中的数据时删除特定标记

C# 在gridview中显示数据库中的数据时删除特定标记,c#,asp.net,sql-server,gridview,C#,Asp.net,Sql Server,Gridview,我有一些数据需要在GridView中显示,然后允许用户将其导出为excel文件。 sql当前看起来如下所示: public static class Export { public static DataTable ExportData() { using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["product.products_tblConn

我有一些数据需要在GridView中显示,然后允许用户将其导出为excel文件。 sql当前看起来如下所示:

public static class Export
{
    public static DataTable ExportData()
    {

        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["product.products_tblConnectionString"].ConnectionString))
        using (SqlCommand command = new SqlCommand("SELECT TITLE, CUSTOMER, PRODUCTS, PHOTOGRAPHS FROM products_tbl WHERE LANG = 'EN' ", conn))
        {
            DataTable data = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter(command);
            adapter.Fill(data);
            return data;

        }

    }
}
现在的问题是:在数据库中有一些东西,如标记或额外空间等。我无法在数据库中删除它们,但我确实需要在从数据库中获取数据时,以及在GridView中显示数据之前,或者在用户将数据导出到excel文件后,删除它们

“导出到excel”功能如下所示:

 private void ExportGridToExcel()
{
    Response.Clear();
    Response.Buffer = true;
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Charset = "";
    string FileName = "Product" + DateTime.Now + ".xls";
    StringWriter strwritter = new StringWriter();
    HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
    Response.ContentEncoding = System.Text.Encoding.Unicode;
    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
    GridView1.AllowPaging = false;
    ResultsFilter();
    GridView1.DataBind();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
    GridView1.GridLines = GridLines.Both;
    GridView1.HeaderStyle.Font.Bold = true;
    GridView1.RenderControl(htmltextwrtter);
    Response.Write(strwritter.ToString());
    Response.End();

}
SELECT  
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("CUSTOMER",'<p>',''),'</p>',''),'<br>','')
 ,'</br>',''),'  ','') ,'   ',''),'\n','')  as 'CUSTOMER',

 FROM dbo.products_tbl

FROM
在我刚刚使用Sql Server数据导出管理器之前,我使用了replace来删除额外的标记等。因此类似这样的内容:

 private void ExportGridToExcel()
{
    Response.Clear();
    Response.Buffer = true;
    Response.ClearContent();
    Response.ClearHeaders();
    Response.Charset = "";
    string FileName = "Product" + DateTime.Now + ".xls";
    StringWriter strwritter = new StringWriter();
    HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
    Response.ContentEncoding = System.Text.Encoding.Unicode;
    Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
    GridView1.AllowPaging = false;
    ResultsFilter();
    GridView1.DataBind();
    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.ContentType = "application/vnd.ms-excel";
    Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);
    GridView1.GridLines = GridLines.Both;
    GridView1.HeaderStyle.Font.Bold = true;
    GridView1.RenderControl(htmltextwrtter);
    Response.Write(strwritter.ToString());
    Response.End();

}
SELECT  
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("CUSTOMER",'<p>',''),'</p>',''),'<br>','')
 ,'</br>',''),'  ','') ,'   ',''),'\n','')  as 'CUSTOMER',

 FROM dbo.products_tbl

FROM
我知道它不漂亮,但它奏效了。我试图在我的.cs文件的sql查询中使用相同的东西,但它似乎没有删除标记、空格等。
有人知道我从数据库获取数据或用户将数据导出到excel时如何做到这一点吗?

我根本不会在sql级别这样做。相反,使用一个正则表达式,它将允许您在c中的单个替换中替换所需的所有内容。同意将您的业务逻辑排除在数据访问之外,更好的是,为什么不在插入时对其进行清理?@ZoharPeled您是否碰巧有关于如何使用reg ex进行替换的资源或示例?我不太了解它,因为你在用c,而且