怎么保存WEB页面的数据到一个WORD文件中去????在线等 急,能否给出代码,谢谢。请各位高手帮忙。

解决方案 »

  1.   

    可以先保存为一个html文件,然后用word打开,然后存成word文件
      

  2.   

    直接用word打开,然后存成.doc文件就可以了。protected void ConvertToHtml(string SrcFilePath,string TargetFilePath)
    {
      Word.Application app=new Word.Application();
      app.Visible=false;
      Object o=Missing.Value;
      object docFile=SrcFilePath;
      _Document doc=app.Documents.Open(ref docFile,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
      object fileName=TargetFilePath;
      object format=Word.WdSaveFormat.wdFormatDocument; //word format
      doc.SaveAs(ref fileName,ref format,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o,ref o);
      object t=true;
      app.Quit(ref t,ref o,ref o);
    }
      

  3.   

    #region 数据集生成Word文件
    /// <summary>
    /// 数据集生成Word文件
    /// </summary>
    private void ToWord()
    {
            object Missing = System.Reflection.Missing.Value;
            int NumRows, NumColumns, rowIndex, colIndex;
            object FileName = "c:\\liu.doc"; //保存word文件的路径
    // Office.CommandBar MyBar; //工具栏
    Word.ApplicationClass wordApp = new Word.ApplicationClass();
    Word.Document myDoc;
    Word.Table oTable;
    DataTable Table = new DataTable(); rowIndex = 1;
    colIndex = 0;
    wordApp.Documents.Add(ref Missing,ref Missing,ref Missing, ref Missing);
    myDoc = wordApp.ActiveDocument;

    Table = MyBind();
    oTable = myDoc.Tables.Add(myDoc.Range(ref Missing, ref Missing),NumRows=Table.Rows.Count + 1, NumColumns=Table.Columns.Count,ref Missing,ref Missing); //将所得到的表的列名,赋值给单元格
    foreach(DataColumn Col in Table.Columns)
    {
    colIndex = colIndex + 1;
    oTable.Cell(1, colIndex).Range.InsertAfter(Col.ColumnName);
    } //得到的表所有行,赋值给单元格
    foreach(DataRow Row in Table.Rows)
    {
    rowIndex = rowIndex + 1;
    colIndex = 0;
    foreach(DataColumn Col in Table.Columns)
             {
    colIndex = colIndex + 1;
    oTable.Cell(rowIndex, colIndex).Range.InsertAfter(Row[Col.ColumnName].ToString());
    }
    } oTable.Borders.InsideLineStyle = Word.WdLineStyle.wdLineStyleDashDot;
    oTable.Borders.OutsideLineStyle = Word.WdLineStyle.wdLineStyleDot;
    wordApp.Visible = false; //true:word文件显示 false:word文件不显示

    myDoc.SaveAs(ref FileName,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing,ref Missing);
    }
    #endregion

    #region DataGridToWord
    /// <summary>
    /// DataGridToWord
    /// </summary>
    /// <param name="ctl">DataGrid控件</param>
    private void DataGridToWord(System.Web.UI.Control ctl)
    {
    HttpContext.Current.Response.Clear();
    HttpContext.Current.Response.Charset ="";
    HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename=liu0910.doc");

    HttpContext.Current.Response.ContentEncoding =System.Text.Encoding.GetEncoding("GB2312"); 
    //更改ContentType的值为ms-word即可实现导出到Word
    HttpContext.Current.Response.ContentType ="application/ms-word";//image/JPEG;text/HTML;image/GIF;vnd.ms-excel/msword
    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();
    }
    #endregion