Hi,网页设计师综合门户欢迎您!
设为首页  加为收藏  我要投稿找回密码免责声明意见反馈

招聘精英个人求职推广网站网友作品网页特效动画欣赏网页素材酷站欣赏源码下载设计软件

  • 全站搜索
  • 新闻资讯
  • 网页教程
  • 平面教程
  • 程序设计
  • 视频教程
  • Flash教程

网页设计师提供网页设计、网站程序开发、系统开发等,请联系QQ 103198680

 您当前的位置:首页 > 阅读文章
网页设计师综合门户欢迎您!

ASP.NET(C#)将数据导出到Word或Excel

刊登方式:转载   来源:网络  作者:  关键词:平面设计理念  更新时间:2017/8/8

   最简单的方法是把页面上所有的东西都导出

  在载入时调用,注意页面里不能有其它控件,包括按钮

  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 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);

  Word.Table newTable = oDoc.Tables[1];

  object beforeRow = newTable.Rows[1];

  newTable.Rows.Add(ref beforeRow);

  7.单元格合并

  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);

  Word.Table newTable = oDoc.Tables[1];

  object beforeRow = newTable.Rows[1];

  newTable.Rows.Add(ref beforeRow);

  Word.Cell cell = newTable.Cell(1, 1);

  cell.Merge(newTable.Cell(1, 2));

  8.单元格分离

  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);

  Word.Table newTable = oDoc.Tables[1];

  object beforeRow = newTable.Rows[1];

  newTable.Rows.Add(ref beforeRow);

  Word.Cell cell = newTable.Cell(1, 1);

  cell.Merge(newTable.Cell(1, 2));

  object Rownum = 2;

  object Columnnum = 2;

  cell.Split(ref Rownum, ref Columnnum);

  9.通过段落控制插入

  object oMissing = System.Reflection.Missing.Value;

  object oEndOfDoc = "endofdoc"; /**//* endofdoc is a predefined bookmark */

  //Start Word and create a new document.

  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);

  //Insert a paragraph at the beginning of the document.

  Word.Paragraph oPara1;

  oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing);

  oPara1.Range.Text = "Heading 1";

  oPara1.Range.Font.Bold = 1;

  oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph.

  oPara1.Range.InsertParagraphAfter();