Skip to content

Instantly share code, notes, and snippets.

@AndrewBarfield
Created April 30, 2012 11:39
Show Gist options
  • Save AndrewBarfield/2557544 to your computer and use it in GitHub Desktop.
Save AndrewBarfield/2557544 to your computer and use it in GitHub Desktop.
C#: Automating MS Excel to create a new Workbook and fill it with data
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;
namespace ExcelTest
{
class Program
{
static void Main( string[] args )
{
ApplicationClass xla = null;
Workbook wb = null;
Worksheet dataWorkSheet = null;
Chart temperatureChart = null;
Chart humidityChart = null;
try
{
xla = new ApplicationClass();
xla.Visible = true;
// WorkBook
wb = xla.Workbooks.Add( XlSheetType.xlWorksheet );
wb.Author = "Andrew M. Barfield";
// Default Worksheet
dataWorkSheet = (Worksheet)wb.Sheets[1];
dataWorkSheet.Name = "Data";
Range rgDateStampColumn = dataWorkSheet.get_Range( "A1", Type.Missing );
rgDateStampColumn.EntireColumn.ColumnWidth = 20;
rgDateStampColumn.EntireColumn.NumberFormat = "[$-F400]h:mm:ss AM/PM";
dataWorkSheet.Cells[1, 1] = "Date/Time";
for ( int x = 2 ; x < 22 ; x++ )
{
dataWorkSheet.Cells[x, 1] = DateTime.Now;
}
Range rgTempColumn = dataWorkSheet.get_Range( "B1", Type.Missing );
rgTempColumn.EntireColumn.ColumnWidth = 20;
rgTempColumn.EntireColumn.NumberFormat = "###.###";
dataWorkSheet.Cells[1, 2] = "Temperature";
for ( int x = 2 ; x < 22 ; x++ )
{
dataWorkSheet.Cells[x, 2] = new Random().NextDouble() * 100;
}
Range rgHumidityColumn = dataWorkSheet.get_Range( "C1", Type.Missing );
rgHumidityColumn.EntireColumn.ColumnWidth = 20;
//rgHumidityColumn.EntireColumn.NumberFormat = "0.000%";
rgHumidityColumn.EntireColumn.NumberFormat = "###.###";
dataWorkSheet.Cells[1, 3] = "Humidity";
for ( int x = 2 ; x < 22 ; x++ )
{
dataWorkSheet.Cells[x, 3] = new Random().NextDouble();
}
//Worksheet ws = (Worksheet)currentWorkBook.Sheets.Add( Type.Missing, Type.Missing, 1, XlSheetType.xlWorksheet );
//ws.Name = wsName;
// Add Humidity Chart
humidityChart = (Chart)wb.Sheets.Add( Type.Missing, Type.Missing, Type.Missing, XlSheetType.xlChart );
humidityChart.ChartType = XlChartType.xlLine;
humidityChart.Name = "Humidity";
// Add Temperature Chart
temperatureChart = (Chart)wb.Sheets.Add( humidityChart, Type.Missing, Type.Missing, XlSheetType.xlChart );
temperatureChart.ChartType = XlChartType.xlLine;
temperatureChart.Name = "Temperature";
}
catch
{
}
finally
{
//
Console.In.ReadLine();
//
xla.DisplayAlerts = false;
if ( humidityChart != null )
{
Marshal.ReleaseComObject( humidityChart );
humidityChart = null;
}
if ( temperatureChart != null )
{
Marshal.ReleaseComObject( temperatureChart );
temperatureChart = null;
}
if ( dataWorkSheet != null )
{
Marshal.ReleaseComObject( dataWorkSheet );
dataWorkSheet = null;
}
if ( wb != null )
{
Marshal.ReleaseComObject( wb );
wb = null;
}
if ( xla != null )
{
xla.Quit();
Marshal.ReleaseComObject( xla );
xla = null;
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment