C# 如何在不加粗整个文档的情况下以编程方式将粗体文本写入 Word 文档?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11564073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I write bold text to a Word document programmatically without bolding the entire document?
提问by Ricardo Altamirano
My program needs to generate highly simple reports in the Office .docformat (non-XML), and certain parts of the document need to be bold. I've been looking at the documentation for defining ranges, which is partly what my code derives from at the moment. Thispart of the documentation doesn't really give me enough details to implement this in general in my document. Here is my code so far:
我的程序需要以 Office.doc格式(非 XML)生成高度简单的报告,并且文档的某些部分需要加粗。我一直在查看定义范围的文档,这部分是我的代码目前派生的。文档的这一部分并没有真正给我足够的细节来在我的文档中实现这一点。到目前为止,这是我的代码:
object miss = System.Reflection.Missing.Value;
object Visible = true;
object start = 0;
Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.Application();
Document report = WordApp.Documents.Add(ref miss, ref miss, ref miss, ref miss);
String header = "Bold Header: ";
Range headerRange = report.Range(ref start, ref miss);
headerRange.Text = header;
headerRange.Font.Bold = -1;
String data = "Information underneath the header";
Range dataRange = report.Range();
dataRange.Text = data;
dataRange.Font.Bold = 1;
object filename = "test.doc";
report.SaveAs(ref filename, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
object saveChanges = Microsoft.Office.Interop.Word.WdSaveOptions.wdPromptToSaveChanges;
object originalFormat = Microsoft.Office.Interop.Word.WdOriginalFormat.wdWordDocument;
object routeDocument = true;
WordApp.Visible = true;
This produces a word document with only the text **Information underneath the header**. This is a simple example.
这会生成一个只有 text 的 word 文档**Information underneath the header**。这是一个简单的例子。
My document won't get much more complicated than this, but I'm hoping to generate Word documents based on variable amounts of data, with bold text and non-bold text dispersed throughout.
我的文档不会比这更复杂,但我希望基于可变数量的数据生成 Word 文档,粗体文本和非粗体文本分散在各处。
采纳答案by krillgar
Here's an answer that I came up with that will allow you to have part of a string bold and regular in the same string.
这是我想出的一个答案,它允许您在同一字符串中使用粗体和常规字符串的一部分。
What I was doing was automated, but the same applies if you know what you're doing. Keep in mind too that the Bold is only an int, there is no boolean true/false (for some reason).
我正在做的是自动化的,但如果你知道你在做什么,这同样适用。还要记住,粗体只是一个整数,没有布尔值真/假(出于某种原因)。
As per Ricardo's excellent point, I'll post the code here as well:
根据里卡多的精彩观点,我也会在这里发布代码:
private void InsertMultiFormatParagraph(string psText, int piSize, int piSpaceAfter = 10) {
Word.Paragraph para = mdocWord.Content.Paragraphs.Add(ref mobjMissing);
para.Range.Text = psText;
// Explicitly set this to "not bold"
para.Range.Font.Bold = 0;
para.Range.Font.Size = piSize;
para.Format.SpaceAfter = piSpaceAfter;
object objStart = para.Range.Start;
object objEnd = para.Range.Start + psText.IndexOf(":");
Word.Range rngBold = mdocWord.Range(ref objStart, ref objEnd);
rngBold.Bold = 1;
para.Range.InsertParagraphAfter();
}
Obviously, if you are trying to abstract this even further, you can add a parameter for the charor stringso you can change what is being used to set the bold start/stop.
显然,如果您想进一步抽象,您可以为charor添加一个参数,string以便您可以更改用于设置粗体开始/停止的内容。
One thing to note that was discussed in the comments of the other thread was that for some reason, Bold is only an int. There is no bool value for setting that. It's weird, I know.
在另一个线程的评论中讨论的一件事是,由于某种原因,Bold 只是一个 int。没有用于设置的布尔值。这很奇怪,我知道。
回答by Duy Pham
You can simply use Paragraph object to customize formatting of different text blocks. Example code as below:
您可以简单地使用 Paragraph 对象来自定义不同文本块的格式。示例代码如下:
object DocumentEndIndex = "\endofdoc";
object endDocument = wordDocument.Bookmarks.get_Item(ref DocumentEndIndex).Range;
Paragraph para = wordDocument.Content.Paragraphs.Add(ref endDocument);
para.Range.Text = text;
para.Range.set_Style(ref headingLevel);
// do format the text with para.Range object as you want
para.Range.InsertParagraphAfter();
Hope this helps.
希望这可以帮助。
回答by tzarad
It's an old question, but as I faced the same problem and this didn't help me for modifications in header or footer, but helped me figure how to do it, here is my solution:
这是一个老问题,但是当我遇到同样的问题时,这并没有帮助我修改页眉或页脚,但帮助我弄清楚如何去做,这是我的解决方案:
Word.Paragraph p = c2.Range.Paragraphs.Add(ref missing);
p.Range.Text = "your trip at " + sejour.Location;
SetTextColor(p.Range, Word.WdColor.wdColorWhite,0, p.Range.Text.Length - 1);
SetTextSize(p.Range, (float)14, 0, p.Range.Text.Length - 1);
SetTextSize(p.Range, (float)16, p.Range.Text.Length - 2 - sejour.Location.Length, sejour.Location.Length);
public void SetTextColor( Word.Range range, Microsoft.Office.Interop.Word.WdColor color, int start, int length)
{
Word.Range rng = range;
rng.Start = range.Start + start;
rng.End = range.Start + start + length;
rng.Font.Color = color;
}
public void SetTextSize(Word.Range range, float size, int start, int length)
{
Word.Range rng = range;
rng.Start = range.Start + start;
rng.End = range.Start + start + length;
rng.Font.Size = size;
}

