萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> asp.net編程 >> ASP.NET(C#)將數據導出到Word或Excel

ASP.NET(C#)將數據導出到Word或Excel

   最簡單的方法是把頁面上所有的東西都導出

  在載入時調用,注意頁面裡不能有其它控件,包括按鈕

  void converttoexcel()

  {

  Response.Clear();

  Response.Buffer = true;

  Response.Charset = "GB2312";

  Response.AppendHeader("Content-Disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".xls");

  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

  Response.ContentType = "application/ms-excel";

  this.Page.EnableViewState = false;

  System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

  System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

  this.Page.RenderControl(oHtmlTextWriter);

  Response.Write(oStringWriter.ToString());

  Response.End();

  }

  ASP.NET(C#)將數據導出到Word或Excel

  命名空間:

  using System.IO;

  using System.Text;

  將DataGrid的數據導出到Excel

  string excelname="excel文件名";

  HttpContext.Current.Response.Charset = "GB2312";

  HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

  HttpContext.Current.Response.ContentType = "application/ms-excel";

  HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".xls");

  dr1.Page.EnableViewState = false;

  StringWriter sw = new StringWriter();

  HtmlTextWriter tw = new HtmlTextWriter(sw);

  dr1.RenderControl(tw);

  HttpContext.Current.Response.Write(sw.ToString());

  HttpContext.Current.Response.End();

  將DataGrid的數據導出到Word

  string excelname="word文件名";

  HttpContext.Current.Response.Charset = "GB2312";

  HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;

  HttpContext.Current.Response.ContentType = "application/ms-winword";

  HttpContext.Current.Response.AppendHeader("Content-disposition", "attachment;filename=" + excelname + ".doc");

  dr1.Page.EnableViewState = false;

  StringWriter sw = new StringWriter();

  HtmlTextWriter tw = new HtmlTextWriter(sw);

  dr1.RenderControl(tw);

  HttpContext.Current.Response.Write(sw.ToString());

  HttpContext.Current.Response.End();

  ASP.NET 2.0,C#----利用GridView控件導出其他文件(導出Excel,導出Word文件)

  // 注意,在Visual Studio2005平台下,如果使用GridView導出文件,

  //就必須重載VerifyRenderingInServerForm方法

  public override void VerifyRenderingInServerForm(Control control)

  {

  }

  ///

  /// 導出到文件的方法,

  ///

  /// Model=1:導出為Execl,Model=2:導出為Word

  private void toFiles(int Model)

  {

  string strFileName = DateTime.Now.ToString("yyyyMMdd-hhmmss");

  System.Web.HttpContext HC = System.Web.HttpContext.Current;

  HC.Response.Clear();

  HC.Response.Buffer = true;

  HC.Response.ContentEncoding = System.Text.Encoding.UTF8;//設置輸出流為簡體中文

  if (Model == 1)

  {

  //---導出為Excel文件

  HC.Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".xls");

  HC.Response.ContentType = "application/ms-excel";//設置輸出文件類型為excel文件。

  }

  else

  {

  //--- 導出為Word文件

  HC.Response.AddHeader("Content-Disposition", "attachment;filename=" +HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8) + ".doc");

  HC.Response.ContentType = "application/ms-word";//設置輸出文件類型為Word文件。

  }

  System.IO.StringWriter sw = new System.IO.StringWriter();

  System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw);

  this.GridView1.RenderControl(htw);

  HC.Response.Write(sw.ToString());

  HC.Response.End();

  }

  //-導出為Excel文件

  protected void ToExecl_Click(object sender, EventArgs e)

  {

  toFiles(1);

  }

  //-導出為Word文件

  protected void Button1_Click(object sender, EventArgs e)

  {

  toFiles(2);

  }

  FROM:http://hi.baidu.com/jg_%B3%C2/blog/item/4f0edf188851c50135fa41ce.html

  C#操作Word[轉]

  導入COM庫:Microsoft word 11.0 Object Library.

  引用裡面就增加了:

  1.創建新Word

  object oMissing = System.Reflection.Missing.Value;

  Word._Application oWord;

  Word._Document oDoc;

  oWord = new Word.Application();

  oWord.Visible = true;

  oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,

  ref oMissing, ref oMissing);

  2.打開文檔:

  object oMissing = System.Reflection.Missing.Value;

  Word._Application oWord;

  Word._Document oDoc;

  oWord = new Word.Application();

  oWord.Visible = true;

  object fileName = @"E:CCCXCXXTestDoc.doc";

  oDoc = oWord.Documents.Open(ref fileName,

  ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

  ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,

  ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

  3.導入模板

  object oMissing = System.Reflection.Missing.Value;

  Word._Application oWord;

  Word._Document oDoc;

  oWord = new Word.Application();

  oWord.Visible = true;

  object fileName = @"E:XXXCCXTest.doc";

  oDoc = oWord.Documents.Add(ref fileName, ref oMissing,

  ref oMissing, ref oMissing);

  4..添加新表

  object oMissing = System.Reflection.Missing.Value;

  Word._Application oWord;

  Word._Document oDoc;

  oWord = new Word.Application();

  oWord.Visible = true;

  oDoc = oWord.Documents.Add(ref oMissing, ref oMissing,

  ref oMissing, ref oMissing);

  object start = 0;

  object end = 0;

  Word.Range tableLocation = oDoc.Range(ref start, ref end);

  oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing);

  5.設置表寬度和高度

  //選中書簽區域

  oDoc.Bookmarks.Item(ref obj).Range.Select();

  //定義表格

  Word.Table table= application.Selection.Tables.Add(application.Selection.Range,3,columns,

  ref missing, ref missing);

  table.Select();

  //設置表格行高度

  table.Application.Selection.Rows.SetHeight(40f,Word.WdRowHeightRule.wdRowHeightExactly);

  //設置表格列寬度

  table.Application.Selection.Columns.SetWidth(120f,Word.WdRulerStyle.wdAdjustSameWidth);

  //設置表格行間距

  table.Application.Selection.ParagraphFormat.LineSpacing=10f;

  //設置表格字體大小

  table.Range.Font.Size=10;

  6..表插入行

  object oMissing = System.Reflection.Missing.Value;

  Word._Application oWord;

  Word._Document oDoc;

  oWord = new Word.Application();

  oWord.Visible = true;

  oDoc = oWord.Documents.Add(ref oMissing, ref oMissi

copyright © 萬盛學電腦網 all rights reserved