tags:

views:

245

answers:

2

I'm using late binding to launch Excel from C# and copy data into it (Type.GetTypeFromProgID("Excel.Application"), Activator.CreateInstance, InvokeMember, and all that). I have this working fine, but I can't figure out how to set the cell format, specifically to make a cell bold. anyone have any idea how to do this?

+1  A: 

I'm not sure how to do it late binding, but if you created an Excel helper class, this is probably the easiest way do what you want (I also added in color and number formating):

using System;
using Excel = Microsoft.Office.Interop.Excel;

public class MyClass
{
    public void FormatRange(Excel.Worksheet sheet)
    {
     Excel.Range range = sheet.Cells["1","A"];
     range.Interior.ColorIndex = 15;//This sets it to gray
     range.Font.Bold = true;//Sets the Bold
     range.NumberFormat = "@";//Sets it to numeric
    }
}

Cheers!

Erick
A: 

SpreadsheetGear for .NET will let you read, modify and write Excel workbooks without relying on Excel being installed. If you are calling very many APIs, it will also run much faster than using Excel via COM Interop. Here is an example which loads a workbook, sets a cell's font to bold and saves the workbook:

        SpreadsheetGear.IWorkbook workbook = SpreadsheetGear.Factory.GetWorkbook(@"C:\tmp\MyWorkbook.xlsx");
        workbook.Worksheets["Sheet1"].Cells["A1"].Font.Bold = true;
        workbook.Save();

You can see a number of live samples here and download the free trial here.

Disclaimer: I own SpreadsheetGear LLC

Joe Erickson