Scroll through Excel sheets and save the text in a TXT file using C #

I have solved most of the problem. I can scroll through the sheets, I can create a text file and write to it. The only problem I ran into is extracting text or values ​​from each sheet. Below is the code, but it only throws the object onto the worksheet and writes these words to a text file instead of the actual values.

System.Object[,]
System.Object[,]

What else am I missing? I must indicate that I am a beginner programmer.

Here is the code that I still have:

using (StreamWriter sw = File.CreateText("ExtractedText.txt"))
{
    Excel.Application xlApp = new Excel.Application();
    Excel.Workbook thisWkBook = xlApp.Workbooks.Open(thisFile);

    foreach (Excel.Worksheet sheet in thisWkBook.Worksheets)
    {
        sw.WriteLine(sheet.UsedRange.Value);
    }
}

Any ideas? Thank!

+5
source share
2 answers

Something like this is not verified. See http://dontbreakthebuild.com/2011/01/30/excel-and-c-interop-with-net-4-how-to-read-data-from-excel/

using (StreamWriter sw = File.CreateText("ExtractedText.txt"))
{
    var excelApp = new Excel.Application();
    var workBook = excelApp.Workbooks.Open(thisFile);

    foreach (var sheet in workBook.Worksheets)
    {
      foreach (var row in sheet.UsedRange.Rows)
      {
        foreach (var cell in row.Columns)
        {
          sw.Write(cell.Value + " ");
        } 
        sw.WriteLine();
      }
    }
}
+3
source

You need to get the data on the sheet, for example:

sw.WriteLine(sheet.Range["C5"].Value.ToString());
+1
source

All Articles