【问题标题】:microsoft.interop.excel Formatting cellsmicrosoft.interop.excel 格式化单元格
【发布时间】:2011-09-13 21:56:35
【问题描述】:

我正在使用 C# 中的 microsoft.interop.excel 库构建报告。

我有这样的事情:

 Range rangeTarget;
 .
 .
 .
 rangeTarget = worksheet.get_Range("C" + row, "N" + row);

我希望范围将其值显示为整数,即没有小数位。 我试过 rangeTarge.AutoFormat,但不知道如何使用它。

有什么想法吗?

谢谢。

【问题讨论】:

  • 您能找到代表您想要的格式的NumberFormat 吗?另外,您还需要解决这个问题吗?
  • 这里有很多数据类型格式化示例:support.office.com/en-us/article/…

标签: c# excel formatting office-interop excel-interop


【解决方案1】:

我不知道其他格式是什么,但您可以查看the MSDN

Excel.Range ThisRange = ThisSheet.get_Range("A:A",system.type.missing);
ThisRange.NumberFormat = "0.00%";
ThisRange.NumberFormat = "General";    
ThisRange.NumberFormat = "hh:mm:ss";
ThisRange.NumberFormat = "DD/MM/YYYY";

Marshal.FinalReleaseComObject(ThisRange);

【讨论】:

  • @Xtian11 - 这是一个非常有用的评论。
【解决方案2】:

MSDN

Microsoft.Office.Tools.Excel.NamedRange namedRange1 =
this.Controls.AddNamedRange(this.Range["A1", "A5"], "namedRange1");

namedRange1.NoteText("This is a Formatting test", missing, missing);
namedRange1.Value2 = "Martha";
namedRange1.Font.Name = "Verdana";
namedRange1.VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
namedRange1.HorizontalAlignment = Excel.XlHAlign.xlHAlignCenter;
namedRange1.BorderAround(missing, Excel.XlBorderWeight.xlThick,
    Excel.XlColorIndex.xlColorIndexAutomatic, missing);
namedRange1.AutoFormat(Excel.XlRangeAutoFormat.xlRangeAutoFormat3DEffects1,
    true, false, true, false, true, true);

if (MessageBox.Show("Clear the formatting and notes?", "Test",
    MessageBoxButtons.YesNo) == DialogResult.Yes)
{
    namedRange1.ClearFormats();
    namedRange1.ClearNotes();
}

【讨论】:

  • 我不认为这会回答你的问题,但我还是把它留在这里。
猜你喜欢
  • 2010-10-20
  • 2016-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-28
  • 1970-01-01
  • 2011-08-16
相关资源
最近更新 更多