使用c#将Excel数据导入Datagridview [英] Importing Excel Data into Datagridview using c#

查看:822
本文介绍了使用c#将Excel数据导入Datagridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意它是windows应用程序。

使用c#



将Excel数据导入Datagridview,我的代码如下;

Note It is windows application.
Importing Excel Data into Datagridview using c#

for that my code as follows;

private void Btn_Import_Click(object sender, EventArgs e)
          {
  try
              {
  FileStream stream = File.Open(@"E:\Projects\Sheet1.xls", FileMode.Open, FileAccess.Read);
  IExcelDataReader excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
   DataSet result = excelReader.AsDataSet();
  excelReader.Close();
                  datagridView.DataSource = result.Tables[0];

              }
              catch (Exception ex)
              {
                  MessageBox.Show(ex.ToString(), "Error in Import");
                  return;
              }
              finally
              {
                  GFun.OleDbCon.Close();
              }
          } 





当我点击导入按钮时,excel数据正在检索但是Date没有检索正确。





excel记录如下







when i click the import button,the excel data is retrieving but Date is not retrieving in the correctly.


In excel records as follows


Date     Session     Day

15-APR     1         MOn
           2
           3
           4

16-APR     1         Tue
           2
           3
           4







在Datagridview中,excel记录正在检索但是日期检索不正确



在Datagridview记录中如下(从Excel导入)








IN Datagridview the excel records are retrieving But Date is not retrieving correct

In Datagridview records as follows(Importing From Excel)


Date     Session     Day

 41379      1         MOn
            2
            3
            4

 41380      1         Tue
            2
            3
            4





请帮帮我。



有什么问题。

问候,

Narasiman P.



Please help me.

what is the problem.
Regards,
Narasiman P.

推荐答案

这是因为Excel存储日期数据的方式。你可以在一个空的excel工作簿中证明这一点...在一个单元格中键入 41379 ,然后使用F o rmat,C e lls,日期和该号码将显示为 15/04/2013

您可以尝试将电子表格内容更改为文本格式(不推荐),请参阅 ExcelReaderFactory 的文档和/或代码,或使用替代方法从excel加载数据。此链接已被推荐用于 http:// codehill。 com / 2009/01 / reading-excel-2003-and-2007-files-using-oledb / [ ^ ]
This is because of the way Excel stores it's date data. You can demonstrate this in an empty excel workbook ... type 41379 into a cell, then use Format, Cells, Date and that number will be displayed as 15/04/2013.
You could try changing the spreadsheet contents to text format (not recommended), refer to the documentation and/or code for ExcelReaderFactory or use an alternative method to load the data from excel. This link has been recommended on several other posts http://codehill.com/2009/01/reading-excel-2003-and-2007-files-using-oledb/[^]


试试这个兄弟......为我工作。 ..





OleDbConnection Connection = new OleDbConnection(source);

Connection.ConnectionString = Provider = Microsoft.ACE.OLEDB.12.0;数据源= {0}; +扩展属性= \Excel 12.0; HDR = {1}; IMEX = 2 \,filePath,header? 是:否;



OleDbCommand command = new OleDbCommand(SELECT * Column1,Column2 FROM [Sheet1
Try this brother...Work for me...


OleDbConnection Connection = new OleDbConnection(source);
Connection.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};" + "Extended Properties=\"Excel 12.0;HDR={1};IMEX=2\"", filePath, header ? "Yes" : "No";

OleDbCommand command = new OleDbCommand("SELECT * Column1, Column2 FROM [Sheet1


,Connection);

DataSet dsTest = new Datasets();

OleDbDataAdapter adapter = new OleDbDataAdapter(command);

adapter.Fill(dsTest);

dataGridView1.DataSource = dsTest.Tables [0];
", Connection);
DataSet dsTest = new Datasets();
OleDbDataAdapter adapter = new OleDbDataAdapter(command);
adapter.Fill(dsTest);
dataGridView1.DataSource = dsTest.Tables[0];


这篇关于使用c#将Excel数据导入Datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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