如题

解决方案 »

  1.   

    protected void Button2_Click(object sender, EventArgs e)
        {
            Test();
        }    private void Test()
        {
            Object nothing = System.Reflection.Missing.Value;
            //获取Word文档保存路径
            object fileName = System.Web.HttpRuntime.AppDomainAppPath +"wj.doc";
            //创建一个名为WordApp的组件对象
            Word.ApplicationClass WordApp = new Word.ApplicationClass();
            //创建一个名为WordDoc的文档对象
            Word.Document WordDoc = WordApp.Documents.Add(ref nothing, ref nothing, ref nothing, ref nothing);
            //增加一表格
            Word.Table table = WordDoc.Tables.Add(WordApp.Selection.Range, 1, 1, ref nothing, ref nothing);
            //在表格第一单元格中添加自定义的文字内容
            table.Cell(1, 1).Range.Text = "在表格第一单元格中添加自定义的文字内容,如:我我我我我";
            //在文档空白地方添加文字内容
            WordDoc.Paragraphs.Last.Range.Bold = 72;
            WordApp.Visible = true;
            WordDoc.Activate();
            #region 标题
            WordApp.Selection.Font.Size = 15;
            WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; // 居中
            WordApp.Selection.Font.Bold = 1;    // 黑体
            WordApp.Selection.TypeText("我是标题");
            #endregion        #region 时间和来源
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.Font.Size = 10;
            WordApp.Selection.Font.Bold = 0;    // 取消黑体
            WordApp.Selection.TypeText("发布时间:" + "我是时间" + " 来源:" + "我是文件来源");
            #endregion
             #region 摘要
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphLeft; // 居左
            WordApp.Selection.TypeText("摘要:");
            WordApp.Selection.TypeParagraph();
            //WordApp.Selection.ParagraphFormat.CharacterUnitFirstLineIndent = 2.0f;  //首行缩进2个字符
            WordApp.Selection.TypeText("    " + "我是内容摘要");
            #endregion         #region 内容
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.TypeParagraph();
            WordApp.Selection.TypeText("内容:");        string strPageContent = "我是内容";
            //将一个<br>变成两个<br>
            //strPageContent = Regex.Replace(strPageContent, "(<br>[\\s]*)+", "<br /><br />");
            //将所有标签去掉,只剩下\r\n
            strPageContent = Regex.Replace(strPageContent, @"<[^>]+/?>|</[^>]+>", "", RegexOptions.IgnoreCase);
            string[] strContents = strPageContent.Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);        foreach (string strContent in strContents)
            {
                WordApp.Selection.TypeParagraph();
                WordApp.Selection.TypeText("    " + strContent);
            }
            #endregion        //WordDoc.Paragraphs.Last.Range.Text += SaveShowInfo.PictureUrl;        //将WordDoc文档对象的内容保存为DOC文档
            WordDoc.SaveAs(ref fileName, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing, ref nothing);
            //关闭WordDoc文档对象
            WordDoc.Close(ref nothing, ref nothing, ref nothing);
            //关闭WordApp组件对象
            WordApp.Quit(ref nothing, ref nothing, ref nothing);
            //返回结果
            //lblMsg.Text = "文档路径:<a href='/c:\\111.doc'>c:\\111.doc</a>(点击链接查看)<br>生成结果:成功!";        //使导出文件清除特殊符号
            //string outFileName ;
            //outFileName = outFileName.Replace("\", " ");
            //outFileName = outFileName.Replace(":", " ");
            //outFileName = outFileName.Replace("*", " ");
            //outFileName = outFileName.Replace("?", " ");
            //outFileName = outFileName.Replace(""", " ");
            //outFileName = outFileName.Replace("<", " ");
            //outFileName = outFileName.Replace(">", " ");
            //outFileName = outFileName.Replace("|", " ");
            //Response.WriteFile(outFileName + ".doc",System.Web.HttpRuntime.AppDomainAppPath + "\\XMLFiles\\EduceWordFiles\\" + this.Context.User.Identity.Name + ".doc",1024000);
        }
      

  2.   

    .NET 操作EXCEL WORD
      

  3.   

    function AllAreaWord()
    {
      var oWD = new ActiveXObject("Word.Application");
      var oDC = oWD.Documents.Add("",0,1);
      var orange =oDC.Range(0,1);
      var sel = document.body.createTextRange();
      sel.moveToElementText(PrintA);
      sel.select();
      sel.execCommand("Copy");
      orange.Paste();
      oWD.Application.Visible = true;
    }
    操作word模版
    动态添加table
     Word.Range oRng = Doc.Books.get_Item(ref oEndOfDoc).Range;
     Word.Table tb = Doc.Tables.Add(oRng, 1, 2, ref nothing, ref nothing);
     tb.Rows.Alignment = Word.WdRowAlignment.wdAlignRowCenter;
    根据行数换页
    WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekCurrentPageHeader;
    WordApp.Selection.WholeStory();
    WordApp.Selection.Font.Name ="华文新魏";   
    WordApp.Selection.Font.Size =10.5f;   
    WordApp.Selection.Font.UnderlineColor = Word.WdColor.wdColorAutomatic;   
    WordApp.Selection.Font.Underline = Word.WdUnderline.wdUnderlineSingle
    WordApp.Selection.TypeText( "");
    WordApp.ActiveWindow.ActivePane.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
    http://topic.csdn.net/u/20100913/12/754163c2-642b-42c6-9d13-59dc8f10f2e4.html
      

  4.   

     public string ExportBenefit(object sFile, object tFile,decimal? d1,decimal? d2,decimal? d3,decimal? d4,decimal? d5)
            {
                string strM = "";
                Word._Document Doc = app.Documents.Open(ref sFile, ref optional, ref docreadonly, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref visible, ref optional, ref optional, ref optional, ref optional);
                try
                {
                    if (Doc.Books.Exists("a"))
                    {
                        object b11 = "a";
                        Word.Book bk11 = Doc.Books.get_Item(ref b11);
                        bk11.Range.Text = d1.ToString();
                    }                Doc.SaveAs(ref tFile, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional, ref optional);
                    Doc.Close(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
                    app.Quit(ref NOTsaveChanges, ref originalFormat, ref routeDocument);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(app);
                    System.Runtime.InteropServices.Marshal.ReleaseComObject(Doc);
                    app = null;
                    Doc = null;
                    GC.Collect();
                    GC.Collect();
                    CloseExplore();
                }
                catch (Exception err)
                {
                    strM = err.Message + ",导出失败!";
                    return strM;
                }
                return "";
            }