【问题标题】:Keep text formatting with Get_range in C#在 C# 中使用 Get_range 保持文本格式
【发布时间】:2014-07-05 23:53:51
【问题描述】:

我想将范围从 excel 文件复制到另一个文件,现在,我使用 get_Range 方法选择源数据,然后使用 Cells.Value2 获取数组。

然后我将此值放入目标文件的get_Range,但这样做不会保留文本格式。

有没有办法保持格式并更好地选择我想要保留的内容(比如只有文本格式而不是背景单元格格式,或边框等)?

【问题讨论】:

标签: c# excel


【解决方案1】:

是的,它不起作用,因为您只从初始范围中获取 .Value2,您还需要格式。因为如果没有,您的目标范围的格式将被考虑。 我认为你可以这样做:

Range initial_Range = initial_worksheet.get_Range(...);
Range destination_Range = destination_worksheet.get_Range(...);

destination_Range = initial_Range;

在新的范围内拥有所有:numberFormat、backgroundColor ...您的初始范围,

或者使用类似的东西:

destination_Range.Value2 = initial_Range.Value2;
destination_Range.Font.ColorIndex = initial_Range.Font.ColorIndex;
destination_Range.Interior.ColorIndex = initial_Range.Interior.ColorIndex;
destination_Range.Interior.Pattern = initial_Range.Interior.Pattern;
destination_Range.NumberFormat = initial_Range.NumberFormat;

只有你需要的,你可以选择实例化 NumberFormat 而不是背景...

(如果这不适用于一次总范围,您可以在目标和初始范围中循环单元格)

【讨论】:

  • 我尝试了您的解决方案,但文本仍然没有任何样式(就像我试图复制一些删除的文本但它不起作用。)
  • 我也尝试了 Copy、PasteSpecial 方法,但复制的单元格被粘贴为图像。
  • 我找到了解决这个问题的方法:stackoverflow.com/questions/16990946/…
猜你喜欢
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 2013-02-09
  • 2013-08-10
  • 2022-01-02
  • 1970-01-01
  • 2018-01-08
相关资源
最近更新 更多