Export to Excel in C#
October 12, 2009
This is a very simple snippet showing how to use the Microsoft.Office.Interop.Excel assembly (version 12 for this example).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var app = new ApplicationClass(); | |
var book = app.Workbooks.Open(@"C:\test.xlsx", false, false, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, | |
Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value); | |
var sheet = (Worksheet) book.Worksheets[1]; | |
var range = sheet.get_Range("D7", "D127"); | |
var cell = (Range) range.Cells[1, 1]; // all indexes are 1 based. This gets the first row, column | |
string val = cell.Value2.ToString(); |
I'm Chris Small, a software engineer working in London. This is my tech blog. Find out more about me via Github, Stackoverflow, Resume