gridview导出excel,内容为空的。

terry21 2010-05-27 04:00:52
//Gridview中的内容导出到Excel
public override void VerifyRenderingInServerForm(Control control)
{
// Confirms that an HtmlForm control is rendered for
}
public void BtnExport_Click(object sender, EventArgs e)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
HttpContext.Current.Response.Charset = "gb2312";
HttpContext.Current.Response.ContentType = "application/ms-excel";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);

this.GridView1.AllowPaging = false;
this.getData();
this.GridView1.RenderControl(htmlWrite);

HttpContext.Current.Response.Write(stringWrite.ToString());
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
this.getData();
}

gridview导出excel,内容为空的。
请大家解答,谢谢。
...全文
153 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
pdsnet 2010-05-27
  • 打赏
  • 举报
回复
this.getData(); 执行别的去了..
terry21 2010-05-27
  • 打赏
  • 举报
回复
我把这个this.getData();去掉
居然神奇的导出成功了。
jack15850798154 2010-05-27
  • 打赏
  • 举报
回复
上面的问题你可以调试看看。同时检查一下数据库是否有数据。
一个公共方法:
/// <summary>
/// 生成EXCEL文件
/// _til是表格的标题,_row是表格的行数,_col是表格的列数,_dt是数据表,_filename是保存的EXCEL表的名称
/// </summary>
public void SaveToExcel(string _til, int _row, int _col, DataTable _dt, string _filename)
{
Microsoft.Office.Interop.Owc11.SpreadsheetClass xlsheet = new Microsoft.Office.Interop.Owc11.SpreadsheetClass();
//合并单元格
xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[1, _col]).set_MergeCells(true);
// xlsheet.get_Range(xlsheet.Cells[2, 1], xlsheet.Cells[3, 2]).set_MergeCells(true);
xlsheet.ActiveSheet.Cells[1, 1] = _til;
//字体加粗
xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[1, _col]).Font.set_Bold(true);
//单元格文本水平居中对齐
xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[_row + 2, _col]).set_HorizontalAlignment(Microsoft.Office.Interop.Owc11.XlHAlign.xlHAlignCenter);
//设置字体大小
xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[1, _col]).Font.set_Size(14);
//设置列宽
//xlsheet.get_Range(xlsheet.Cells[1, 3], xlsheet.Cells[1, 3]).set_ColumnWidth(50);

//画边框线
xlsheet.get_Range(xlsheet.Cells[1, 1], xlsheet.Cells[_row + 2, _col]).Borders.set_LineStyle(Microsoft.Office.Interop.Owc11.XlLineStyle.xlContinuous);

//定义一个2维数组用来存储DATATABLE里的数据
object[,] dataArray = new object[_row, _col];
//添加列名
for (int k = 0; k < _dt.Columns.Count; k++)
{
xlsheet.ActiveSheet.Cells[2, k + 1] = _dt.Columns[k].Caption.ToString();
}
//把DATATABLE里的数据导到2维数组中
for (int i = 0; i < _row; i++)
{
for (int j = 0; j < _col; j++)
{
dataArray[i, j] = _dt.Rows[i][j];
}
}
//把2维数组中的数据导到EXCEL中
xlsheet.get_Range("A3", xlsheet.Cells[_row + 2, _col]).Value2 = dataArray;
xlsheet.Export(_filename, Microsoft.Office.Interop.Owc11.SheetExportActionEnum.ssExportActionNone, Microsoft.Office.Interop.Owc11.SheetExportFormat.ssExportXMLSpreadsheet);
}
/// <summary>
/// 导入EXCEL表
/// </summary>
/// <param name="strFileName"></param>
/// <returns></returns>
public DataSet CreateDataSource(string strFileName, string Sqlstr)
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strFileName + "; Extended Properties=Excel 8.0;";
OleDbConnection olecon = new OleDbConnection(strCon);
OleDbDataAdapter myda = new OleDbDataAdapter(Sqlstr, olecon);
DataSet myds = new DataSet();
myda.Fill(myds);
return myds;
}
小黑哥gs 2010-05-27
  • 打赏
  • 举报
回复
给你个方法。。可以直接把GV导为EXCEL。。

/// <summary>
/// 导出Exel
/// </summary>
/// <param name="ctl">导出数据的控件载体</param>
/// <param name="FileName">导出Exel的默认名字</param>
public static void ToExcel(System.Web.UI.Control ctl, string FileName)
{

HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + "" + FileName + ".xls");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
}
terry21 2010-05-27
  • 打赏
  • 举报
回复
<asp:GridView ID="GridView1" runat="server"
OnRowDataBound="GridView1_RowDataBound"
BackColor="White" BorderColor="White" BorderStyle="None" BorderWidth="2px" CellPadding="3"
CellSpacing="1" GridLines="None" Font-Size="12px" HorizontalAlign="Center">
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<RowStyle BackColor="#E8F5FE" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E8E8FF" Font-Bold="True" ForeColor="Black" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<HeaderStyle BackColor="#006699" Font-Bold="False" ForeColor="White" Wrap="False" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
</asp:GridView>
<asp:Button ID="BtnExport" runat="server" OnClick="BtnExport_Click" Text="Export to Excel" />

前台代码

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧