user1056466
user1056466

Reputation: 667

Reading Excel Files using c#

My code for reading excel file is working properly for many of the rows but it doesnt display some contents in the file for e.g(#######) and dates for e.g (8/11/2009) but do display dates which have a 2 digit day in it for e.g(11/12/2011).

DataTable dtExcel = new DataTable();

string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HafizwalaFile + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";
string strSQL = "SELECT * FROM [qe$] ";
OleDbConnection excelConnection = new OleDbConnection(connectionString);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(strSQL, excelConnection);

dataAdapter.Fill(dtExcel);
   for (int i = 0; i < dtExcel.Rows.Count; i++)
   {
      for (int j = 0; j < dtExcel.Columns.Count;j++ )
      {
         System.Console.WriteLine(dtExcel.Rows[i][j]);
         count_records++;
      }
   }

Upvotes: 0

Views: 2806

Answers (3)

houssam
houssam

Reputation: 1873

this occurred because you format a column as Date , and you entered data that is not a valid date. you can check this from C# using :
MessageBox.Show(dtExcel.Columns[0].DataType.ToString());
Excel stores dates and times as a number representing the number of days since 1900-Jan-0, plus a fractional portion of a 24 hour day: ddddd.tttttt Read this
I tried Many times and I find that the following code works fine , (Test it please) :

String cellValue = "41577.333333"; // 30/10/2013  8:00:00 AM
            int days  = 0 ;
            int hours = 0;
            string[] arr =  cellValue.Split('.');
            days = int.Parse(arr[0]);
            if(arr.Length == 2)
            {
                decimal  d_hours = decimal.Parse("0." +  arr[1]);
                hours =(int) ( d_hours  * 24  );
            }

            TimeSpan dateFromExcel = new TimeSpan(days ,hours ,0,0,0);
            DateTime resultingDate = new DateTime(1900,1,1 ,0,0,0).Add(dateFromExcel).AddDays(-2);
            MessageBox.Show(resultingDate.ToString());

try to use ACE instead of JET , some web sites mentioned this:

string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;;Data Source=" + HafizwalaFile + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1'";

Upvotes: 0

Before the transfer of data to the table, you have to enter the fileexcel and adjust the worksheet width of this columns are as follows

 //using Microsoft.Office.Interop.Excel;
        Microsoft.Office.Interop.Excel.Application excelapp = new Microsoft.Office.Interop.Excel.Application();
       // excelapp.Visible = true;
        string filename_Path = @"D:\exmp1.xls";
        _Workbook workbook = (_Workbook)(excelapp.Workbooks.Open(filename_Path, 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));



    excelapp .Cells .Columns .AutoFit ();
    workbook.Save();
   // workbook.Close();
    excelapp.Quit(); 

Upvotes: 1

MichaelEvanchik
MichaelEvanchik

Reputation: 1766

I would suggest formatting that column of the excel into date format that forces it to pad 0's or keep it straight text. I don't think there is anything you can do on the c# side of things

Upvotes: 0

Related Questions