[转载].Net将数据导出Word

来源:互联网 发布:海贼王周边 淘宝 编辑:程序博客网 时间:2024/06/11 12:50

  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 />");
        //将所有标签去掉,只剩下rn
        strPageContent = Regex.Replace(strPageContent, @"<[^>]+/?>|</[^>]+>", "", RegexOptions.IgnoreCase);


        string[] strContents = strPageContent.Split(new string[] { "rn" }, 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);
    }

原创粉丝点击