关于将网页上的GridView中的数据导出一个Excel遇到的有关问题(解决有关问题送50分)
关于将网页上的GridView中的数据导出一个Excel遇到的问题(解决问题送50分)
本人VS2010 C# 开发的系统的某个网页上有一个GridView控件显示查询出的数据,使用以下一个函数导出一个Excel表,本地机安装的是office 2010,当打开该导出的Excel文件时,系统提示说:您尝试打开的文件“FileName.xls”的格式与文件扩展名指定的格式不一致。打开文件前请验证文件没有损坏且来源可靠。是否立刻打开该文件?
当点击是(Y)后,文件可以打开,数据也对,另存为Excel文档后,打开新保存的文件正常。请各位帮忙看看,如何修改方能直接保存成正确的Excel文件,以下是导出的函数和参数。(网上查了下要第三方工具,不知咋搞)
public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
点击“to Excel” 按钮
protected void btnExcel_Click(object sender, EventArgs e)
{
try
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
// Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("孟宪会Excel表格测试") + ".xml");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dgvMain.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch
{
}
finally
{
}
}
------解决思路----------------------
这个类是绝对可用的,而且我已经成功的把它放到winform的工程里,也是可用的
------解决思路----------------------
你可以用oledb导出的方式来导出,
HttpContext.Current.Response.有用到这种方法的导出的都是强制命名成xls的,你用记事本打开显示正常,真正的xls文件用记事本打开显示的是乱码
------解决思路----------------------
把Sql Server数据库数据查询结果导出到Excel文档
// 定义全局变量及对象
string M_str_Con = "Data Source=mrwxk\\wangxiaoke;Database=db_TomeOne;Uid=sa;Pwd=;";//定义数据库连接字符串
SqlConnection sqlcon;//声明数据库连接对象
SqlCommand sqlcmd;//声明执行命令对象
SqlDataAdapter sqlda;//声明数据桥接器对象
DataSet myds;//声明数据集对象
#endregion
private void Frm_Main_Load(object sender, EventArgs e)
{
cbox_Condition.SelectedIndex = 0;//默认选择条件为第一项
dgv_Info.DataSource = SelectEInfo("", "").Tables[0];//将数据库中的数据全部显示在数据表格控件中
}
private void btn_Query_Click(object sender, EventArgs e)
{
dgv_Info.DataSource = SelectEInfo(cbox_Condition.Text, txt_KeyWord.Text).Tables[0];//按条件查询数据
}
private void btn_Excel_Click(object sender, EventArgs e)
本人VS2010 C# 开发的系统的某个网页上有一个GridView控件显示查询出的数据,使用以下一个函数导出一个Excel表,本地机安装的是office 2010,当打开该导出的Excel文件时,系统提示说:您尝试打开的文件“FileName.xls”的格式与文件扩展名指定的格式不一致。打开文件前请验证文件没有损坏且来源可靠。是否立刻打开该文件?
当点击是(Y)后,文件可以打开,数据也对,另存为Excel文档后,打开新保存的文件正常。请各位帮忙看看,如何修改方能直接保存成正确的Excel文件,以下是导出的函数和参数。(网上查了下要第三方工具,不知咋搞)
public static void GridViewToExcel(Control ctrl, string FileType, string FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition",
"attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
ctrl.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
ctrl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
点击“to Excel” 按钮
protected void btnExcel_Click(object sender, EventArgs e)
{
try
{
Response.ClearContent();
Response.AddHeader("content-disposition", "attachment; filename=MyExcelFile.xls");
// Response.AppendHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode("孟宪会Excel表格测试") + ".xml");
Response.ContentType = "application/excel";
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
dgvMain.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}
catch
{
}
finally
{
}
}
------解决思路----------------------
这个类是绝对可用的,而且我已经成功的把它放到winform的工程里,也是可用的
------解决思路----------------------
你可以用oledb导出的方式来导出,
HttpContext.Current.Response.有用到这种方法的导出的都是强制命名成xls的,你用记事本打开显示正常,真正的xls文件用记事本打开显示的是乱码
------解决思路----------------------
把Sql Server数据库数据查询结果导出到Excel文档
// 定义全局变量及对象
string M_str_Con = "Data Source=mrwxk\\wangxiaoke;Database=db_TomeOne;Uid=sa;Pwd=;";//定义数据库连接字符串
SqlConnection sqlcon;//声明数据库连接对象
SqlCommand sqlcmd;//声明执行命令对象
SqlDataAdapter sqlda;//声明数据桥接器对象
DataSet myds;//声明数据集对象
#endregion
private void Frm_Main_Load(object sender, EventArgs e)
{
cbox_Condition.SelectedIndex = 0;//默认选择条件为第一项
dgv_Info.DataSource = SelectEInfo("", "").Tables[0];//将数据库中的数据全部显示在数据表格控件中
}
private void btn_Query_Click(object sender, EventArgs e)
{
dgv_Info.DataSource = SelectEInfo(cbox_Condition.Text, txt_KeyWord.Text).Tables[0];//按条件查询数据
}
private void btn_Excel_Click(object sender, EventArgs e)