我直接在Microsoft Word文件中编写报告,但问题是我无法编写其他文本。它只会覆盖我的其他文字。
Word.Application myWord = new Word.Application();
//create the document
Word.Document myDoc = myWord.Documents.Add();
//ADD PARAGRAPH
myDoc.Content.Text = "Identified Market/Visitors" + " " + "Museum (as a whole)" + " " + "Percentage" + " ";
myDoc.Content.Text = "2" + " " + "76)" + " " + "25% "+ " ";
saveFileDialog1.Filter = "Word Document|*.docx";
string path = saveFileDialog1.FileName;
myDoc.Close();
第二个内容文本将覆盖第一个内容文本。 如何写另一行文字?
答案 0 :(得分:1)
生成docx的最佳方法是使用the spec。
最简单的编写代码的方法是用单词创建docx(看起来像您想要的报告),然后使用OpenXmlSDK从docx反转Enginier C#代码,而不是重新发明轮子。 open xml sdk tools
为您的文本生成的代码:
Body body1 = new Body();
Paragraph paragraph1 = new Paragraph(){ RsidParagraphAddition = "008B2DA9", RsidParagraphProperties = "008B2DA9", RsidRunAdditionDefault = "008B2DA9", ParagraphId = "769280AB", TextId = "395E2934" };
ParagraphProperties paragraphProperties1 = new ParagraphProperties();
Tabs tabs1 = new Tabs();
TabStop tabStop1 = new TabStop(){ Val = TabStopValues.Left, Position = 3686 };
TabStop tabStop2 = new TabStop(){ Val = TabStopValues.Left, Position = 6804 };
tabs1.Append(tabStop1);
tabs1.Append(tabStop2);
ParagraphMarkRunProperties paragraphMarkRunProperties1 = new ParagraphMarkRunProperties();
Languages languages1 = new Languages(){ Val = "en-US" };
paragraphMarkRunProperties1.Append(languages1);
paragraphProperties1.Append(tabs1);
paragraphProperties1.Append(paragraphMarkRunProperties1);
Run run1 = new Run(){ RsidRunProperties = "005F7E61" };
RunProperties runProperties1 = new RunProperties();
Languages languages2 = new Languages(){ Val = "en-US" };
runProperties1.Append(languages2);
Text text1 = new Text();
text1.Text = "Identified Market/Visitors";
run1.Append(runProperties1);
run1.Append(text1);
Run run2 = new Run();
RunProperties runProperties2 = new RunProperties();
Languages languages3 = new Languages(){ Val = "en-US" };
runProperties2.Append(languages3);
TabChar tabChar1 = new TabChar();
run2.Append(runProperties2);
run2.Append(tabChar1);
Run run3 = new Run(){ RsidRunProperties = "005F7E61" };
RunProperties runProperties3 = new RunProperties();
Languages languages4 = new Languages(){ Val = "en-US" };
runProperties3.Append(languages4);
Text text2 = new Text();
text2.Text = "Museum (as a whole)";
run3.Append(runProperties3);
run3.Append(text2);
Run run4 = new Run();
RunProperties runProperties4 = new RunProperties();
Languages languages5 = new Languages(){ Val = "en-US" };
runProperties4.Append(languages5);
TabChar tabChar2 = new TabChar();
run4.Append(runProperties4);
run4.Append(tabChar2);
Run run5 = new Run(){ RsidRunProperties = "005F7E61" };
RunProperties runProperties5 = new RunProperties();
Languages languages6 = new Languages(){ Val = "en-US" };
runProperties5.Append(languages6);
Text text3 = new Text();
text3.Text = "Percentage";
run5.Append(runProperties5);
run5.Append(text3);
paragraph1.Append(paragraphProperties1);
paragraph1.Append(run1);
paragraph1.Append(run2);
paragraph1.Append(run3);
paragraph1.Append(run4);
paragraph1.Append(run5);
Paragraph paragraph2 = new Paragraph(){ RsidParagraphMarkRevision = "008B2DA9", RsidParagraphAddition = "008B2DA9", RsidParagraphProperties = "008B2DA9", RsidRunAdditionDefault = "008B2DA9", ParagraphId = "6C5F87B4", TextId = "46169FF3" };
ParagraphProperties paragraphProperties2 = new ParagraphProperties();
Tabs tabs2 = new Tabs();
TabStop tabStop3 = new TabStop(){ Val = TabStopValues.Left, Position = 3686 };
TabStop tabStop4 = new TabStop(){ Val = TabStopValues.Left, Position = 6804 };
tabs2.Append(tabStop3);
tabs2.Append(tabStop4);
ParagraphMarkRunProperties paragraphMarkRunProperties2 = new ParagraphMarkRunProperties();
Languages languages7 = new Languages(){ Val = "en-US" };
paragraphMarkRunProperties2.Append(languages7);
paragraphProperties2.Append(tabs2);
paragraphProperties2.Append(paragraphMarkRunProperties2);
Run run6 = new Run();
RunProperties runProperties6 = new RunProperties();
Languages languages8 = new Languages(){ Val = "en-US" };
runProperties6.Append(languages8);
Text text4 = new Text();
text4.Text = "2";
run6.Append(runProperties6);
run6.Append(text4);
Run run7 = new Run();
RunProperties runProperties7 = new RunProperties();
Languages languages9 = new Languages(){ Val = "en-US" };
runProperties7.Append(languages9);
TabChar tabChar3 = new TabChar();
Text text5 = new Text();
text5.Text = "76";
run7.Append(runProperties7);
run7.Append(tabChar3);
run7.Append(text5);
Run run8 = new Run();
RunProperties runProperties8 = new RunProperties();
Languages languages10 = new Languages(){ Val = "en-US" };
runProperties8.Append(languages10);
TabChar tabChar4 = new TabChar();
Text text6 = new Text();
text6.Text = "25%";
run8.Append(runProperties8);
run8.Append(tabChar4);
run8.Append(text6);
paragraph2.Append(paragraphProperties2);
paragraph2.Append(run6);
paragraph2.Append(run7);
paragraph2.Append(run8);
Paragraph paragraph3 = new Paragraph(){ RsidParagraphMarkRevision = "005F7E61", RsidParagraphAddition = "00BA3985", RsidParagraphProperties = "005F7E61", RsidRunAdditionDefault = "008B2DA9", ParagraphId = "2562476B", TextId = "454E7663" };
ParagraphProperties paragraphProperties3 = new ParagraphProperties();
ParagraphMarkRunProperties paragraphMarkRunProperties3 = new ParagraphMarkRunProperties();
Languages languages11 = new Languages(){ Val = "en-US" };
paragraphMarkRunProperties3.Append(languages11);
paragraphProperties3.Append(paragraphMarkRunProperties3);
BookmarkStart bookmarkStart1 = new BookmarkStart(){ Name = "_GoBack", Id = "0" };
BookmarkEnd bookmarkEnd1 = new BookmarkEnd(){ Id = "0" };
paragraph3.Append(paragraphProperties3);
paragraph3.Append(bookmarkStart1);
paragraph3.Append(bookmarkEnd1);
SectionProperties sectionProperties1 = new SectionProperties(){ RsidRPr = "005F7E61", RsidR = "00BA3985" };
PageSize pageSize1 = new PageSize(){ Width = (UInt32Value)12240U, Height = (UInt32Value)15840U };
PageMargin pageMargin1 = new PageMargin(){ Top = 1440, Right = (UInt32Value)1440U, Bottom = 1440, Left = (UInt32Value)1440U, Header = (UInt32Value)720U, Footer = (UInt32Value)720U, Gutter = (UInt32Value)0U };
Columns columns1 = new Columns(){ Space = "720" };
DocGrid docGrid1 = new DocGrid(){ LinePitch = 360 };
sectionProperties1.Append(pageSize1);
sectionProperties1.Append(pageMargin1);
sectionProperties1.Append(columns1);
sectionProperties1.Append(docGrid1);
body1.Append(paragraph1);
body1.Append(paragraph2);
body1.Append(paragraph3);
body1.Append(sectionProperties1);
document1.Append(body1);
PS:尽管使用字符串格式构建表很奇怪。在docx中使用表格,从中创建C#代码。
答案 1 :(得分:0)
要在新行中开始下一个句子,只需在每个文本的末尾添加换行符(“ \ r \ n”或Environment.NewLine):
myDoc.Content.Text = "Identified Market/Visitors" + " " + "Museum (as a whole)" + " " + "Percentage" + " " + Environment.NewLine;
myDoc.Content.Text += "2" + " " + "76)" + " " + "25% "+ " " + Environment.NewLine;