-1

I would like to be able to open an excel document and loop through the values in the cells using Open XML in C#.

Tried using the below code, but it never gets past the Foreach (Row...). Also comments state this is for numeric values not Alpha Numeric values.

    using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(Filedirectory, false))
    {
        WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
        WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();
        SheetData sheetData = worksheetPart.Worksheet.Elements<SheetData>().First();
        string text;
        foreach (Row r in sheetData.Elements<Row>())
        {
            foreach (Cell c in r.Elements<Cell>())
            {
                text = c.CellValue.Text;
            }
        }
    }
Mistlin
  • 69
  • 1
  • 6
  • 3
    Have you googled this at all? – System Down Sep 20 '13 at 20:19
  • Don't reinvent the wheel, see: http://stackoverflow.com/questions/151005/create-excel-xls-and-xlsx-file-from-c-sharp. Many of those tools also support reading values from Excel. – Anthony Sep 20 '13 at 20:19
  • Check this : http://msdn.microsoft.com/en-us/library/office/gg575571.aspx –  Sep 20 '13 at 20:22
  • This was helpful Precious but for some reason it doesn't read the values. Also there is a comment that it is only for numeric values. – Mistlin Sep 20 '13 at 21:05

1 Answers1

-1

A coworker came up with the following solution.

            FileInfo uploadedFile = new FileInfo(FileDirectory);
            using (ASC.ExcelPackage.ExcelPackage xlPackage = new ASC.ExcelPackage.ExcelPackage(uploadedFile))
            {
                ASC.ExcelPackage.ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];
                Int32 currentRow = 1;
                while (worksheet.Cell(currentRow, 1) != null && !string.IsNullOrEmpty(worksheet.Cell(currentRow, 1).Value))
                {
                    string value = worksheet.Cell(currentRow, 1).Value;

                    currentRow++;
                }
            }
Mistlin
  • 69
  • 1
  • 6