2014-02-24 49 views
0

我想從使用NPOI的.xlsx Excel文件中提取TEXT。NPOI:如何讀取沒有標題的excel單元格

下面是樣本Excel文件的鏈接:

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

功能ExcelDocumentToText提取的所有單元格的值,但不包含INR(小區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()); 
    } 

莫非細胞任何人都有這個查詢的一些解決方案?

+0

什麼是xlsReaderObject? – Sajeetharan

+0

它是XSSFWorkbook的一個對象... – user3324192

回答

0

ICell具有取決於CellType的值的不同屬性。 Yous可以使用類似於以下內容的東西:

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; 
} 
相關問題