标签:office
//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!");//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"); //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"); 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里的对齐按钮功能一致。 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;
}
标签:office
原文地址:http://blog.csdn.net/qzyf1992/article/details/40075047