Create an Excel file programmatically

Create an Excel file programmatically.

For C# you will need to create an Excel assembly for interoperability:

  1. open a .NET cmd box (see the Visual Studio .Net Tools menu)
  2. find the Excel9.olb file in your Excel program directory
  3. copy excel9.olb to your project directory
  4. cd to your project directory and run 'TlbImp Excel9.olb Excel.dll'
  5. add excel.dll to the references


Download createexcel.zip

Synopsis:

createexcel.cs


createexcel.cs

Synopsis
using System;

//This is created from the interop tlbimp tool.
// 1) open a .NET cmd box (see the Visual Studio .Net Tools menu)
// 2) find the Excel9.olb file in your Excel program directory 
// 3) copy excel9.olb to your project directory
// 4) cd to your project directory and run 'TlbImp Excel9.olb Excel.dll'
// 5) add excel.dll to the references
 
using Excel;
using System.Reflection; // For Missing.Value and BindingFlags
using System.Runtime.InteropServices; // For COMException

namespace csharp
{
  /// <summary>
  /// Summary description for createexcel.
  /// </summary>
  public class createexcel
  {
    public createexcel()
    {
      ExcelHandler xl = new ExcelHandler();

      xl.Create();

      xl.SetCell("A1", "Title1 - really really long title that is too long");
      xl.SetCell("B1", "Title2 - really really long title that is too long");
      xl.SetCell("C1", "Title3");

      xl.SetBold("A1");
      xl.SetWrapText("A1");
      xl.SetBold("B1");
      xl.SetWrapText("B1");
      xl.SetColumnWidth("B1", 30);
      xl.SetBold("C1");

      xl.SetCell("A2", 2);
      xl.SetCell("B2", 3.2);  //floats are ok
      xl.SetCell("C2", 1);
      xl.SetCell("D2", "@sum(A2:C2)"); // formulas are just strings...
      xl.Save(@"x.xls");

      xl.Quit();
    }
  }

  //does the actual work
  public class ExcelHandler
  {
    Excel.Application exc;
    _Workbook workbook;
    _Worksheet worksheet;
    
    public void Quit()
    {
      exc.Quit();
    }
    public void Save(string fname)
    {
      exc.AlertBeforeOverwriting = true;
      workbook.SaveAs(fname, Excel.XlFileFormat.xlWorkbookNormal, null, null, null, null, Excel.XlSaveAsAccessMode.xlExclusive, null, null, null, null);
    }
    public void Create()
    {
      exc = new Excel.Application();
      if (exc == null) 
      {
        Console.WriteLine("ERROR: EXCEL couldn't be started");
        return;
      }
      exc.Visible = true;

      Workbooks workbooks = exc.Workbooks;
      workbook = workbooks.Add(XlWBATemplate.xlWBATWorksheet);

      Sheets sheets = workbook.Worksheets;
      worksheet = (_Worksheet) sheets.get_Item(1);
      if (worksheet == null) 
      {
        Console.WriteLine ("ERROR in worksheet == null");
      }
    }

    public void SetCell(string address, object val)
    {
      Range range1 = worksheet.get_Range(address, Missing.Value);
      if (range1 == null) 
      {
        Console.WriteLine ("ERROR: range == null");
      }
      Object[] args1 = new Object[1];
      args1[0] = val;
      range1.GetType().InvokeMember("Value", BindingFlags.SetProperty, null, range1, args1);

    }
    public void SetBold(string address)
    {
      Range range1 = worksheet.get_Range(address, Missing.Value);
      if (range1 == null) 
      {
        Console.WriteLine ("ERROR: range == null");
      }
      range1.Font.Bold = true; 
    }
    public void SetWrapText(string address)
    {
      Range range1 = worksheet.get_Range(address, Missing.Value);
      if (range1 == null) 
      {
        Console.WriteLine ("ERROR: range == null");
      }

      range1.WrapText = true; 
    }
    public void SetColumnWidth(string address, int width)
    {
      Range range1 = worksheet.get_Range(address, Missing.Value);
      if (range1 == null) 
      {
        Console.WriteLine ("ERROR: range == null");
      }

      range1.ColumnWidth = width; 
    }
  }
}






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001,2002,2003,2004,2005,2006,2007