Office
一套 Microsoft 高效工作软件,支持常见业务任务,包括文字处理、电子邮件、演示文稿以及数据管理和分析。
25 个问题
如果您有任何想法,请让朋友知道。我需要使用 c# 列出 word 文档中的所有格式。我尝试将word转换为XML,然后读取XML文件格式,但它无法正常工作。那么谁能帮我?
我附上了下面的图片。我需要像这样列出。
Note:此问题总结整理于: How to List out Word Document Formats (ex: Bold, Italic, BoldItalic) using C#
您可以使用 Microsoft.Office.Interop.Word 来执行此操作,这是一个简单示例:
private void browseBtn_Click(object sender, EventArgs e)
{
string filePath = null;
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "d:\\";
openFileDialog.Filter = @"All Files|*.txt;*.docx;*.doc;";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;
//Read the contents of the file into a stream
var fileStream = openFileDialog.OpenFile();
}
}
Word.Application application = new Word.Application();
Document document = application.Documents.Open(filePath);
List<string> text = GeyText(document);
textBox1.Text = text[0];
textBox2.Text = text[1];
document.Close();
application.Quit();
}
private List<string> GeyText(Document document)
{
List<string> strings = new List<string>();
StringBuilder boldText = new StringBuilder();
StringBuilder italicText = new StringBuilder();
foreach (Range item in document.Words)
{
if (item.Bold == -1)
{
boldText.Append(item.Text + Environment.NewLine);
}
if (item.Italic == -1)
{
italicText.Append(item.Text + Environment.NewLine);
}
}
strings.Add(boldText.ToString());
strings.Add(italicText.ToString());
return strings;
}
如果回复有帮助,请点击“接受答案”并点赞。 注意:如果您想接收此线程的相关电子邮件通知,请按照我们文档中的步骤启用电子邮件通知。