积极答复者
如何把GridView模板中的内容导入到Excel

问题
-
以下是把GridView的数据导入到Excel表的代码,有点特别的是,这个GridView中大量使用了模板。因此,必须先要判断模板的类型,再把模板的内容写到str字段里,最后写到Excel对应的Cell中。为了方便测试,我把模板都做成TextBox,可是每次运行到 if (GridView_Search.Controls[j].GetType() == typeof(TextBox)) 这一句时,就会出现异常(见图1)。
我尝试把GridView_Search.Controls[j].GetType()改成GridView_Search.Columns[j].GetType(),但还是读取不到数据,我到底错在哪里了?下面有完整的前、后台代码及错误提示,后台代码的注释中还有我调试的结果!请各位帮忙看看,谢谢!
-----------------------前台----------------------<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default3.aspx.cs" Inherits="Default3" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> <style type="text/css"> .style1 { width: 100%; } </style> </head> <body> <form id="form1" runat="server"> <div> <table cellpadding="0" cellspacing="0" class="style1"> <tr> <td> <asp:Button ID="Button_SQLToExcel" runat="server" onclick="Button_SQLToExcel_Click" Text="导入到Excel表" /></td> </tr> <tr> <td> <asp:GridView ID="GridView_Search" runat="server" AutoGenerateColumns="False" CellPadding="0" DataSourceID="SqlDataSource1"> <Columns> <asp:TemplateField HeaderText="SupplierID" SortExpression="SupplierID"> <ItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SupplierID") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="CategoryID" SortExpression="CategoryID"> <ItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("CategoryID") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="QuantityPerUnit" SortExpression="QuantityPerUnit"> <ItemTemplate> <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("QuantityPerUnit") %>'></asp:TextBox> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [SupplierID], [CategoryID], [QuantityPerUnit] FROM [Alphabetical list of products]"> </asp:SqlDataSource> </td> </tr> </table> </div> </form> </body> </html>
-------------------------后台-------------------------
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Collections; using System.Configuration; using System.Data; using System.Web.Security; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; using Word = Microsoft.Office.Interop.Word; using System.Threading; using office = Microsoft.Office.Core; using System.Reflection; using System.IO; using System.Text.RegularExpressions; using System.Text; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button_SQLToExcel_Click(object sender, EventArgs e) { if (GridView_Search.Rows.Count == 0) return; Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application(); excel.Application.Workbooks.Add(true); excel.Visible = true; for (int i = 0; i < GridView_Search.Columns.Count; i++) { excel.Cells[1, i + 1] = GridView_Search.Columns[i].HeaderText; } for (int i = 0; i < GridView_Search.Rows.Count; i++) { for (int j = 0; j < GridView_Search.Columns.Count; j++) { //if (GridView_Search.Columns[j].GetType() == typeof(TextBox))//这样写不会出现异常,但也是只有Excel的标题行,内容都没有 //Response.Write(GridView_Search.Columns[j].GetType());//得到的是System.Web.UI.WebControls.TemplateField if (GridView_Search.Controls[j].GetType() == typeof(TextBox)) { string str = (GridView_Search.Controls[j] as TextBox).Text; excel.Cells[i + 2, j + 1] = str; } else { excel.Cells[i + 2, j + 1] = GridView_Search.Rows[i].Cells[j].Text; } //if (GridView_Search.Controls[j].GetType() == typeof(TextBox)) //{ // string str = (GridView_Search.Controls[j] as TextBox).Text; // excel.Cells[i + 2, j + 1] = str; //} //else //{ // excel.Cells[i + 2, j + 1] = GridView_Search.Rows[i].Cells[j].Text; //} } } } }
-------------------------------图1--------------------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
答案
-
1)代码只是大概的示例你需要仔细理解一会好很多的 因为我给的代码片段不一定可以连起来的 ..要做适当的修改
上面我也说错了 其实那几句话不应该注释掉 具体代码如下 这回仔细看下 很多地方下面可以优化的!
public void toexcel(System.Web.UI.WebControls.DataGrid datagrid) { System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Write("<html xmlns:x=/"urn:schemas-microsoft-com:office:excel/">"); System.Web.HttpContext.Current.Response.Write("<head>"); System.Web.HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("<x:Name>sheet1</x:Name>"); System.Web.HttpContext.Current.Response.Write("<x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("<x:Print>"); System.Web.HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>"); System.Web.HttpContext.Current.Response.Write("</x:Print>"); System.Web.HttpContext.Current.Response.Write("</x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("</xml>"); System.Web.HttpContext.Current.Response.Write("<![endif]--> "); System.Web.HttpContext.Current.Response.Write("</head>"); System.Web.HttpContext.Current.Response.Write("<body>"); System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Now.ToFileTime() + ".xls"); System.Web.HttpContext.Current.Response.Charset = "UTF-8"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"; //loggrid.Page.EnableViewState =false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); datagrid.RenderControl(hw); System.Web.HttpContext.Current.Response.Write(tw.ToString()); System.Web.HttpContext.Current.Response.Write("<font style=/"color:white;/">.</font></body>"); System.Web.HttpContext.Current.Response.Write("</html>"); System.Web.HttpContext.Current.Response.End(); }
GridView GV = new GridView();//一个无分页的GridView
2) 把上面GV直接改成你页面那个Gridview就可以了 不需要用其他的。
认真的活,认真的爱!
- 已标记为答案 Song TianModerator 2012年8月15日 9:01
全部回复
-
private void ExportExcel() { HttpContext curContext = System.Web.HttpContext.Current; System.IO.StringWriter strWriter = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter); curContext.Response.ContentType = "application/vnd.ms-excel"; curContext.Response.ContentEncoding = Encoding.GetEncoding("GB2312"); curContext.Response.Charset = "GB2312"; GridView GV = new GridView();//一个无分页的GridView GV.DataSource = SqlDataSource1; GV.AllowPaging = false; GV.DataBind(); GV.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); }
最简单的一种导出格式和gridview 一样的认真的活,认真的爱!
-
谢谢您的答复!
但我没看懂!面前的电脑没装Excel,我也无法测试。因此想问问:
问题1:需要引用什么特殊的命名空间么?
问题2:我平时用的是Excel2003,适用么?
问题3:我做的界面是一个GridView配一个SqlDataSource,您提示的这段代码似乎是在后台新建了一个GV。GridView GV=new GridView(); 我只要把GV.DataSource=SqlDataSource1; 改成我的GV.DataSource=SqlDataSource_Search就可以了,是吧?
明天再去电脑上试试才知道!再次感谢您的及时解答!Happy!
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
尝试了您提供的代码,我做了个Button键
protected void Button1_Click(object sender, EventArgs e) { ExportExcel(); }
当我点击Button键,出现如图所示,生成了一个Deafult.aspx文件,下面是它的代码,这不是导出到Excel吧?
<div> <table cellspacing="0" rules="all" border="1" style="border-collapse:collapse;"> <tr> <th scope="col">SupplierID</th><th scope="col">CategoryID</th><th scope="col">QuantityPerUnit</th> </tr><tr> <td>1</td><td>1</td><td>10 boxes x 20 bags</td> </tr><tr> <td>1</td><td>1</td><td>24 - 12 oz bottles</td> </tr><tr> <td>1</td><td>2</td><td>12 - 550 ml bottles</td> </tr><tr> <td>2</td><td>2</td><td>48 - 6 oz jars</td> </tr><tr> <td>3</td><td>2</td><td>12 - 8 oz jars</td> </tr><tr> <td>3</td><td>7</td><td>12 - 1 lb pkgs.</td> </tr><tr> <td>3</td><td>2</td><td>12 - 12 oz jars</td> </tr><tr> <td>4</td><td>8</td><td>12 - 200 ml jars</td> </tr><tr> <td>5</td><td>4</td><td>1 kg pkg.</td> </tr><tr> <td>5</td><td>4</td><td>10 - 500 g pkgs.</td> </tr><tr> <td>6</td><td>8</td><td>2 kg box</td> </tr><tr> <td>6</td><td>7</td><td>40 - 100 g pkgs.</td> </tr><tr> <td>6</td><td>2</td><td>24 - 250 ml bottles</td> </tr><tr> <td>7</td><td>3</td><td>32 - 500 g boxes</td> </tr><tr> <td>7</td><td>8</td><td>16 kg pkg.</td> </tr><tr> <td>8</td><td>3</td><td>10 boxes x 12 pieces</td> </tr><tr> <td>8</td><td>3</td><td>30 gift boxes</td> </tr><tr> <td>8</td><td>3</td><td>24 pkgs. x 4 pieces</td> </tr><tr> <td>9</td><td>5</td><td>24 - 500 g pkgs.</td> </tr><tr> <td>9</td><td>5</td><td>12 - 250 g pkgs.</td> </tr><tr> <td>11</td><td>3</td><td>20 - 450 g glasses</td> </tr><tr> <td>11</td><td>3</td><td>100 - 250 g bags</td> </tr><tr> <td>11</td><td>3</td><td>100 - 100 g pieces</td> </tr><tr> <td>13</td><td>8</td><td>10 - 200 g glasses</td> </tr><tr> <td>14</td><td>4</td><td>12 - 100 g pkgs</td> </tr><tr> <td>14</td><td>4</td><td>24 - 200 g pkgs.</td> </tr><tr> <td>15</td><td>4</td><td>500 g</td> </tr><tr> <td>16</td><td>1</td><td>24 - 12 oz bottles</td> </tr><tr> <td>16</td><td>1</td><td>24 - 12 oz bottles</td> </tr><tr> <td>17</td><td>8</td><td>24 - 250 g jars</td> </tr><tr> <td>17</td><td>8</td><td>12 - 500 g pkgs.</td> </tr><tr> <td>18</td><td>1</td><td>12 - 75 cl bottles</td> </tr><tr> <td>18</td><td>1</td><td>750 cc per bottle</td> </tr><tr> <td>19</td><td>8</td><td>24 - 4 oz tins</td> </tr><tr> <td>19</td><td>8</td><td>12 - 12 oz cans</td> </tr><tr> <td>20</td><td>1</td><td>16 - 500 g tins</td> </tr><tr> <td>20</td><td>2</td><td>20 - 2 kg bags</td> </tr><tr> <td>21</td><td>8</td><td>1k pkg.</td> </tr><tr> <td>21</td><td>8</td><td>4 - 450 g glasses</td> </tr><tr> <td>22</td><td>3</td><td>10 - 4 oz boxes</td> </tr><tr> <td>22</td><td>3</td><td>10 pkgs.</td> </tr><tr> <td>23</td><td>3</td><td>24 - 50 g pkgs.</td> </tr><tr> <td>23</td><td>3</td><td>12 - 100 g bars</td> </tr><tr> <td>24</td><td>7</td><td>50 - 300 g pkgs.</td> </tr><tr> <td>24</td><td>5</td><td>16 - 2 kg boxes</td> </tr><tr> <td>25</td><td>6</td><td>16 pies</td> </tr><tr> <td>25</td><td>6</td><td>24 boxes x 2 pies</td> </tr><tr> <td>26</td><td>5</td><td>24 - 250 g pkgs.</td> </tr><tr> <td>26</td><td>5</td><td>24 - 250 g pkgs.</td> </tr><tr> <td>27</td><td>8</td><td>24 pieces</td> </tr><tr> <td>28</td><td>4</td><td>5 kg pkg.</td> </tr><tr> <td>28</td><td>4</td><td>15 - 300 g rounds</td> </tr><tr> <td>29</td><td>2</td><td>24 - 500 ml bottles</td> </tr><tr> <td>29</td><td>3</td><td>48 pies</td> </tr><tr> <td>7</td><td>2</td><td>15 - 625 g jars</td> </tr><tr> <td>12</td><td>5</td><td>20 bags x 4 pieces</td> </tr><tr> <td>2</td><td>2</td><td>32 - 8 oz bottles</td> </tr><tr> <td>2</td><td>2</td><td>24 - 8 oz jars</td> </tr><tr> <td>16</td><td>1</td><td>24 - 12 oz bottles</td> </tr><tr> <td>8</td><td>3</td><td>10 boxes x 8 pieces</td> </tr><tr> <td>15</td><td>4</td><td>10 kg pkg.</td> </tr><tr> <td>7</td><td>1</td><td>24 - 355 ml bottles</td> </tr><tr> <td>15</td><td>4</td><td>10 - 500 g pkgs.</td> </tr><tr> <td>14</td><td>4</td><td>24 - 200 g pkgs.</td> </tr><tr> <td>17</td><td>8</td><td>24 - 150 g jars</td> </tr><tr> <td>4</td><td>7</td><td>5 kg pkg.</td> </tr><tr> <td>12</td><td>1</td><td>24 - 0.5 l bottles</td> </tr><tr> <td>23</td><td>1</td><td>500 ml</td> </tr><tr> <td>12</td><td>2</td><td>12 boxes</td> </tr> </table> </div>
----------------------------图-----------------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
刚才忽然灵机一动用Excel打开Default(1).aspx,成功了!于是要多问两个问题!
问题1:如何点击Button时,可以生成Excel文件,而非aspx文件呢?
问题2:我的Sql数据库中列都是英文的,但导出到Excel中的各列都要是中文的,有办法解决么?
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2012年8月10日 2:05
-
1、加入下面的代码导出的就会是excel的文件了
System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Buffer = true; System.Web.HttpContext.Current.Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">"); System.Web.HttpContext.Current.Response.Write("<head>"); System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");//解决中文乱码问题,导出数据在20行内容易出现乱码 System.Web.HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("<x:Name>sheet1</x:Name>"); System.Web.HttpContext.Current.Response.Write("<x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("<x:Print>"); System.Web.HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>"); System.Web.HttpContext.Current.Response.Write("</x:Print>"); System.Web.HttpContext.Current.Response.Write("</x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("</xml>"); System.Web.HttpContext.Current.Response.Write("<![endif]--> ");
2、他导出的是根据你的gridview导出的 和数据库没关系 所以你可以在gridview设置别名!认真的活,认真的爱!
-
1、加入下面的代码导出的就会是excel的文件了
请问一下,这一段是加在private void ExprotExcel() 的什么位置呢?System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Buffer = true; System.Web.HttpContext.Current.Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">"); System.Web.HttpContext.Current.Response.Write("<head>"); System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");//解决中文乱码问题,导出数据在20行内容易出现乱码 System.Web.HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("<x:Name>sheet1</x:Name>"); System.Web.HttpContext.Current.Response.Write("<x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("<x:Print>"); System.Web.HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>"); System.Web.HttpContext.Current.Response.Write("</x:Print>"); System.Web.HttpContext.Current.Response.Write("</x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("</xml>"); System.Web.HttpContext.Current.Response.Write("<![endif]--> ");
2、他导出的是根据你的gridview导出的 和数据库没关系 所以你可以在gridview设置别名!
认真的活,认真的爱!
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
-
问题1:替换前(图1),程序会提示要下载;替换后(图2)不提示下载,变成网页的摸样,不会提示下载生成的文件。我是要导出到Excel中啊,哪里错了?代码1是我替换后的代码!
问题2:我设置了HeadText为中文,但导出来的Excel表,表头还是英文的啊,应该在GridView GV = new GridView(); GV.DataSource = SqlDataSource1; 后面还要加段关于HeadText的代码吧?怎么加呢?我不会!
另外,每一列我都有ShowFooter做合计的,导出来的Excel表没有Footer,要怎么弄呢?-----------------------代码1-------------------------
private void ExportExcel() { HttpContext curContext = System.Web.HttpContext.Current; System.IO.StringWriter strWriter = new StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(strWriter); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Buffer = true; System.Web.HttpContext.Current.Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">"); System.Web.HttpContext.Current.Response.Write("<head>"); System.Web.HttpContext.Current.Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");//解决中文乱码问题,导出数据在20行内容易出现乱码 System.Web.HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("<x:Name>sheet1</x:Name>"); System.Web.HttpContext.Current.Response.Write("<x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("<x:Print>"); System.Web.HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>"); System.Web.HttpContext.Current.Response.Write("</x:Print>"); System.Web.HttpContext.Current.Response.Write("</x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("</xml>"); System.Web.HttpContext.Current.Response.Write("<![endif]--> "); //curContext.Response.ContentType = "application/vnd.ms-excel"; //curContext.Response.ContentEncoding = Encoding.GetEncoding("GB2312"); //curContext.Response.Charset = "GB2312"; GridView GV = new GridView();//一个无分页的GridView GV.DataSource = SqlDataSource1; GV.AllowPaging = false; GV.DataBind(); GV.RenderControl(htmlWriter); curContext.Response.Write(strWriter.ToString()); curContext.Response.End(); }
------------------------------图1(未替换前)--------------------------
--------------------------------图2(替换后)--------------------------------
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!
- 已编辑 linjiangxian11 2012年8月14日 6:57
-
1)代码只是大概的示例你需要仔细理解一会好很多的 因为我给的代码片段不一定可以连起来的 ..要做适当的修改
上面我也说错了 其实那几句话不应该注释掉 具体代码如下 这回仔细看下 很多地方下面可以优化的!
public void toexcel(System.Web.UI.WebControls.DataGrid datagrid) { System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Write("<html xmlns:x=/"urn:schemas-microsoft-com:office:excel/">"); System.Web.HttpContext.Current.Response.Write("<head>"); System.Web.HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("<x:Name>sheet1</x:Name>"); System.Web.HttpContext.Current.Response.Write("<x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("<x:Print>"); System.Web.HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>"); System.Web.HttpContext.Current.Response.Write("</x:Print>"); System.Web.HttpContext.Current.Response.Write("</x:WorksheetOptions>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheet>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheets>"); System.Web.HttpContext.Current.Response.Write("</x:ExcelWorkbook>"); System.Web.HttpContext.Current.Response.Write("</xml>"); System.Web.HttpContext.Current.Response.Write("<![endif]--> "); System.Web.HttpContext.Current.Response.Write("</head>"); System.Web.HttpContext.Current.Response.Write("<body>"); System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Now.ToFileTime() + ".xls"); System.Web.HttpContext.Current.Response.Charset = "UTF-8"; System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default; System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel"; //loggrid.Page.EnableViewState =false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); datagrid.RenderControl(hw); System.Web.HttpContext.Current.Response.Write(tw.ToString()); System.Web.HttpContext.Current.Response.Write("<font style=/"color:white;/">.</font></body>"); System.Web.HttpContext.Current.Response.Write("</html>"); System.Web.HttpContext.Current.Response.End(); }
GridView GV = new GridView();//一个无分页的GridView
2) 把上面GV直接改成你页面那个Gridview就可以了 不需要用其他的。
认真的活,认真的爱!
- 已标记为答案 Song TianModerator 2012年8月15日 9:01
-
GridView GV = new GridView();//一个无分页的GridView
2) 把上面GV直接改成你页面那个Gridview就可以了 不需要用其他的。
您提供的最后这一段新代码,哪里有 GridView GV=new GridView(); 这一句话啊?
是这样的么?面前的电脑无法尝试,只好先问问了 ,呵呵!
protected void Button1_Click(object sender, EventArgs e) { toexcel(GirdView_Search); }
C# 菜鸟中的雏鸟!提的问题也许很幼稚,但我是认真的。希望看在党国的面子上拉兄弟一把!