Spire.Doc测评

网上的office类库很多比较流行的有openxmlsdk,npoi,epplus,aspose等等..

e-iceblue公司出品的office通用类库虽然并不是很有名气但是公司也很强大。前2天受朋友邀请这里为e-iceblue公司的word类库做一个简单的测评 我直挑我认为非常有用的说

功能一:向word中段落后插入数据

 //Create word document
            Document document = new Document();

            //Create a new secition
            Section section = document.AddSection();

            //Create a new paragraph
            Paragraph paragraph = section.AddParagraph();

            //Append Text
            paragraph.AppendText("Hello World!\r");
            paragraph.AppendText("Hello World!");

spire.doc 提供了 appentText 方法插入数据,当然要换行的话用换行符\r就OK的。简单的hello,world例子入门非常不错。

功能二:替换文本中的某段字符串为另一个字符串

//Create word document
            Document document = new Document();

            //load a document
            document.LoadFromFile(@"..\..\..\..\..\..\Data\FindAndReplace.doc");

            //Replace text
            document.Replace(this.textBox1.Text, this.textBox2.Text,true,true);

            //Save doc file.
            document.SaveToFile("Sample.doc", FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

这样就把textbox1.text比如是word,textbox2.text比如是hello,就可以讲所有的word这个字符串全部替换成hello,这里包括段落等操作元素同样有replace方法非常给力。

功能三:操作range属性

 //Open a blank word document as template
            Document document = new Document(@"..\..\..\..\..\..\Data\Blank.doc");

            //Get the first secition
            Section section = document.Sections[0];

            //Create a new paragraph or get the first paragraph
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

            //Append Text
            String text
                = "This paragraph is demo of text font and color. "
                + "The font name of this paragraph is Tahoma. "
                + "The font size of this paragraph is 20. "
                + "The under line style of this paragraph is DotDot. "
                + "The color of this paragraph is Blue. ";
            TextRange txtRange = paragraph.AppendText(text);

            //Font name
            txtRange.CharacterFormat.FontName = "Tahoma";

            //Font size
            txtRange.CharacterFormat.FontSize = 20;

            //Underline
            txtRange.CharacterFormat.UnderlineStyle = UnderlineStyle.DotDot;

            //Change text color
            txtRange.CharacterFormat.TextColor = Color.Blue;

            //Save doc file.
            document.SaveToFile("Sample.doc",FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

txtRange.CharacterFormat可以给区域提供各种各样的属性。非常方便。我们可以通过这个属性随心所欲的设置客户喜欢的样式。

功能四:提供了很多内定的美丽的样式

//Open a blank word document as template
            Document document = new Document(@"..\..\..\..\..\..\Data\Blank.doc");

            //Get the first secition
            Section section = document.Sections[0];

            //Create a new paragraph or get the first paragraph
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

            //Append Text
            paragraph.AppendText("Builtin Style:");

            foreach (BuiltinStyle builtinStyle in Enum.GetValues(typeof(BuiltinStyle)))
            {
                paragraph = section.AddParagraph();
                //Append Text
                paragraph.AppendText(builtinStyle.ToString());
                //Apply Style
                paragraph.ApplyStyle(builtinStyle);
            }

            //Save doc file.
            document.SaveToFile("Sample.doc",FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

BuiltinStyle是个枚举内部有很多样式。通过直接设置这些枚举的样式就能把段落搞的很漂亮。

功能五:设置段落对齐

 private void button1_Click(object sender, EventArgs e)
        {
            //Open a blank word document as template
            Document document = new Document(@"..\..\..\..\..\..\Data\Blank.doc");

            //Get the first secition
            Section section = document.Sections[0];

            //Create a new paragraph or get the first paragraph
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();

            //Append Text
            paragraph.AppendText("The various ways to format paragraph text in Microsoft Word:");

            paragraph.ApplyStyle(BuiltinStyle.Heading1);

            //Append alignment text
            AppendAligmentText(section);

            //Append indentation text
            AppendIndentationText(section);

            AppendBulletedList(section);

            //Save doc file.
            document.SaveToFile("Sample.doc",FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

        }

        private void AppendAligmentText(Section section)
        {
            Paragraph paragraph = null;

            paragraph = section.AddParagraph();

            //Append Text
            paragraph.AppendText("Horizontal Aligenment");

            paragraph.ApplyStyle(BuiltinStyle.Heading3);

            foreach (Spire.Doc.Documents.HorizontalAlignment align in Enum.GetValues(typeof(Spire.Doc.Documents.HorizontalAlignment)))
            {
                Paragraph paramgraph = section.AddParagraph();
                paramgraph.AppendText("This text is " + align.ToString());
                paramgraph.Format.HorizontalAlignment = align;
            }
        }

        private void AppendIndentationText(Section section)
        {
            Paragraph paragraph = null;

            paragraph = section.AddParagraph();

            //Append Text
            paragraph.AppendText("Indentation");

            paragraph.ApplyStyle(BuiltinStyle.Heading3);

            paragraph = section.AddParagraph();
            paragraph.AppendText("Indentation is the spacing between text and margins. Word allows you to set left and right margins, as well as indentations for the first line of a paragraph and hanging indents");
            paragraph.Format.FirstLineIndent = 15;
        }

        private void AppendBulletedList(Section section)
        {
            Paragraph paragraph = null;

            paragraph = section.AddParagraph();

            //Append Text
            paragraph.AppendText("Bulleted List");

            paragraph.ApplyStyle(BuiltinStyle.Heading3);

            paragraph = section.AddParagraph();
            for (int i = 0; i < 5; i++)
            {
                paragraph = section.AddParagraph();
                paragraph.AppendText("Item" + i.ToString());

                if (i == 0)
                {
                    paragraph.ListFormat.ApplyBulletStyle();
                }
                else
                {
                    paragraph.ListFormat.ContinueListNumbering();
                }

                paragraph.ListFormat.ListLevelNumber = 1;
            }
        }

跟word里的对齐按钮功能一致。

功能6:设置书签

 private void button1_Click(object sender, EventArgs e)
        {
            //Open a blank word document as template
            Document document = new Document(@"..\..\..\..\..\..\Data\Blank.doc");

            Bookmark(document.Sections[0]);

            //Save doc file.
            document.SaveToFile("Sample.doc",FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

        }

        private void Bookmark(Section section)
        {
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
            paragraph.AppendText("The sample demonstrates how to using bookmark.");
            paragraph.ApplyStyle(BuiltinStyle.Heading2);

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Simple bookmark.");
            paragraph.ApplyStyle(BuiltinStyle.Heading4);

            // Writing simple bookmarks
            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendBookmarkStart("SimpleBookMark");
            paragraph.AppendText("This is a simple book mark.");
            paragraph.AppendBookmarkEnd("SimpleBookMark");

            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendText("Nested bookmark.");
            paragraph.ApplyStyle(BuiltinStyle.Heading4);

            // Writing nested bookmarks
            section.AddParagraph();
            paragraph = section.AddParagraph();
            paragraph.AppendBookmarkStart("Root");
            paragraph.AppendText(" Root data ");
            paragraph.AppendBookmarkStart("NestedLevel1");
            paragraph.AppendText(" Nested Level1 ");
            paragraph.AppendBookmarkStart("NestedLevel2");
            paragraph.AppendText(" Nested Level2 ");
            paragraph.AppendBookmarkEnd("NestedLevel2");
            paragraph.AppendText(" Data Level1 ");
            paragraph.AppendBookmarkEnd("NestedLevel1");
            paragraph.AppendText(" Data Root ");
            paragraph.AppendBookmarkEnd("Root");

        }

功能七:插入图片

 private void button1_Click(object sender, EventArgs e)
        {
            //Open a blank word document as template
            Document document = new Document(@"..\..\..\..\..\..\Data\Blank.doc");

            InsertImage(document.Sections[0]);

            //Save doc file.
            document.SaveToFile("Sample.doc",FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

        }

        private void InsertImage(Section section)
        {
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
            paragraph.AppendText("The sample demonstrates how to insert a image into a document.");
            paragraph.ApplyStyle(BuiltinStyle.Heading2);

            paragraph = section.AddParagraph();
            paragraph.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Left;
            paragraph.AppendPicture(Image.Properties.Resources.Word);
        }

可以在段落中插入图片并且设置其对齐方式。

功能八:插入表格

 private void button1_Click(object sender, EventArgs e)
        {
            //Open a blank word document as template
            Document document = new Document(@"..\..\..\..\..\..\Data\Blank.doc");

            addTable(document.Sections[0]);

            //Save doc file.
            document.SaveToFile("Sample.doc",FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

        }

        private void addTable(Section section)
        {
            String[] header = { "Name", "Capital", "Continent", "Area", "Population" };
            String[][] data =
                {
                    new String[]{"Argentina", "Buenos Aires", "South America", "2777815", "32300003"},
                    new String[]{"Bolivia", "La Paz", "South America", "1098575", "7300000"},
                    new String[]{"Brazil", "Brasilia", "South America", "8511196", "150400000"},
                    new String[]{"Canada", "Ottawa", "North America", "9976147", "26500000"},
                    new String[]{"Chile", "Santiago", "South America", "756943", "13200000"},
                    new String[]{"Colombia", "Bagota", "South America", "1138907", "33000000"},
                    new String[]{"Cuba", "Havana", "North America", "114524", "10600000"},
                    new String[]{"Ecuador", "Quito", "South America", "455502", "10600000"},
                    new String[]{"El Salvador", "San Salvador", "North America", "20865", "5300000"},
                    new String[]{"Guyana", "Georgetown", "South America", "214969", "800000"},
                    new String[]{"Jamaica", "Kingston", "North America", "11424", "2500000"},
                    new String[]{"Mexico", "Mexico City", "North America", "1967180", "88600000"},
                    new String[]{"Nicaragua", "Managua", "North America", "139000", "3900000"},
                    new String[]{"Paraguay", "Asuncion", "South America", "406576", "4660000"},
                    new String[]{"Peru", "Lima", "South America", "1285215", "21600000"},
                    new String[]{"United States of America", "Washington", "North America", "9363130", "249200000"},
                    new String[]{"Uruguay", "Montevideo", "South America", "176140", "3002000"},
                    new String[]{"Venezuela", "Caracas", "South America", "912047", "19700000"}
                };
            Spire.Doc.Table table = section.AddTable();
            table.ResetCells(data.Length + 1, header.Length);

            // ***************** First Row *************************
            TableRow row = table.Rows[0];
            row.IsHeader = true;
            row.Height = 20;    //unit: point, 1point = 0.3528 mm
            row.HeightType = TableRowHeightType.Exactly;
            row.RowFormat.BackColor = Color.Gray;
            for (int i = 0; i < header.Length; i++)
            {
                row.Cells[i].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                Paragraph p = row.Cells[i].AddParagraph();
                p.Format.HorizontalAlignment = Spire.Doc.Documents.HorizontalAlignment.Center;
                TextRange txtRange = p.AppendText(header[i]);
                txtRange.CharacterFormat.Bold = true;
            }

            for (int r = 0; r < data.Length; r++)
            {
                TableRow dataRow = table.Rows[r + 1];
                dataRow.Height = 20;
                dataRow.HeightType = TableRowHeightType.Exactly;
                dataRow.RowFormat.BackColor = Color.Empty;
                for (int c = 0; c < data[r].Length; c++)
                {
                    dataRow.Cells[c].CellFormat.VerticalAlignment = VerticalAlignment.Middle;
                    dataRow.Cells[c].AddParagraph().AppendText(data[r][c]);
                }
            }
        }

功能九:水印效果

 private void button1_Click(object sender, EventArgs e)
        {
            //Open a blank word document as template
            Document document = new Document(@"..\..\..\..\..\..\Data\Blank.doc");

            InsertWatermark(document.Sections[0]);

            //Save doc file.
            document.SaveToFile("Sample.doc",FileFormat.Doc);

            //Launching the MS Word file.
            WordDocViewer("Sample.doc");

        }

        private void InsertWatermark(Section section)
        {
            Paragraph paragraph
                = section.Paragraphs.Count > 0 ? section.Paragraphs[0] : section.AddParagraph();
            paragraph.AppendText("The sample demonstrates how to insert a watermark into a document.");
            paragraph.ApplyStyle(BuiltinStyle.Heading2);

            TextWatermark txtWatermark = new TextWatermark();
            txtWatermark.Text = "Watermark Demo";
            txtWatermark.FontSize = 90;
            txtWatermark.Layout = WatermarkLayout.Diagonal;
            section.Document.Watermark = txtWatermark;

        }

功能十:转换为其他的格式包括xml,html,image,pdf...参见demo.

此类库还有其他的非常强大的功能。具体可去官网下载demo..确实是一个非常实用的类库。

时间: 2024-10-17 19:25:26

Spire.Doc测评的相关文章

在C#中使用Spire.doc对word的操作总结

在C#中使用Spire.doc对word的操作总结 在最近的工程中我们要处理一些word文档.通过在网上的大量搜索,我发现大多数软件功能不是不完整就是有重复.极少数可以完全实现的word组件又要收费.功夫不负有心人,终于找到了可以满足我们需要的免费的C# word程序库.为了和其他的作比较,我在这里先做以下汇总.希望对大家有帮助. 如何得到? 这个免费版的word 组件可以在Codeplex下载到,你也可以从本文里直接下载msi文件.它还提供了一些源代码. Word操作汇总 1.        

Spire.DOC生成表格测试

首先,很感谢Jack对我的信任,让我来写一个评测,在此对Jack说一声抱歉,由于本人愚钝,并且最近项目比较紧张,把评测这个事情脱了一个月之久,由于往后的日子可能更忙,所以今晚抽空只能只写了一个小程序来测试. Spire系列的Word,PDF,Excel,Presentation是一套专为.NET开发人员设计的控件.使用该套工具,程序员可以在任意.NET平台上对Office文件.PDF文档进行生成,读取,编辑,修改,转换格式等处理,且不需要安装MS Office. 红字部分的内容,是有Jack给我

支持Word文档和其他文件格式间的转换的控件Spire.Doc for .NET

Spire.Doc for .NET是e-iceblue公司推出的一款专门对Microsoft Word 文档进行操作的.NET类控件.这款控件的主要功能在于帮助开发人员轻松快捷地生成.编辑和查看Word文档.同时,开发人员还可以通过使用Spire.Doc for .NET 设置Word文档的格式,插入图片,表格,超链接等.Spire.Doc for .NET 最大的便利之处在于它不依赖于Microsoft Word以及任何其他第三方软件.只需将此款控件安装在您的电脑上,您就可以对word文档进

使用Spire.Doc组件利用模板导出Word文档

以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作,有NPOI,Spire等第三方组件.开始考虑的是NPOI,毕竟它在操作Excel方面还是很强大的:但是不知道是它本身没有,还是我没找到,无法实现利用Word模板的标签插入内容,纯靠代码去生成Word文档,排版是个大问题.最终找到了Spire.Doc组件,轻松实现! Spire的官网地址:https:

Spire.Doc组件

使用Spire.Doc组件利用模板导出Word文档 以前一直是用Office的组件实现Word文档导出,但是让客户在服务器安装Office,涉及到版权:而且Office安装,包括权限配置也是比较麻烦. 现在流行使用第三方组件来实现对Office的操作,有NPOI,Spire等第三方组件.开始考虑的是NPOI,毕竟它在操作Excel方面还是很强大的:但是不知道是它本身没有,还是我没找到,无法实现利用Word模板的标签插入内容,纯靠代码去生成Word文档,排版是个大问题.最终找到了Spire.Doc

[.NET] 操作 Word 组件 - Spire.Doc 介绍

操作 Word 组件 - Spire.Doc 介绍 [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5898368.html 序 本打算过几天简单介绍下组件 Spire.XLS,突然发现园友率先发布了一篇,既然 xls 已经出现,为避免打上抄袭嫌疑,博主只能抢先一步使用 Spire.Doc 简单介绍 Doc 操作,下面是通过 WinForm 程序执行代码完成介绍的. 本机环境:Win10 x64.VS 2015.MS Office 2016. 目

【好文翻译】一步一步教你使用Spire.Doc转换Word文档格式

背景: 本文试图证明和审查Spire.Doc的格式转换能力.很长的一段时间里,为了操作文档,开发人员不得不在服务器上安装Office软件.首先,这是一个很糟糕的设计和实践.第二,微软从没打算把Office作为一个服务器组件,它也用来在服务器端解释和操作文档的.于是乎,产生了类似Spire.Doc这样的类库.当我们讨论这个问题时,值得一提的是 Office Open Xml. Office Open XML (也有非正式地称呼为 OOXML 或OpenXML) 是一种压缩的, 基于XML的文件格式

使用Spire.Doc来转换文本

使用Spire.Doc来转换文本 前段时间,我为不熟悉这个产品的读者们写了一篇关于我对 Spire.Doc的初识印象.Spire.Doc是一个专业的Word .NET库,它是专门为开发人员设计的用来快捷高效地在任何.NET(C#,VB.NET,ASP.NET)创建,载入,编辑,转化,打印 Word文档的.作为一个独立的Word .NET组件,Spire.Doc for .NET不需要您在机器上安装 MicrosoftWord.但是它可以将Microsoft Word 文件创造力与在任何开发者的应

SPIRE.DOC - .NET开发者的福利

SPIRE.DOC - .NET开发者的福利 前面我们使用过Spire.XLS for .NET Component创建Excel文件.最近试用了下.DOC 方面的API.这次测试的产品是Spire.Doc for .NET,这是一个专业的Word .NET库,尤其是为那些需要快速高效地在任何.NET平台( C#, VB.NET, ASP.NET)上创建,读取,编辑,转换和打印word文档的开发人员设计的.它根本就不需要你的系统中安装word,因为它是一个完完全全独立的组件(一个Word.Net