OPENXML read excel

  • Thread starter Thread starter Vagelis Dermos
  • Start date Start date
V

Vagelis Dermos

Guest
hi to all,

i have this vode and i am trying to read an excel file.

my problem is that i cannot read blank cells


private void ReadExcel(string fileName)
{
try
{
using (SpreadsheetDocument spreadSheetDocument = SpreadsheetDocument.Open(fileName, false))
{
//string seperator = "";
//string line = "";
//int i = 0;
WorkbookPart workbookPart = spreadSheetDocument.WorkbookPart;
Workbook workbook = spreadSheetDocument.WorkbookPart.Workbook;
IEnumerable<Sheet> sheets = workbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>();
//pb2
//int pb2Count = 0;

foreach (WorksheetPart worksheetpart in workbook.WorkbookPart.WorksheetParts)
{
//pb2Count++;
Application.DoEvents();
Worksheet workSheet = worksheetpart.Worksheet;

string sheetName = workSheet.InnerText;
SheetData sheetData = workSheet.GetFirstChild<SheetData>();
IEnumerable<Row> rows = sheetData.Descendants<Row>();


foreach (Row row in rows) //this will also include your header row...
{
for (int i = 0; i < row.Descendants<Cell>().Count(); i++)
{
line = GetCellValue(spreadSheetDocument, row.Descendants<Cell>().ElementAt(i));
}
}
}
MessageBox.Show("End!!!", "Excel", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}



public static string GetCellValue(SpreadsheetDocument document, Cell cell)
{
SharedStringTablePart stringTablePart = document.WorkbookPart.SharedStringTablePart;
if (cell.CellValue != null)
{
string value = cell.CellValue.InnerXml;

if (cell.DataType != null && cell.DataType.Value == CellValues.SharedString)
{
return stringTablePart.SharedStringTable.ChildElements[Int32.Parse(value)].InnerText;
}
else
{
return value;
}
}
else
{
return "0";
}
}

Continue reading...
 
Back
Top