我在使用 C# 读取 excel 文件时遇到一个问题。在我的 Excel 文件中,有一列是日期时间,它在 excel 文件中的值很长。因此,当我使用 C# 在 C# 中获取相同的值读取相同的 Excel 时,Excel 会将其转换为 ###### 格式。谁能帮忙:我怎样才能得到日期时间值而不是#####?
我的代码如下:
using Excel = Microsoft.Office.Interop.Excel;
Excel.Application xlApp;
Excel.Workbook xlWorkBook;
Excel.Worksheet xlWorkSheet;
object misValue = System.Reflection.Missing.Value;
ExcelObj = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(oldg.FileName, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
for (int i = 3; i <= worksheet.UsedRange.Rows.Count; i++)
{
var getvalue = excel_getValue("B" + i);
}
最佳答案
如果您有一个日期列并且值超出范围,您的日期转换可能会失败。如果您在日期列中输入“5555555555555555555555”(比方说),您会得到一系列“#####”和工具提示:
Dates and times that are negative or too large display as ######.
最常见的原因是您有一个美国格式的日期 (MM/dd/yyyy) 并且您正试图将其转换为欧洲格式的日期 (dd/MM/yyyy) 或者您不是完全转换字符串。
有关 Excel 为什么显示“######”而不是实际列值的更多信息,请参阅 this Super User question
关于c# - 在 C# 中读取 excel 文件时获取 ###### 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17648793/