简体   繁体   English

如何在电子表格中使用C#获取单元格的格式类型

[英]How to get format type of cell using c# in spreadsheetlight

I am using spreadsheetlight library to read Excel sheet(.xslx) values using c#. 我正在使用试算表库使用c#读取Excel工作表(.xslx)值。

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 注意:对于.xls类型的excel文件,我正在使用ExcelLibrary.dll库,在其中可以使用以下代码轻松获取单元格的数据类型

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

but there is no similar method in spreadsheetlight. 但是电子表格中没有类似的方法。

Here is the answer from the developer Vincent Tang after I asked him as I wasn't sure how to use DataType: 这是我不确定他如何使用DataType后,问他后开发人员Vincent Tang的回答:


Yes use SLCell.DataType. 是的,请使用SLCell.DataType。 It's an enumeration, but for most data, you'll be working with Number, SharedString and String. 这是一个枚举,但是对于大多数数据,您将使用Number,SharedString和String。

Text data will be SharedString, and possibly String if the text is directly embedded in the worksheet. 文本数据将为SharedString,如果文本直接嵌入工作表中,则可能为String。 There's a GetSharedStrings() or something like that. 有一个GetSharedStrings()或类似的东西。

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). 数据类型也是Number(忽略日期枚举,因为Microsoft Excel未使用它)。 For dates, you also have to check the FormatCode, which is in the SLStyle for the SLCell. 对于日期,您还必须检查SLCell的SLStyle中的FormatCode。 Use the GetStyles() to get a list. 使用GetStyles()获取列表。 The SLCell.StyleIndex gives you the index to that list. SLCell.StyleIndex为您提供该列表的索引。

For example, if your SLCell has cell value "15" and data type SharedString, then look for index 15 in the list of shared strings. 例如,如果您的SLCell的单元格值为“ 15”且数据类型为SharedString,则在共享字符串列表中查找索引15。 If it's "blah" with String data type, then that's it. 如果它是String类型的数据,就这样。

If it's 56789 with Number type, then that's it. 如果数字类型是56789,那么就是这样。

Unless the FormatCode is "mm-yyyy" (or some other date format code), then 56789 is actually the number of days since 1 Jan 1900. 除非FormatCode是“ mm-yyyy”(或其他日期格式代码),否则56789实际上是自1900年1月1日以来的天数。


He also recommended using GetCellList() in order to obtain the list of SLCell objects in the sheet. 他还建议使用GetCellList()以获得工作表中SLCell对象的列表。 However, for some reason that function was not available in my version of SL, so I used GetCells() instead. 但是,由于某种原因,该功能在我的SL版本中不可用,所以我改用GetCells()。 That returns a dictionary of SLCell objects, with keys of type SLCellPoint. 这将返回SLCell对象的字典,其键类型为SLCellPoint。

So for example to get the DataType (which is a CellValues object) of cell A1 do this: 因此,例如,要获取单元格A1的DataType(这是一个CellValues对象),请执行以下操作:

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. 顺便说一句,我在打开chm帮助文件时也遇到了问题。 Try this: 尝试这个:

  • Right Click on the chm file and select properties 右键单击chm文件并选择属性
  • 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. 签出SLCell.DataType属性。 Spreadsheetlight documentation mentions that this returns the Cell datatype, in class Spreadsheetlight.SLCell Spreadsheetlight文档提到在Spreadsheetlight.SLCell类中它返回Cell数据类型。

public CellValues DataType { get; set; }

PS: On a side note, I figured out how to open the chm documentation. PS:在旁注中,我想出了如何打开chm文档。 Try opening the chm file in Winzip, it opens without any issues. 尝试在Winzip中打开chm文件,该文件不会出现任何问题。

Hope it helps. 希望能帮助到你。 Thanks 谢谢

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. 根据单元格的formatCode ,我们可以确定单元格的formatType

Using GetCellStyle method we can get the formatcode of the cell. 使用GetCellStyle方法,我们可以获取单元格的格式代码 Using this formatCode we can decide the formatType . 使用这个formatCode,我们可以确定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. 这种方法适用于99%的情况。

Hope it helps you. 希望对您有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM