Thursday, May 26, 2011

Read.csv from data


        if (!string.IsNullOrEmpty(fpPriceData.FileName))
            {
                if (fpPriceData.FileName.Contains(".csv"))
                {                  
                                 

                   
                    string strpath = System.IO.Path.GetTempPath() + "\\" + Guid.NewGuid().ToString("N") + ".csv";
                    fpPriceData.PostedFile.SaveAs(strpath);

                 
                 

                    #region code for using .csv files
                    //uncomment this code if we want to use .csv files

                    StreamReader str = new StreamReader(strpath);
                    DataTable dtEntireTable = oCOMClass.GetCompleteDataForCSV(str);

                    #endregion
                  }
              }



  public DataTable GetCompleteDataForCSV(StreamReader srdr)
    {
        //  System.IO.StreamReader srdr = new System.IO.StreamReader(strm);
        DataTable m_dtCSV = new DataTable();
        String strLine = String.Empty;
        Int32 iLineCount = 0;
        do
        {
            strLine = srdr.ReadLine();
            if (strLine == null)
            {
                break;
            }
            if (0 == iLineCount++)
            {
                m_dtCSV = this.CreateDataTableForCSVData(strLine);
            }
            else
            {
                this.AddDataRowToTable(strLine, m_dtCSV);
            }
        } while (true);

        return m_dtCSV;
    }
    private DataTable CreateDataTableForCSVData(String strLine)
    {
        DataTable dt = new DataTable("CSVTable");
        String[] strVals = strLine.Split(new char[] { ',' });
        m_iColumnCount = strVals.Length;
        int idx = 0;
        foreach (String strVal in strVals)
        {
            String strColumnName = String.Format(strVal);
            // String strColumnName = String.Format("Column-{0}", idx++);

            dt.Columns.Add(strColumnName, Type.GetType("System.String"));
        }
        return dt;
    }
    private DataRow AddDataRowToTable(String strCSVLine, DataTable dt)
    {
        String[] strVals = strCSVLine.Split(new char[] { ',' });
        Int32 iTotalNumberOfValues = strVals.Length;
        // If number of values in this line are more than the columns
        // currently in table, then we need to add more columns to table.
        //if (iTotalNumberOfValues > m_iColumnCount)
        //{
        //    Int32 iDiff = iTotalNumberOfValues - m_iColumnCount;
        //    for (Int32 i = 0; i < iDiff; i++)
        //    {
        //        String strColumnName = String.Format("Column-{0}", (m_iColumnCount + i));
        //        dt.Columns.Add(strColumnName, Type.GetType("System.String"));
        //    }
        //    m_iColumnCount = iTotalNumberOfValues;
        //}
        int idx = 1;
        DataRow drow = dt.NewRow();
        foreach (String strVal in strVals)
        {
            //  String strColumnName = String.Format("Column-{0}", idx++);

            drow[idx - 1] = strVal.Trim();
            idx++;

        }
        dt.Rows.Add(drow);
        return drow;
    }  

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();

           
        }