13

I am building a report using the microsoft.interop.excel library in C#.

I have something like this:

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

I want the range to display its values as whole numbers i.e. with no decimal places. I've tried rangeTarge.AutoFormat, but have no idea how to use it.

Any Ideas ?

Thanks.

Jonny
  • 2,787
  • 10
  • 40
  • 62
  • were you able to find a `NumberFormat` that represents the formatting you want? Also, is there anything else you need for this question to be resolved? – TMB Sep 14 '11 at 20:05
  • 1
    Lots of data type formatting examples here: https://support.office.com/en-us/article/Create-or-delete-a-custom-number-format-2d450d95-2630-43b8-bf06-ccee7cbe6864?ui=en-US&rs=en-US&ad=US – Gábor Imre Dec 12 '14 at 12:26

2 Answers2

16

I don't know what the other formats are but you can look on 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);
TMB
  • 4,683
  • 4
  • 25
  • 44
7

see 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();
}
TMB
  • 4,683
  • 4
  • 25
  • 44
  • I don't think this is going to answer you question, but I'll leave it here anyways. – TMB Sep 13 '11 at 14:06