DATAGRID导出EXCEL中文内容数据乱码?
如题:导出按钮代码如下,请各位大虾帮帮忙? Show_CustomerList.AllowPaging = false; //设置不能分页
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
String st_selsql = "SELECT dbo.CarInfo.RepairID, dbo.CarInfo.ChePai, dbo.Car.CarName,dbo.Project.ProjectName, dbo.BanZhu.BanZhuName, dbo.Sa.SaName,dbo.Repair.JiShiBianHao, dbo.Chengben.ChengBenLeiXing, dbo.Repair.ChengBen,dbo.Repair.EndTime, dbo.Repair.LinJianChengBen, dbo.Repair.GongShiJinE,dbo.Repair.LinJianJinE, dbo.Repair.GongShiDaZhe, dbo.Repair.LinJianDaZhe,dbo.FuKuangFangShi.FKFSName FROM dbo.CarInfo INNER JOIN dbo.Repair ON dbo.CarInfo.RepairID = dbo.Repair.RepairID INNER JOIN dbo.Car ON dbo.CarInfo.CarID = dbo.Car.CarID INNER JOIN dbo.Project ON dbo.Repair.ProjectID = dbo.Project.ProjectID INNER JOIN dbo.BanZhu ON dbo.Repair.BanZhuID = dbo.BanZhu.BanZhuID INNER JOIN dbo.Sa ON dbo.CarInfo.SaID = dbo.Sa.SaID INNER JOIN dbo.Chengben ON dbo.Repair.ChengBenID = dbo.Chengben.ChengBenID INNER JOIN dbo.FuKuangFangShi ON dbo.Repair.FKID = dbo.FuKuangFangShi.FKID";
string st_datafrom = YearFromList.SelectedItem.Value.ToString() + "-" + MouthFromList.SelectedItem.Value.ToString() + "-" + DayFromList.SelectedItem.Value.ToString();
string st_dateto = YearToList.SelectedItem.Value.ToString() + "-" + MouthToList.SelectedItem.Value.ToString() + "-" + DayToList.SelectedItem.Value.ToString();
st_selsql = st_selsql + " where EndTime between '" + st_datafrom + "'and'" + st_dateto + "'";
//读取方式记录
if (FKFSList.SelectedIndex == 1)
{
st_selsql = st_selsql + " and FKFSName like '客户自付'";
switch (CarNameList.SelectedIndex)
{
case 1:
{
st_selsql = st_selsql + " and CarName='国产车'";
if (InputTextBox.Text != "")
{
//按维修项目
if (StatusList.SelectedIndex == 1)
st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
//按班组
else if (StatusList.SelectedIndex == 2)
st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
//按接待人员
else if (StatusList.SelectedIndex == 3)
st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
}
break;
}
case 2:
{
st_selsql = st_selsql + " and CarName='进口车'";
if (InputTextBox.Text != "")
{
//按维修项目
if (StatusList.SelectedIndex == 1)
st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
//按班组
else if (StatusList.SelectedIndex == 2)
st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
//按接待人员
else if (StatusList.SelectedIndex == 3)
st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
}
break;
}
case 3:
{
st_selsql = st_selsql + " and CarName='非丰田车'";
if (InputTextBox.Text != "")
{
//按维修项目
if (StatusList.SelectedIndex == 1)
st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
//按班组
else if (StatusList.SelectedIndex == 2)
st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
//按接待人员
else if (StatusList.SelectedIndex == 3)
st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
}
break;
}
}
}
else
{
st_selsql = st_selsql + " and FKFSName not like '客户自付'";
switch (CarNameList.SelectedIndex)
{
case 1:
{
st_selsql = st_selsql + " and CarName='国产车'";
if (InputTextBox.Text != "")
{
//按维修项目
if (StatusList.SelectedIndex == 1)
st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
//按班组
else if (StatusList.SelectedIndex == 2)
st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
//按接待人员
else if (StatusList.SelectedIndex == 3)
st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
}
break;
}
case 2:
{
st_selsql = st_selsql + " and CarName='进口车'";
if (InputTextBox.Text != "")
{
//按维修项目
if (StatusList.SelectedIndex == 1)
st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
//按班组
else if (StatusList.SelectedIndex == 2)
st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
//按接待人员
else if (StatusList.SelectedIndex == 3)
st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
}
break;
}
case 3:
{
st_selsql = st_selsql + " and CarName='非丰田车'";
if (InputTextBox.Text != "")
{
//按维修项目
if (StatusList.SelectedIndex == 1)
st_selsql = st_selsql + " and ProjectName like'%" + InputTextBox.Text.Trim() + "%'";
//按班组
else if (StatusList.SelectedIndex == 2)
st_selsql = st_selsql + " and BanZhuName like'%" + InputTextBox.Text.Trim() + "%'";
//按接待人员
else if (StatusList.SelectedIndex == 3)
st_selsql = st_selsql + " and SaName like'%" + InputTextBox.Text.Trim() + "%'";
}
break;
}
}
}
//创建SqlDataAdapter对象,调用ST_selsql
SqlDataAdapter ST_myadapter = new SqlDataAdapter(st_selsql, conn);
//创建并填充DataSet
DataSet ST_ds = new DataSet();
ST_myadapter.Fill(ST_ds);
Show_CustomerList.DataSource = ST_ds;
Show_CustomerList.DataBind();//常规导出方法
SW = new ();
System.Web.UI.HtmlTextWriter HTW = new System.Web.UI.HtmlTextWriter(SW);
Show_CustomerList.RenderControl(HTW);
//Page为要导出的对象,当前是Page,如果是DataGrid,DataList等都可以
System.Web.HttpContext.Current.Response.Buffer = true;
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearContent();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";
//Response.ContentType是输出流的 HTTP MIME 类型
//Response.ContentType --- word文件
//application/vnd.ms-excel --- excel文件
//...
System.Web.HttpContext.Current.Response.Charset = "UTF-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
//System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + strFileName + ".xls");
//attachment --- 作为附件下载
//inline --- 在线打开
//filename如过是中文,则可以用HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8)
//进行进行编码,以解决文件名乱码的问题
System.Web.HttpContext.Current.Response.Write(SW.ToString());
System.Web.HttpContext.Current.Response.Flush();
System.Web.HttpContext.Current.Response.Close();