我有一些数据需要在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;
}
}
}
现在问题是:在数据库中有几个东西,如<p>
标签或<br>
和额外的空格等。我无法在数据库中摆脱它们,但我确实需要在从数据库获取数据之后以及在用户将数据导出到excel文件之后在GridView
或中显示数据之前将其删除。
我对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();
}
在我刚刚使用Sql Server数据导出管理器并使用replace来删除额外的标签等之前,所以像这样:
SELECT
REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE(REPLACE("CUSTOMER",'<p>',''),'</p>',''),'<br>','')
,'</br>',''),' ','') ,' ',''),'\n','') as 'CUSTOMER',
FROM dbo.products_tbl
FROM
我知道它不漂亮,但它有效。我尝试在我的.cs文件中的sql查询中使用相同类型的东西但它似乎没有删除标记,空格等。 任何人都知道如何从数据库获取数据或用户将数据导出到Excel时如何实现这一目标?