C# Excel报表中项目名称中未正确携带特殊字符
当我试图下载项目名称时,我在Excel导出中获得了一个不同的字符作为“特殊宪章” 我应该怎么做才能正确显示我的所有特殊字符C# Excel报表中项目名称中未正确携带特殊字符,c#,asp.net,C#,Asp.net,当我试图下载项目名称时,我在Excel导出中获得了一个不同的字符作为“特殊宪章” 我应该怎么做才能正确显示我的所有特殊字符 protected void lnkExportToExcel_Click(object sender, EventArgs e) { //INS1-219 DataTable dt1 = DataHelper.ExecuteQuery(Session["strForExcelReport"].ToString());
protected void lnkExportToExcel_Click(object sender, EventArgs e)
{
//INS1-219
DataTable dt1 = DataHelper.ExecuteQuery(Session["strForExcelReport"].ToString());
DataTable dt3 = new DataTable();
dt3.Columns.Add("Project Code");
dt3.Columns.Add("Project Name");
/// many more columns
foreach (DataRow dr in dt1.Rows)
{
drnew["Project Code"] = dr["ProjectCode"].ToString();
drnew["Project Name"] = dr["ProjectName"].ToString();
drnew["Client"] = dr["OrganizationName"].ToString();
// many, many business rules
dt3.Rows.Add(drnew);
}
// DataTable is now complete
string attachment = "attachment; filename=ProjectInHandDetailsList_" + DateTime.Now.ToString("dd MMM yyyy") + ".xls";
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
string tab = "";
foreach (DataColumn dc in dt3.Columns)
{
Response.Write(tab + dc.ColumnName);
tab = "\t";
}
Response.Write("\n");
int i;
foreach (DataRow dr in dt3.Rows)
{
tab = "";
for (i = 0; i < dt3.Columns.Count; i++)
{
Response.Write(tab + dr[i].ToString());
tab = "\t";
}
Response.Write("\n");
}
Response.End();
//END INS1-219
}
受保护的无效lnkExportToExcel\u单击(对象发送方,事件参数e)
{
//INS1-219
DataTable dt1=DataHelper.ExecuteQuery(会话[“strForExcelReport”].ToString());
DataTable dt3=新的DataTable();
dt3.列。添加(“项目代码”);
dt3.列。添加(“项目名称”);
///更多专栏
foreach(dt1.Rows中的数据行dr)
{
drnew[“项目代码”]=dr[“项目代码”].ToString();
drnew[“Project Name”]=dr[“ProjectName”].ToString();
drnew[“Client”]=dr[“OrganizationName”].ToString();
//很多很多商业规则
dt3.Rows.Add(drnew);
}
//DataTable现在已完成
string attachment=“attachment;filename=projectnhanddetailslist_quot+DateTime.Now.ToString(“dd-MMM-yyyy”)+“.xls”;
Response.ClearContent();
AddHeader(“内容处置”,附件);
Response.ContentType=“application/vnd.ms excel”;
字符串选项卡=”;
foreach(dt3.Columns中的数据列dc)
{
Response.Write(制表符+dc.ColumnName);
tab=“\t”;
}
响应。写入(“\n”);
int i;
foreach(dt3.Rows中的数据行dr)
{
tab=“”;
对于(i=0;i
使用上述代码,Excel工作表中的项目名称如下所示:
皇家免费闪光案例研究3–胸膜炎胸部Pa
这是数据库中的值:
皇家免费闪光案例研究3–胸膜炎性胸痛
如何防止这些奇怪的字符出现?您需要在标头和中使用a将字符串写入输出流。编码不匹配是奇怪的字符替换的原因
Response.ClearContent();
Response.AddHeader("content-disposition", attachment);
Response.ContentType = "application/vnd.ms-excel";
// set the contentencoding of the http content
Response.ContentEncoding = Encoding.UTF8;
// use a streamwriter that knows how to write strings in
// a specified encoding to a stream
using(var sw = new StreamWriter(Response.OutputStream, Response.ContentEncoding))
{
string tab = "";
foreach (DataColumn dc in dt3.Columns)
{
// use the StreamWriter
sw.Write(tab + dc.ColumnName);
tab = "\t";
}
sw.Write("\n");
int i;
foreach (DataRow dr in dt3.Rows)
{
tab = "";
for (i = 0; i < dt3.Columns.Count; i++)
{
sw.Write(tab + dr[i].ToString());
tab = "\t";
}
sw.Write("\n");
}
}
Response.End();
Response.ClearContent();
AddHeader(“内容处置”,附件);
Response.ContentType=“application/vnd.ms excel”;
//设置http内容的contentencoding
Response.ContentEncoding=Encoding.UTF8;
//使用知道如何在中写入字符串的streamwriter
//流的指定编码
使用(var sw=newstreamwriter(Response.OutputStream,Response.ContentEncoding))
{
字符串选项卡=”;
foreach(dt3.Columns中的数据列dc)
{
//使用StreamWriter
sw.Write(制表符+dc.ColumnName);
tab=“\t”;
}
sw.Write(“\n”);
int i;
foreach(dt3.Rows中的数据行dr)
{
tab=“”;
对于(i=0;i
我已经编辑了(删除了大部分代码)您的问题,我认为现在的问题有点像一个问题。请检查我的问题,并在需要时对其进行增强。您可能还想使用验证您的问题是否已准备好发布。