简体   繁体   English

如何使用C#中的文本保存文本字体(手动设置)

[英]How to save text font (which was set up manually) with a text in C#

I set up the font manually for the labels, however, when I am saving it as a Word Document the font which I set up previously disappears. 我为标签手动设置了字体,但是,当我将其另存为Word Document时,以前设置的字体就会消失。 I do not know how to figure it out 我不知道该怎么解决

private void button1_Click(object sender, EventArgs e)
    {
        string text = label1.Text + textBox1.Text + "\r\n\r\n\r\n" +
                      label2.Text + textBox2.Text + "\r\n\r\n\r\n";

        sSaveFileDialog sfd = new SaveFileDialog();
        sfd.Filter = "Microsoft Word| *.doc";
        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        {
            string path = sfd.FileName;
            MessageBox.Show(path);
            if (!File.Exists(path))
            {

                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine(text);

                }

            }

        }
    }

StreamWriter (basically) writes a string (of characters) to a file. StreamWriter (基本上)将一个字符串(字符)写入文件。 Word formatting is not that simple. Word格式化不是那么简单。 If you want the formatting then it gets more complicated. 如果您想要格式化,那么它将变得更加复杂。

See this MSDN article for more info on formatting Word documents. 请参阅此MSDN文章 ,以获取有关格式化Word文档的更多信息。 You need an object that can control the document. 您需要一个可以控制文档的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM