如何将 Word table 单元格中的列表复制到 Excel 单元格
How to copy a list in a Word table cell to Excel cell
我在 Word 中进行了以下测试 table,一个单元格有一个多级列表:
使用下面的代码,我可以将 Word Table 中的单元格复制到 Excel 工作表中的相应单元格:
foreach (Microsoft.Office.Interop.Word.Table table in objDoc.Tables)
{
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 1; col <= table.Columns.Count; col++)
{
string text = table.Cell(row, col).Range.Text;
worksheet.Cells[row, col] = text;
}
}
}
但是,我得到以下结果,其中包含列表的 Word 单元格未正确复制到 Excel:
我也试过以下方法:
worksheet.Cells[row, col] = table.Cell(row, col).Range.FormattedText;
但我得到了相同的结果。
我也试过转换Word文件中的列表,通过复制和粘贴仅保留文本来删除Word的自动格式,并手动删除选项卡。产生了这个结果:
虽然我可以得到带有列表编号的文本,但我没有得到回车 return、换行符或换行符来分隔列表中的项目。
至少,我想保留列表编号和换行符,而不必手动 cut/paste 使用仅保留文本;我想避免必须解析列表编号(可以是数字或字母)的文本并插入换行符。
实现所述结果涉及多个问题:
Excel 不使用与 Word 相同的字符来换行或换段落。 (在这种情况下,它必须是新段落,因为正在生成编号。)Excel 需要 ANSI 10; Word 使用的是 ANSI 13。因此需要进行转换。
自动行编号正在格式化。传递字符串会丢失格式;它只能使用复制进行。或者必须将编号转换为纯文本。
另一个问题是单元格内容末尾的 "dot",它又是 ANSI 13 与 ANSI 7(单元格结束标记)的组合。这也应该被删除。
下面的示例代码负责所有三种转换。 (注意:这是 VBA 代码,我已经从头顶转换了,所以要注意小语法 "gotchas")
Word.Range rng = table.Cell[rowCounter, colCounter].Range;
//convert the numbers to plain text, then undo the conversion
rng.ListFormat.ConvertNumbersToText();
string cellContent = rng.Text;
objDoc.Undo(1);
//remove end-of-cell characters
cellContent = TrimCellText2(cellContent);
//replace remaining paragraph marks with the Excel new line character
cellContent.Replace((char)13, (char)10);
worksheet.Cells[rowCounter, colCounter].Value = cellContent;
//cut off ANSI 13 + ANSI 7 from the end of the string coming from a
//Word table cell
private string TrimCellText2(s As String)
{
int len = s.Length;
while (len > 0 && s.Substring(len - 1) == (char)13 || s.Substring(len - 1) == (char)7);
s = s.Substring(0, Math.Min(len-1, len));
return s;
}
在 Cindy Meister 的帮助下,结合 Paul Walls 在 replacing characters in a C# string 的另一个问题中的回答,这里是最终的答案。
foreach (Microsoft.Office.Interop.Word.Table table in objDoc.Tables)
{
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 1; col <= table.Columns.Count; col++)
{
// Convert the formatted list number to plain text, then undo the conversion
table.Cell(row, col).Range.ListFormat.ConvertNumbersToText();
string cellContent = table.Cell(row, col).Range.Text;
objDoc.Undo(1);
// remove end-of-cell characters
cellContent = trimCellText2(cellContent);
// Replace remaining paragraph marks with the excel newline character
char[] linefeeds = new char[] { '\r', '\n' };
string[] temp1 = cellContent.Split(linefeeds, StringSplitOptions.RemoveEmptyEntries);
cellContent = String.Join("\n", temp1);
// Replace tabs from the list format conversion with spaces
char[] tabs = new char[] { '\t', ' ' };
string[] temp2 = cellContent.Split(tabs, StringSplitOptions.RemoveEmptyEntries);
cellContent = String.Join(" ", temp2);
worksheet.Cells[row, col] = cellContent;
}
}
}
private static string trimCellText2(string myString)
{
int len = myString.Length;
string charString13 = "" + (char)13;
string charString7 = "" + (char)7;
while ((len > 0 && myString.Substring(len - 1) == charString13) || (myString.Substring(len - 1) == charString7))
myString = myString.Substring(0, Math.Min(len - 1, len));
return myString;
}
下面是 Excel 中的结果输出:Excel Output
我在 Word 中进行了以下测试 table,一个单元格有一个多级列表:
使用下面的代码,我可以将 Word Table 中的单元格复制到 Excel 工作表中的相应单元格:
foreach (Microsoft.Office.Interop.Word.Table table in objDoc.Tables)
{
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 1; col <= table.Columns.Count; col++)
{
string text = table.Cell(row, col).Range.Text;
worksheet.Cells[row, col] = text;
}
}
}
但是,我得到以下结果,其中包含列表的 Word 单元格未正确复制到 Excel:
我也试过以下方法:
worksheet.Cells[row, col] = table.Cell(row, col).Range.FormattedText;
但我得到了相同的结果。
我也试过转换Word文件中的列表,通过复制和粘贴仅保留文本来删除Word的自动格式,并手动删除选项卡。产生了这个结果:
虽然我可以得到带有列表编号的文本,但我没有得到回车 return、换行符或换行符来分隔列表中的项目。
至少,我想保留列表编号和换行符,而不必手动 cut/paste 使用仅保留文本;我想避免必须解析列表编号(可以是数字或字母)的文本并插入换行符。
实现所述结果涉及多个问题:
Excel 不使用与 Word 相同的字符来换行或换段落。 (在这种情况下,它必须是新段落,因为正在生成编号。)Excel 需要 ANSI 10; Word 使用的是 ANSI 13。因此需要进行转换。
自动行编号正在格式化。传递字符串会丢失格式;它只能使用复制进行。或者必须将编号转换为纯文本。
另一个问题是单元格内容末尾的 "dot",它又是 ANSI 13 与 ANSI 7(单元格结束标记)的组合。这也应该被删除。
下面的示例代码负责所有三种转换。 (注意:这是 VBA 代码,我已经从头顶转换了,所以要注意小语法 "gotchas")
Word.Range rng = table.Cell[rowCounter, colCounter].Range;
//convert the numbers to plain text, then undo the conversion
rng.ListFormat.ConvertNumbersToText();
string cellContent = rng.Text;
objDoc.Undo(1);
//remove end-of-cell characters
cellContent = TrimCellText2(cellContent);
//replace remaining paragraph marks with the Excel new line character
cellContent.Replace((char)13, (char)10);
worksheet.Cells[rowCounter, colCounter].Value = cellContent;
//cut off ANSI 13 + ANSI 7 from the end of the string coming from a
//Word table cell
private string TrimCellText2(s As String)
{
int len = s.Length;
while (len > 0 && s.Substring(len - 1) == (char)13 || s.Substring(len - 1) == (char)7);
s = s.Substring(0, Math.Min(len-1, len));
return s;
}
在 Cindy Meister 的帮助下,结合 Paul Walls 在 replacing characters in a C# string 的另一个问题中的回答,这里是最终的答案。
foreach (Microsoft.Office.Interop.Word.Table table in objDoc.Tables)
{
for (int row = 1; row <= table.Rows.Count; row++)
{
for (int col = 1; col <= table.Columns.Count; col++)
{
// Convert the formatted list number to plain text, then undo the conversion
table.Cell(row, col).Range.ListFormat.ConvertNumbersToText();
string cellContent = table.Cell(row, col).Range.Text;
objDoc.Undo(1);
// remove end-of-cell characters
cellContent = trimCellText2(cellContent);
// Replace remaining paragraph marks with the excel newline character
char[] linefeeds = new char[] { '\r', '\n' };
string[] temp1 = cellContent.Split(linefeeds, StringSplitOptions.RemoveEmptyEntries);
cellContent = String.Join("\n", temp1);
// Replace tabs from the list format conversion with spaces
char[] tabs = new char[] { '\t', ' ' };
string[] temp2 = cellContent.Split(tabs, StringSplitOptions.RemoveEmptyEntries);
cellContent = String.Join(" ", temp2);
worksheet.Cells[row, col] = cellContent;
}
}
}
private static string trimCellText2(string myString)
{
int len = myString.Length;
string charString13 = "" + (char)13;
string charString7 = "" + (char)7;
while ((len > 0 && myString.Substring(len - 1) == charString13) || (myString.Substring(len - 1) == charString7))
myString = myString.Substring(0, Math.Min(len - 1, len));
return myString;
}
下面是 Excel 中的结果输出:Excel Output