导出到Word和导出到Excel的问题

wyb3204 2009-07-01 07:02:03
public partial class printlist : System.Web.UI.Page
{

protected void Page_Load(object sender, System.EventArgs e)
{
if (Session["SQL"] == null || Session["SQL"].ToString() == "")
{
Response.Write("打印报表失败,没有指定SQL");
}
if (Request["TITLE"] != null)
{
LB_Title.Text = Request["TITLE"].ToString();
}

DBHelper helper = new DBHelper();
IDataReader Reader = helper.ExecuteReader(Session["SQL"].ToString());
DataGrid1.DataSource = Reader;
DataGrid1.DataBind();
Reader.Close(); Reader.Dispose();
}

private void ToExcel(System.Web.UI.Control ctl)
{
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-excel";

ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);

ctl.RenderControl(hw);

Response.Write(tw.ToString());
Response.End();
hw.Close();
tw.Close();
}
private void ToWord(System.Web.UI.Control ctl)
{
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=word.doc");
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/ms-word";

ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);

ctl.RenderControl(hw);

Response.Write(tw.ToString());
Response.End();
hw.Close();
tw.Close();
}

#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{

}
#endregion

protected void BT_ToExcel_Click(object sender, System.EventArgs e)
{
ToExcel(DataGrid1);
}

protected void BT_ToWord_Click(object sender, System.EventArgs e)
{
ToWord(DataGrid1);
}
}
明明可以打印但是为什么点按钮的时候会跳出
IDataReader Reader = helper.ExecuteReader(Session["SQL"].ToString());
未将对象引用设置到对象的实例。

...全文
21 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
Xuon 2009-07-01
  • 打赏
  • 举报
回复
可能Session["SQL"]是空的呢,单步一下看看。

62,046

社区成员

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

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

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

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