gridview导出数据到excel gridview 导出数据到excel 如何在代码中设置excel的页眉和页脚?如何设置页面设置中的显示是横向还是纵向? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个好像有点难吧,好像不好控制,改用excel组件看行不? 没用过excel 只要能帮我解决问题就行 用VSTO吧。这个问题已经被问及N遍了(N>int32.Max),可以在网上随便一搜。 打开EXCEL模板赋值导出文件string filePath = Server.MapPath("");FileStream fs = new FileStream(filePath, FileMode.Open);byte[] bytes = new byte[(int)fs.Length];fs.Read(bytes, 0, bytes.Length);fs.Close();Response.ContentType = "application/octet-stream";Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));Response.BinaryWrite(bytes);Response.Flush();Response.End();或VSTO开发 你先做一个EXCEL模板行不?然后再根据这个模板进行导出 我导出excel代码: public static void GridViewToExcel(Control gv, string FileType, string FileName) { HttpContext.Current.Response.Charset = "GB2312"; HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;//注意编码 HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString()); HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword gv.Page.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); gv.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End();} AppLogic.Common.Base.ExportExcelTable(GridView1, "车种统计_"); public static void ExportExcelTable(GridView gv, string strName) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = "GB2312"; string fileName = strName + "-" + DateTime.Now.ToString("yyyyMMddhhssff") + ".xls"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString()); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); gv.RenderControl(oHtmlTextWriter); //string a1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\" />\n<style>\n</style>\n</head>\n<body>\n"; //string a2 = "\n</body>\n</html>"; HttpContext.Current.Response.Output.Write(oStringWriter.ToString()); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } public static void ExportExcelTable(string html, string strName, string charSet) { HttpContext.Current.Response.Clear(); HttpContext.Current.Response.Buffer = true; HttpContext.Current.Response.Charset = charSet; string fileName = strName + "-" + DateTime.Now.ToString("yyyyMMddhhssff") + ".xls"; HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString()); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!! HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(charSet); //HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8; HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 //string a1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\" />\n<style>\n</style>\n</head>\n<body>\n"; //string a2 = "\n</body>\n</html>"; HttpContext.Current.Response.Output.Write(html); HttpContext.Current.Response.Flush(); HttpContext.Current.Response.End(); } < DataNavigateUrlFields="sno" HeaderText="学号" DataTextField="sno"> 再添加一个按钮,用于触发导出事件:<asp:LinkButton ID="lbtnexcel" runat="server" Text="导出到Excel" OnClick="lbtnexcel_Click"></asp:LinkButton>以上为aspx页面代码后台代码则是:protected void lbtnexcel_Click(object sender, EventArgs e) { gridview1.BottomPagerRow.Visible = false;//导出到Excel表后,隐藏分页部分 gridview1.Columns[9].Visible = false;//隐藏“编辑”列 gridview1.Columns[10].Visible = false;//隐藏“删除”列 DateTime dt = DateTime.Now;//给导出后的Excel表命名,结合表的用途以及系统时间来命名 string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString(); /*如导出的表中有某些列为编号、身份证号之类的纯数字字符串,如不进行处理,则导出的数据会默认为数字,例如原字符串"0010"则会变为数字10,字符串"1245787888"则会变为科学计数法1.236+E9,这样便达不到我们想要的结果,所以需要在导出前对相应列添加格式化的数据类型,以下为格式化为字符串型*/ foreach (GridViewRow dg in this.gridview1.Rows) { dg.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat: @;"); dg.Cells[5].Attributes.Add("style", "vnd.ms-excel.numberformat: @;"); dg.Cells[6].Attributes.Add("style", "vnd.ms-excel.numberformat: @;"); dg.Cells[8].Attributes.Add("style", "vnd.ms-excel.numberformat: @;"); } Response.Clear(); Response.AddHeader("Content- Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(" 学生表" + filename, System.Text.Encoding.UTF8) + ".xls");//导出文件命名 Response.ContentEncoding = System.Text.Encoding.UTF7;//如果设置为"GB2312"则中文字符可能会乱码 Response.ContentType = "applicationshlnd.xls"; System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); Panel1.RenderControl(oHtmlTextWriter);//Add the Panel into the output Stream. Response.Write(oStringWriter.ToString());//Output the stream. Response.Flush(); Response.End(); } //重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内” public override void VerifyRenderingInServerForm(Control control) { //override VerifyRenderingInServerForm. } excel问我就好了//引入excel组件using Excel = Microsoft.Office.Interop.Excel;using Microsoft.Office.Interop.Excel; ApplicationClass excel; _Workbook xBk; _Worksheet xSt;excel = new ApplicationClass(); xBk = excel.Workbooks.Add(true); xSt = (_Worksheet)xBk.ActiveSheet;xSt.Name = "我的第一张表"//设置工作本名称//设置打印方向 //xSt.PageSetup.Orientation = XlPageOrientation.xlLandscape;excel.Cells[1, 1] = "Jan"//设置第一行第一列内容为"Jan" string strFileName = Server.MapPath("Uploads/qafiles/") + sTimes + ".xls"; xBk.SaveCopyAs(strFileName); xBk.Close(false, null, null); excel.Quit(); System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk); System.Runtime.InteropServices.Marshal.ReleaseComObject(excel); System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt); xBk = null; excel = null; xSt = null; GC.Collect(); file = new System.IO.FileInfo(strFileName); Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.UTF8; Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name)); Response.AddHeader("Content-Length", file.Length.ToString()); Response.ContentType = "application/ms-excel"; Response.WriteFile(file.FullName); Response.End(); 小小建议用dsoframer.ocx以JS编写脚本目前我也在搞这个LZ要是搞定了的话请告知方法好不》?cxx2676@126。com谢谢 十六进制形式字符串转int怎么转! 关于重写请教为什么本机可以服务器上不行 c#开发activex---关闭ie不能释放activex资源 导入word中的几个问题 VS2003DataGrid的排序功能 rbac更新的SQL语句 Response.Redirect转向什么那么慢!!!!!!!! 关于table1.rows(0).cells(0).innerhtml=的问题 帮忙灌水:如何发布.aspx文件 想和大家探讨一个话题:关于美工修饰 asp.net远程连接数据库的小问题。 谁有日月年联动下拉框啊
string filePath = Server.MapPath("");
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
或VSTO开发
然后再根据这个模板进行导出
public static void GridViewToExcel(Control gv, string FileType, string FileName)
{
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;//注意编码
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
HttpContext.Current.Response.ContentType = FileType;//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
gv.Page.EnableViewState = false;
StringWriter tw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
gv.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312"; string fileName = strName + "-" + DateTime.Now.ToString("yyyyMMddhhssff") + ".xls";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString()); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
gv.RenderControl(oHtmlTextWriter); //string a1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\" />\n<style>\n</style>\n</head>\n<body>\n"; //string a2 = "\n</body>\n</html>"; HttpContext.Current.Response.Output.Write(oStringWriter.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
} public static void ExportExcelTable(string html, string strName, string charSet)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = charSet; string fileName = strName + "-" + DateTime.Now.ToString("yyyyMMddhhssff") + ".xls";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, Encoding.UTF8).ToString()); // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding(charSet);
//HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 //string a1 = "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title>Datos</title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=GB2312\" />\n<style>\n</style>\n</head>\n<body>\n"; //string a2 = "\n</body>\n</html>"; HttpContext.Current.Response.Output.Write(html);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
< DataNavigateUrlFields="sno" HeaderText="学号" DataTextField="sno">
再添加一个按钮,用于触发导出事件:
<asp:LinkButton ID="lbtnexcel" runat="server" Text="导出到Excel" OnClick="lbtnexcel_Click"></asp:LinkButton>
以上为aspx页面代码后台代码则是:protected void lbtnexcel_Click(object sender, EventArgs e)
{
gridview1.BottomPagerRow.Visible = false;//导出到Excel表后,隐藏分页部分
gridview1.Columns[9].Visible = false;//隐藏“编辑”列
gridview1.Columns[10].Visible = false;//隐藏“删除”列
DateTime dt = DateTime.Now;//给导出后的Excel表命名,结合表的用途以及系统时间来命名
string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString();
/*如导出的表中有某些列为编号、身份证号之类的纯数字字符串,如不进行处理,则导出的数据会默认为数字,例如原字符串"0010"则会变为数字10,字符串"1245787888"则会变为科学计数法1.236+E9,这样便达不到我们想要的结果,所以需要在导出前对相应列添加格式化的数据类型,以下为格式化为字符串型*/
foreach (GridViewRow dg in this.gridview1.Rows)
{
dg.Cells[0].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[5].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[6].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
dg.Cells[8].Attributes.Add("style", "vnd.ms-excel.numberformat: @;");
}
Response.Clear();
Response.AddHeader("Content- Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(" 学生表" + filename, System.Text.Encoding.UTF8) + ".xls");//导出文件命名
Response.ContentEncoding = System.Text.Encoding.UTF7;//如果设置为"GB2312"则中文字符可能会乱码
Response.ContentType = "applicationshlnd.xls";
System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
Panel1.RenderControl(oHtmlTextWriter);//Add the Panel into the output Stream.
Response.Write(oStringWriter.ToString());//Output the stream.
Response.Flush();
Response.End();
}
//重载VerifyRenderingInServerForm方法,否则运行的时候会出现如下错误提示:“类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内”
public override void VerifyRenderingInServerForm(Control control)
{
//override VerifyRenderingInServerForm.
}
using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel; ApplicationClass excel;
_Workbook xBk;
_Worksheet xSt;
excel = new ApplicationClass();
xBk = excel.Workbooks.Add(true);
xSt = (_Worksheet)xBk.ActiveSheet;
xSt.Name = "我的第一张表"//设置工作本名称
//设置打印方向
//xSt.PageSetup.Orientation = XlPageOrientation.xlLandscape;
excel.Cells[1, 1] = "Jan"//设置第一行第一列内容为"Jan"
string strFileName = Server.MapPath("Uploads/qafiles/") + sTimes + ".xls";
xBk.SaveCopyAs(strFileName); xBk.Close(false, null, null); excel.Quit();
System.Runtime.InteropServices.Marshal.ReleaseComObject(xBk);
System.Runtime.InteropServices.Marshal.ReleaseComObject(excel);
System.Runtime.InteropServices.Marshal.ReleaseComObject(xSt);
xBk = null;
excel = null;
xSt = null;
GC.Collect(); file = new System.IO.FileInfo(strFileName);
Response.Clear();
Response.Charset = "GB2312";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(file.Name));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/ms-excel";
Response.WriteFile(file.FullName);
Response.End();
用dsoframer.ocx
以JS编写脚本
目前我也在搞这个
LZ要是搞定了的话
请告知方法好不》?
cxx2676@126。com
谢谢