I have most of the problem solved. I can loop through the worksheets, I can create the text file and write to it. The only problem I'm having is actually extracting the text or values from every sheet. Below is the code I have, but it only throws an object per worksheet and writes those words into my text file instead of the actual values.
System.Object[,]
System.Object[,]
What else am I missing? I should point out I'm a beginner programmer.
Here's the code I have so far:
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? Thanks!
Something like this, not tested. 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();
}
}
}
You need to get the data fron the sheet, for example:
sw.WriteLine(sheet.Range["C5"].Value.ToString());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With