Question

I am using spreadsheetlight library to read Excel sheet(.xslx) values using c#.

I can read the cell value using following code

 for (int col = stats.StartColumnIndex; col <= stats.EndColumnIndex; col++)
     {              
         var value= sheet.GetCellValueAsString(stats.StartRowIndex, col);  //where sheet is current sheet in excel file    
     }

I am getting the cell value. But how can I get the data type of the cell? I have checked in documentation but didn't find the solution.

Note: For .xls type of excel files i am using ExcelLibrary.dll library where i can easily get the datatype of cells using below code

for (int i = 0; i <= cells.LastColIndex; i++)
     {
         var type = cells[0, i].Format.FormatType;
     }

but there is no similar method in spreadsheetlight.

Was it helpful?

Solution 4

Well, After a lot of trail and error methods i got to find a solution for this.

Based on the formatCode of a cell we can decide the formatType of the cell.

Using GetCellStyle method we can get the formatcode of the cell. Using this formatCode we can decide the formatType.

var FieldType = GetDataType(sheet.GetCellStyle(rowIndex, columnIndex).FormatCode);


private string GetDataType(string formatCode)
    {
        if (formatCode.Contains("h:mm") || formatCode.Contains("mm:ss"))
        {
            return "Time";
        }
        else if (formatCode.Contains("[$-409]") || formatCode.Contains("[$-F800]") || formatCode.Contains("m/d"))
        {
            return "Date";
        }
        else if (formatCode.Contains("#,##0.0"))
        {
            return "Currency";
        }
        else if (formatCode.Last() == '%')
        {
            return "Percentage";
        }
        else if (formatCode.IndexOf("0") == 0)
        {
            return "Numeric";
        }
        else
        {
            return "String";
        }
    } 

This method worked for 99% of the cases.

Hope it helps you.

OTHER TIPS

Here is the answer from the developer Vincent Tang after I asked him as I wasn't sure how to use DataType:


Yes use SLCell.DataType. It's an enumeration, but for most data, you'll be working with Number, SharedString and String.

Text data will be SharedString, and possibly String if the text is directly embedded in the worksheet. There's a GetSharedStrings() or something like that.

For numeric data, it will be Number.

For dates, it's a little tricky. The data type is also Number (ignore the Date enumeration because Microsoft Excel isn't using it). For dates, you also have to check the FormatCode, which is in the SLStyle for the SLCell. Use the GetStyles() to get a list. The SLCell.StyleIndex gives you the index to that list.

For example, if your SLCell has cell value "15" and data type SharedString, then look for index 15 in the list of shared strings. If it's "blah" with String data type, then that's it.

If it's 56789 with Number type, then that's it.

Unless the FormatCode is "mm-yyyy" (or some other date format code), then 56789 is actually the number of days since 1 Jan 1900.


He also recommended using GetCellList() in order to obtain the list of SLCell objects in the sheet. However, for some reason that function was not available in my version of SL, so I used GetCells() instead. That returns a dictionary of SLCell objects, with keys of type SLCellPoint.

So for example to get the DataType (which is a CellValues object) of cell A1 do this:

using (SLDocument slDoc = new SLDocument("Worksheet1.xlsx", "Sheet1")) {

    slCP = SLCellPoint;
    slCP.ColumnIndex = SLConvert.ToColumnIndex("A"); ///Obviously 1 but useful function to know
    slCP.RowIndex = 1;

    CellValues slCV = slDoc.GetCells(slCP).DataType;

}

By the way, I also had a problem with opening the chm help file. Try this:

  • Right Click on the chm file and select properties
  • Click on Unblock button at the bottom of the General Tab

To get the value of the cell try following code

 var cellValue = (string)(excelWorksheet.Cells[10, 2] as Excel.Range).Value;

Use this link for more details

Check out the SLCell.DataType Property. Spreadsheetlight documentation mentions that this returns the Cell datatype, in class Spreadsheetlight.SLCell

public CellValues DataType { get; set; }

PS: On a side note, I figured out how to open the chm documentation. Try opening the chm file in Winzip, it opens without any issues.

Hope it helps. Thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top