在Datagridview中读取Excel日期 [英] Reading Excel date in Datagridview

查看:145
本文介绍了在Datagridview中读取Excel日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试阅读包含日期的Excel文件,但在将其导入我的应用程序后,我能够读取文件而不是日期。我的datagridview上的日期格式似乎是一种int格式而不是日期格式。我是
目前正在使用ExcelDataReader扩展v2.1.2.3,它正在阅读所有内容,但是日期。日期列的Excel表格格式为日期格式

I have been trying to read my Excel file which contains date but after importing it to my application I am able to read the file but not the date. The format for the date on my datagridview which seems to be an int format instead of the date format. I am currently using ExcelDataReader extension v2.1.2.3, It's reading everything fine but the date. My Excel sheet format for the date column is in date format

而不是显示"2017-01-02"等日期,而是显示'42737'

Instead of showing dates like "2017-01-02", it's shows '42737'

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Data.OleDb;
using System.IO;
using Excel;

namespace Dashboard
{
    public partial class Import_Excel : Form
    {
        public Import_Excel()
        {
            InitializeComponent();
        }

        DataSet roster;

        private void btnLoad_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog() { Filter = "Excel Workbook|*.xls", ValidateNames = true })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    FileStream fs = File.Open(ofd.FileName, FileMode.Open, FileAccess.Read);
                    IExcelDataReader reader = ExcelReaderFactory.CreateBinaryReader(fs);
                    reader.IsFirstRowAsColumnNames = true;
                    roster = reader.AsDataSet();
                    cboSheet.Items.Clear();
                    foreach (DataTable dt in roster.Tables)
                    {
                        cboSheet.Items.Add(dt.TableName);

                    }


                    reader.Close();
                }
            }
        }

        private void cboSheet_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataGridView1.DataSource = roster.Tables[cboSheet.SelectedIndex];
        }
    }
}







推荐答案

您好,

我之前从未听说过这个库,所以以下内容是基于查看返回的内容,您将需要使用一种方法来获取日期,例如在他们的

单元测试
中有GetDateTime的方法。 

I've never heard of this library before so the following is based on seeing what's being returned you will need to use a method to get a date e.g. in their unit test there is a method to GetDateTime. 

所以你需要决定如何最好地将GetDateTime用于你的逻辑。

So you will need to decide how best to utilize GetDateTime into your logic.

最后,任何值得的库任何事情都不会自动确定日期等类型并为您处理。

In closing, any library worth anything will not automatically determine a type such as a date and handle this for you.


这篇关于在Datagridview中读取Excel日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆