从excel导入后,如何将datagridview单元格转换为命运格式 [英] How do I convert datagridview cell to fate format after importing from excel

查看:90
本文介绍了从excel导入后,如何将datagridview单元格转换为命运格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Datagidview中将日期列转换为日期格式。我在ExcelDataReader V2.1.2.3上使用nuget包扩展。我的Excel工作表是日期格式,但是当我尝试将工作表加载到DataGridView时,我没有将实际日期作为输出。



[ excel表格的图片]

[导入后数据网格视图中输出的图片]



我尝试过:



How do I convert my date column to date format in Datagidview. I am using a nuget package extension on ExcelDataReader V2.1.2.3. My excel sheet is in date format but when i try to load the sheet into the DataGridView I am not getting the actual date as output.

[Picture of excel sheet]
[Picture of output in datagridview after import]

What I have tried:

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];
        }
    }
}

推荐答案

按照以下说明操作:日期格式表示不正确·问题#177·ExcelDataReader / ExcelDataReader·GitHub [ ^ ]
Follow this instruction: incorrect date format representation · Issue #177 · ExcelDataReader/ExcelDataReader · GitHub[^]


这篇关于从excel导入后,如何将datagridview单元格转换为命运格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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