C# 如何用C语言保存文本字体(手动设置)#
我为标签手动设置字体,但是,当我将其保存为Word文档时,我以前设置的字体将消失。我不知道怎么弄明白C# 如何用C语言保存文本字体(手动设置)#,c#,C#,我为标签手动设置字体,但是,当我将其保存为Word文档时,我以前设置的字体将消失。我不知道怎么弄明白 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";
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);
}
}
}
}
(基本上)将字符串(字符)写入文件。单词格式并不是那么简单。如果您想要格式化,那么它会变得更复杂
有关格式化Word文档的详细信息,请参见此。您需要一个可以控制文档的对象。字符串数据类型不支持字体。您只是保存纯文本,而不是实际的word文档。您没有将其保存为“word文档”。您只是使用了一个以
.doc
结尾的文件名。但这只是一种任意约定——您没有使用实际的Word文档文件格式。