我正在爲我的ASP .NET應用程序使用Visual Studio 2008。我正嘗試通過Response對象將一些帶日文字符的報表導出到Excel中。當我嘗試導出時,所有日文字符看起來都是亂碼。它適用於漢字。這是我試過的:Asp.Net導出到Excel - 日文字符
我試過安裝日語語言包/編碼爲UTF-8/UTF-7/Shift-JIS/Globalization(Web.Config)..但沒有運氣。任何想法如何解決這個問題?謝謝 !!
string attachment = "attachment; filename=PerksPlusReport.xls";
//Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("content-disposition", attachment);
//Response.Charset = "UTF-8";
//Response.Charset = "UTF-7";
//Response.Charset = "Shift_JIS";
Response.ContentType = "application/vnd.ms-excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
// Create a form to contain the grid
HtmlForm frm = new HtmlForm();
ReportGridView.Parent.Controls.Add(frm);
frm.Attributes["runat"] = "server";
GridView GridView2 = new GridView();
ReportView reportDetails = GetReportDetails();
GridView2.DataSource = GetReportResults(this.ReportId.Value, reportDetails.Sql);
GridView2.DataBind();
PrepareGridViewForExport(GridView2);
frm.Controls.Add(GridView2);
frm.RenderControl(htw);
string fileContents = sw.ToString();
int startSpot = fileContents.IndexOf("<table");
fileContents = fileContents.Substring(startSpot);
int endSpot = fileContents.IndexOf("</table>");
fileContents = fileContents.Substring(0, endSpot + 8);
try
{
// Replace all < and > with <and>
fileContents = fileContents.Replace("<", "<");
fileContents = fileContents.Replace(">", ">");
fileContents = fileContents.Replace("€", "€");
string RegularExpression = @"<a[^>]*>([^<]*)</a>";
Regex regex = new Regex(RegularExpression);
//If match found .. uses the delegate function to replace the whole content with the filtered values
if (regex.IsMatch(fileContents))
{
regex.Replace(fileContents, delegate (Match m){return fileContents.Replace(m.Captures[0].Value, m.Groups[1].Value);});
}
}
catch (Exception ex2)
{
Response.Write(ex2.ToString());
}
Response.Write(fileContents);
Response.End();
序言聲明固定對我來說。 – koenmetsu 2011-01-21 08:23:52