0

I am trying to extract TEXT from a .xlsx Excel file using NPOI.

Below is the link of sample Excel file:

http://s29.postimg.org/i7rtrucaf/excel_File.png

The function ExcelDocumentToText extracts all the cell values, but not the cell which contains INR (cell F2)

    static void ExcelDocumentToText(string path = @"..\..\..\01.xlsx")
    {
        StringBuilder textOfExcelDocumnet = new StringBuilder();
        ISheet sheet = null; IRow headerRow = null; IEnumerator allRows = null;

        using (FileStream ExcelFile = new FileStream(path, FileMode.Open, FileAccess.Read))
            xlsReaderObject = new XSSFWorkbook(ExcelFile);

        for (int j = 0; j < xlsReaderObject.Count; j++)
        {
            sheet = xlsReaderObject.GetSheetAt(j);
            for (int p = 0; p < sheet.LastRowNum; p++)
            {
                headerRow = sheet.GetRow(p);
                if (headerRow != null)
                    break;
            }
            allRows = sheet.GetRowEnumerator();

            int colCount = headerRow.LastCellNum;

            while (allRows.MoveNext())
            {
                //IRow row = (HSSFRow)rows.Current;
                IRow row = (XSSFRow)allRows.Current;
                for (int i = 0; i < colCount; i++)
                {
                    ICell cell = row.GetCell(i);
                    if (cell != null)
                        textOfExcelDocumnet.AppendLine(cell.ToString());
                }
            }
            sheet = null; headerRow = null; allRows = null;
        }
        xlsReaderObject = null;

        Console.WriteLine(textOfExcelDocumnet.ToString());
    }

Could anyone please have some solution of this query?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

ICell has different properties for the value depending on the CellType. Yous can use something similar to the following:

switch (cell.CellType)
{ 
    case CellType.Blank:
        return string.Empty;
    case CellType.Boolean:
        return cell.BooleanCellValue;
    case CellType.Formula:
        return cell.CellFormula;
    case CellType.Numeric:
        return cell.NumericCellValue;
    case CellType.String:
        return cell.StringCellValue;
    default:
        return string.Empty;
}
David Robbins
  • 9,996
  • 7
  • 51
  • 82