【问题标题】:How to get format type of cell using c# in spreadsheetlight如何在电子表格中使用 c# 获取单元格的格式类型
【发布时间】:2014-03-24 14:26:34
【问题描述】:

我正在使用电子表格库来使用 c# 读取 Excel 工作表 (.xslx) 值。

我可以使用以下代码读取单元格值

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

我正在获取单元格值。但是我怎样才能得到单元格的数据类型呢?我已经检查了文档,但没有找到解决方案。

注意:对于 .xls 类型的 excel 文件,我使用的是 ExcelLibrary.dll 库,我可以使用以下代码轻松获取单元格的数据类型

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

但在电子表格中没有类似的方法。

【问题讨论】:

  • 来自电子表格网站的 chm 文档文件无法正确打开。任何文档都可以在线获得?
  • 是的。我也下载了试了一下。但无法正常打开。
  • 您需要解锁 zip 文件才能使其正常工作。右键,属性,解锁。

标签: c# excel openxml spreadsheetlight


【解决方案1】:

以下是开发者 Vincent Tang 在我不知道如何使用 DataType 后问他的答案:


是的,使用 SLCell.DataType。这是一个枚举,但对于大多数数据,您将使用 Number、SharedString 和 String。

文本数据将是 SharedString,如果文本直接嵌入工作表中,则可能是 String。有一个 GetSharedStrings() 或类似的东西。

对于数字数据,它将是 Number。

对于日期,这有点棘手。数据类型也是 Number(忽略 Date 枚举,因为 Microsoft Excel 没有使用它)。对于日期,您还必须检查 FormatCode,它位于 SLCell 的 SLStyle 中。使用 GetStyles() 获取列表。 SLCell.StyleIndex 为您提供该列表的索引。

例如,如果您的 SLCell 具有单元格值“15”和数据类型 SharedString,则在共享字符串列表中查找索引 15。如果它是 String 数据类型的“blah”,那么就是这样。

如果是 Number 类型的 56789,那么就是这样。

除非 FormatCode 是“mm-yyyy”(或其他日期格式代码),否则 56789 实际上是自 1900 年 1 月 1 日以来的天数。


他还建议使用 GetCellList() 来获取工作表中的 SLCell 对象列表。但是,由于某种原因,该功能在我的 SL 版本中不可用,所以我改用 GetCells()。这将返回 SLCell 对象的字典,其键类型为 SLCellPoint。

例如,要获取单元格 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;

}

顺便说一句,我在打开 chm 帮助文件时也遇到了问题。试试这个:

  • 右键单击chm文件并选择属性
  • 单击“常规”选项卡底部的“取消阻止”按钮

【讨论】:

    【解决方案2】:

    要获取单元格的值,请尝试以下代码

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

    使用this链接了解更多详情

    【讨论】:

    • 您好。感谢您的回复。我想读取单元格格式类型。
    • 试试this链接
    • 这是您个人资料的链接。我可以在那里尝试什么?
    • 在C#中获取Excel单元格的数据类型
    【解决方案3】:

    查看 SLCell.DataType 属性。 Spreadsheetlight 文档提到这将返回 Cell 数据类型,在类 Spreadsheetlight.SLCell

    public CellValues DataType { get; set; }
    

    PS:顺便说一句,我想出了如何打开 chm 文档。尝试在 Winzip 中打开 chm 文件,它打开没有任何问题。

    希望对您有所帮助。谢谢

    【讨论】:

      【解决方案4】:

      好吧,经过大量的跟踪和错误方法后,我找到了解决方案。

      根据单元格的formatCode,我们可以决定单元格的formatType

      使用GetCellStyle方法我们可以得到单元格的formatcode。使用这个 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";
              }
          } 
      

      这种方法适用于 99% 的案例。

      希望对你有帮助。

      【讨论】:

        猜你喜欢
        • 2012-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-03-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多