WordProcessingDocument 不保留空格

发布于 2025-01-13 02:56:06 字数 2294 浏览 2 评论 0原文

我正在使用 XML 和 Linq 编写一个 C# 程序,该程序从存储在 Word 文档中的表中读取数据并将其插入到 Excel 电子表格中。到目前为止,我的代码就是这样做的,但是它不保留任何新行(在单词 doc 中,“新行”是通过按 Enter 键完成的)。使用调试器,我可以看到新行甚至没有被读入。例如,如果我要复制的文本是:

Something like this
And another line
And maybe even a third line

它被读入为:

Something like thisAnd another lineAnd maybe even a third line

我不能用字符分隔行,因为单词可以是任何东西。这就是我到目前为止所得到的:

internal override Dictionary<string, string> GetContent()
    {
      Dictionary<string, string> contents = new Dictionary<string, string>();
      using (WordprocessingDocument doc = WordprocessingDocument.Open(MainForm.WordFileDialog.FileName, false))
      {
        List<Table> tables = doc.MainDocumentPart.Document.Descendants<Table>().ToList();
        foreach (Table table in tables)
        {
          TableRow headerRow = table.Elements<TableRow>().ElementAt(0);
          TableCell tableSectionTitle;
          try
          {
            tableSectionTitle = headerRow.Elements<TableCell>().ElementAt(0);
          }
          catch (ArgumentOutOfRangeException)
          {
            continue;
          }
            List<TableRow> rows = table.Descendants<TableRow>().ToList();
            foreach (TableRow row in rows)
            {
              TableCell headerCell = row.Elements<TableCell>().ElementAt(0);
              if (headerCell.InnerText.ToLower().Contains("first item"))
              {
                contents.Add("first item", row.Elements<TableCell>().ElementAt(1).InnerText);
              }
              else if (headerCell.InnerText.ToLower().Contains("second item:"))
              {
                char[] split = { ':' };
                Int32 count = 2;
                string str = row.Elements<TableCell>().ElementAt(0).InnerText;
                String[] newStr = str.Split(split, count, StringSplitOptions.None);
                contents.Add("second item:", newStr[1]);
              }
**continues for many more else if statements**
              else
              {
                continue;
              }
            }
        }
        return contents;
      }
    }

我是使用 XML 的新手,所以任何帮助将不胜感激!

I'm writing a C# program using XML and Linq that reads in data from tables stored in a word document and inserts it into an excel spreadsheet. The code I have so far does this, however it does not preserve any new lines (in the word doc the "new line" is done by pressing the enter key). Using the debugger, I can see that the new lines aren't even being read in. For example, if the text I want to copy is:

Something like this
And another line
And maybe even a third line

It gets read in as:

Something like thisAnd another lineAnd maybe even a third line

I can't separate the lines by a character as the words could be anything. This is what I have so far:

internal override Dictionary<string, string> GetContent()
    {
      Dictionary<string, string> contents = new Dictionary<string, string>();
      using (WordprocessingDocument doc = WordprocessingDocument.Open(MainForm.WordFileDialog.FileName, false))
      {
        List<Table> tables = doc.MainDocumentPart.Document.Descendants<Table>().ToList();
        foreach (Table table in tables)
        {
          TableRow headerRow = table.Elements<TableRow>().ElementAt(0);
          TableCell tableSectionTitle;
          try
          {
            tableSectionTitle = headerRow.Elements<TableCell>().ElementAt(0);
          }
          catch (ArgumentOutOfRangeException)
          {
            continue;
          }
            List<TableRow> rows = table.Descendants<TableRow>().ToList();
            foreach (TableRow row in rows)
            {
              TableCell headerCell = row.Elements<TableCell>().ElementAt(0);
              if (headerCell.InnerText.ToLower().Contains("first item"))
              {
                contents.Add("first item", row.Elements<TableCell>().ElementAt(1).InnerText);
              }
              else if (headerCell.InnerText.ToLower().Contains("second item:"))
              {
                char[] split = { ':' };
                Int32 count = 2;
                string str = row.Elements<TableCell>().ElementAt(0).InnerText;
                String[] newStr = str.Split(split, count, StringSplitOptions.None);
                contents.Add("second item:", newStr[1]);
              }
**continues for many more else if statements**
              else
              {
                continue;
              }
            }
        }
        return contents;
      }
    }

I'm new to using XML, so any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文