简体   繁体   English

在C#中使用Excel互操作获取Range的NumberFormat

[英]Getting NumberFormat of Range using Excel interop in C#

I am trying to use Microsoft.Office.Interop.Excel to read excel file in c#. 我正在尝试使用Microsoft.Office.Interop.Excel读取c#中的excel文件。 I want to read range of cells as it is way faster than reading cell one by one: 我想读取单元格范围,因为它比一一读取单元格更快:

Range rbeg = (Range)sheet.Cells[1, i + 1];
Range rend = (Range)sheet.Cells[totalRowCount, i + 1];
Range range = sheet.get_Range(rbeg, rend);
column = (object[,])range.Value2;

The problem is when I want to get number format of cells by calling: 问题是当我想通过调用获取单元格的数字格式时:

range.NumberFormat

I get System.DBNull. 我得到System.DBNull。 It works when I call it for single cell. 当我为单个单元格调用它时,它可以工作。

I want to distinguish between cells with numerical values and "%" values. 我想区分具有数字值和“%”值的单元格。

Any ideas? 有任何想法吗?

See the documentation : 请参阅文档

[NumberFormat] returns Null if all cells in the specified range don't have the same number format. 如果指定范围内的所有单元格都不具有相同的数字格式,则[NumberFormat]返回Null。

What were you expecting it to do in this case? 在这种情况下,您期望它做什么?

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

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