Search code examples
c#.netoffice-interopoffice-automation

Extracting excel worksheet into a string in C#


How can i extract excel worksheet into a string using C#? i already have the sheet and can save as txt file but instead of that I want to extract it into a string directly and do some processing.


Solution

  • Something like the following (untested) code should work:

    Excel.Application oXL= new Excel.Application();
    oXL.Visible = true;
    Excel._Workbook oWB = (Excel._Workbook)(oXL.Workbooks.Add( Missing.Value ));
    Excel._Worksheet oSheet = (Excel._Worksheet)oWB.ActiveSheet;
    string s = (string)oSheet.Cells[1, 1].Value;
    

    Though look at this sample to see how to clean up everything properly etc.