我如何写粗体文字到Word文档编程不加粗整个文档? [英] How do I write bold text to a Word document programmatically without bolding the entire document?

查看:166
本文介绍了我如何写粗体文字到Word文档编程不加粗整个文档?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的程序需要生成在Office .DOC 格式(非XML)高度简单的报表,并且文档的某些部分需要有魄力。我一直在寻找的定义范围的,这部分是我的代码从派生眼下。 文档的这一部分并没有真正给我足够的细节来实现这个一般的我的文档英寸这里是我到目前为止的代码:

My program needs to generate highly simple reports in the Office .doc format (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. This part of the documentation doesn't really give me enough details to implement this in general in my document. Here is my code so far:

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;

这会产生一个word文档,只有文字 **信息标题下方** 。这是一个简单的例子。

This produces a word document with only the text **Information underneath the header**. This is a simple example.

我的文档不会得到多少比这更复杂,但我希望能够生成基于可变数据量的Word文档,以大胆文字和非粗体文本分散。

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.

推荐答案

这里的,我想出了一个答案可以让你有一个字符串大胆的和经常在同一个字符串的一部分。

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.

我在做什么是自动化的,但如果你知道你在做什么同样适用。请记住太多的大胆只是一个int,没有布尔真/假(出于某种原因)。

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 char or string so you can change what is being used to set the bold start/stop.

有一点要注意的是在其他线程的意见讨论的是,由于某种原因,大胆只是一个int。有用于设定,没有布尔值。这很奇怪,我知道了。

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.

这篇关于我如何写粗体文字到Word文档编程不加粗整个文档?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆