Thursday, May 26, 2011

Read excel from data


Add namespace
using Excel = Microsoft.Office.Interop.Excel;

Add reference
Microsoft.Office.Interop.Excel

private void button1_Click(object sender, EventArgs e)
        {
            Excel.Application xlApp ;
            Excel.Workbook xlWorkBook ;
            Excel.Worksheet xlWorkSheet ;
            Excel.Range range ;
            double str1;
            double str;
           

            xlApp = new Excel.ApplicationClass();
            xlWorkBook = xlApp.Workbooks.Open("E:\\Sharmila\\Documents\\Data.xlsx", 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);

            range = xlWorkSheet.UsedRange;
            System.Array arrValues = (System.Array)range.Cells.Value2;

        

            for (int i = 1; i < arrValues.Length; i++)
            {
               
              
                str = Convert.ToDouble(arrValues.GetValue(i,1).ToString());
                str1 = Convert.ToDouble(arrValues.GetValue(i+1,1).ToString());
                DateTime MyDate = DateTime.FromOADate(str);
                DateTime MyDate1 = DateTime.FromOADate(str1);
                int diff = MyDate1.Second - MyDate.Second;
                if (diff > 1)
                {
                    MessageBox.Show("Error");
               
                }
            }
          
        

            xlWorkBook.Close(true, null, null);
            xlApp.Quit();

           
        }

No comments:

Post a Comment