【问题标题】:0002 string value getting truncated to 2 while converting to DataTable using ExcelDataReader0002 字符串值在使用 ExcelDataReader 转换为 DataTable 时被截断为 2
【发布时间】:2018-12-12 13:35:10
【问题描述】:
private DataTable ExcelToDataTable(IFormFile file)
    {
        using (var stream = file.OpenReadStream())
        {
            using (var reader = ExcelReaderFactory.CreateReader(stream))
            {
                var result = reader.AsDataSet(new ExcelDataSetConfiguration()
                {
                    ConfigureDataTable = (data) => new ExcelDataTableConfiguration()
                    {
                        UseHeaderRow = true,
                    }
                });

                DataTableCollection table = result.Tables;
                DataTable resultTable = table["Employee Information"];

                return resultTable;
            }
        }
    }

大家好, excel中有一个名为uniquenumber的列,其值为0002,当数据转换为DataSet时,该列的值变为2。

谁能帮我修一下?

【问题讨论】:

  • 单元格格式是否为“文本”?
  • 如果字段/列的类型是数字,则不会截断。 0002 等于 2 和 00000002。如果您希望数字以特定方式显示,请在显示期间对其进行格式化,例如使用格式字符串或样式
  • @Hmax : 单元格的格式是通用的。
  • @PanagiotisKanavos:问题是我必须将值存储在数据库中。并且 db 中列的数据类型是 nvarchar 所以 '2' '0002' .
  • 您检查过AsDataSet configuration options 吗?你试过设置UseColumnDataType = true,吗?

标签: c# exceldatareader


【解决方案1】:

当单元格被格式化为“常规”并以零开头时,确保单元格被格式化为“文本”,该值被视为数字,所有前导零都被隐藏。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-07
    • 2018-04-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-30
    • 2013-03-26
    相关资源
    最近更新 更多