I am trying to read excel using Open XML SDK in C# using Visual Studio 2013. I followed the following links http://msdn.microsoft.com/en-us/library/office/hh298534(v=office.15).aspx http://msdn.microsoft.com/en-us/library/office/gg575571.aspx
The code is
using (SpreadsheetDocument document = SpreadsheetDocument.Open(excelPath, true))
{
WorkbookPart workbookPart = document.WorkbookPart;
foreach (WorksheetPart workSheetPart in workbookPart.WorksheetParts)
{
SheetData sheetData = workSheetPart.Worksheet.Elements<SheetData>().First();
IEnumerable<Row> rows = sheetData.Elements<Row>().Where(x => x.RowIndex > 1);
foreach (Row r in rows)
{
IEnumerable<string> textValues = from cell in r.Descendants<Cell>() where cell.CellValue != null select cell.CellValue.Text;
foreach (var cell in textValues)
{
string str = cell.ToString();
}
}
}
}
I tried the following code also
using (SpreadsheetDocument document = SpreadsheetDocument.Open(excelPath, true))
{
WorkbookPart workbookPart = document.WorkbookPart;
foreach (WorksheetPart workSheetPart in workbookPart.WorksheetParts)
{
SheetData sheetData = workSheetPart.Worksheet.Elements<SheetData>().First();
IEnumerable<Row> rows = sheetData.Elements<Row>().Where(x => x.RowIndex > 1);
foreach (Row r in rows)
{
List<Cell> cells = r.Descendants<Cell>().ToList();
foreach (var cell in cells)
{
if (cell != null)
{
string value = cell.CellValue.Text;
if (cell.DataType != null)
{
switch (cell.DataType.Value)
{
case CellValues.SharedString:
var stringTable = workSheetPart.GetPartsOfType<SharedStringTablePart>().FirstOrDefault();
if (stringTable != null)
{
value = stringTable.SharedStringTable.ElementAt(int.Parse(value)).InnerText;
}
break;
}
}
}
}
}
}
}
But both are returning only numeric values, not text. Can anyone please help on how to read excel text using Open XML SDK in C#.?
I have not used to OpenXML SDK directly, but have you tried used ClosedXML ? It's a wrapper around the SDK that makes reading and writing Excel documents a breeze.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.