网上的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