how to exclude certain rows while loading excelsheet into dataset

Last post 07-03-2009 6:55 AM by sureshmdb. 4 replies.

Sort Posts:

  • how to exclude certain rows while loading excelsheet into dataset

    07-02-2009, 8:46 AM

     Hi ,

     

    I need to load an excel sheet data into dataset.

    i am able to load complete excel file into the dataset, what i require is to exclude some rows loading into the dataset.

    but the row indexs varies from one excel to other.is it posiible ?

    thanks in advance

  • Re: how to exclude certain rows while loading excelsheet into dataset

    07-02-2009, 9:00 AM
    • Member
      741 point Member
    • G0ggy
    • Member since 05-22-2007, 1:58 PM
    • Posts 207

    Hide the unwanted columns, either programmatically or manually, prior to importing.



    Remember to mark as answer the solution which helped you.
  • Re: how to exclude certain rows while loading excelsheet into dataset

    07-02-2009, 9:01 AM
    • Member
      315 point Member
    • sureshmdb
    • Member since 05-27-2008, 8:18 AM
    • Baleru
    • Posts 63

    string ExcelPath = " D:\\ExcelFiles";
                            if (Directory.Exists(ExcelPath))
                            {
                                DirectoryInfo GetExeclFiles = new DirectoryInfo(ExcelPath);
                                FileInfo[] Files = GetExeclFiles.GetFiles("*.xls");
                                if (Files.Length > 0)
                                {
                                    DataTable dtnewtable = new DataTable();
                                    DataTable dttable = new DataTable();

                                    string FilePath, excelConnectionString, sqlConnectionString;
                                    for (int k = 0; k < Files.Length; k++)
                                    {
                                        FilePath = Files[k].FullName.ToString();
                                        excelConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FilePath + ";Extended Properties=Excel 8.0;";
                                        // Create Connection to Excel Workbook
                                        using (OleDbConnection connection = new OleDbConnection(excelConnectionString))
                                        {
                                            if (connection.State == ConnectionState.Closed)
                                                connection.Open();
                                            OleDbCommand command = new OleDbCommand("Select * FROM [Sheet1$]", connection);
                                            // Create DbDataReader to Data Worksheet
                                            using (System.Data.Common.DbDataReader dr = command.ExecuteReader())
                                            {
                                                dttable.Load(dr);

                              //you can delete rows form datatable dttable here
                                                 
                                                // SQL Server Connection String
                                                sqlConnectionString = ConfigurationSettings.AppSettings["conString"].ToString();

                                                // Bulk Copy to SQL Server
                                                using (SqlBulkCopy bulkCopy = new SqlBulkCopy(sqlConnectionString))
                                                {
                                                    bulkCopy.DestinationTableName = "tbl_DataEntry";
                                                    bulkCopy.WriteToServer(dtnewtable);
                                                }
                                             
                                                connection.Close();
                                                connection.Dispose();
                                                File.Delete(FilePath);
                                            }//end of USING dataReader
                                        }//end of USING sql connection

                                    }//end of for loop
                                  
                                    MessageBox.Show("Successfully Uploaded..");
                                }//end of if(Files.Length > 0)

    Suresh Mediboyina



    Mark as answer if my response is useful to you.
  • Re: how to exclude certain rows while loading excelsheet into dataset

    07-02-2009, 11:57 PM

    Hi, i need to exclude rows of excel before loading data into data reader object

  • Re: how to exclude certain rows while loading excelsheet into dataset

    07-03-2009, 6:55 AM
    • Member
      315 point Member
    • sureshmdb
    • Member since 05-27-2008, 8:18 AM
    • Baleru
    • Posts 63

     Hi rprashanthkumar,

                  The code which I wrote in previous post,before loading the into data reader,we can manipulate the datatable and insert into databaseFrown

    Suresh Mediboyina



    Mark as answer if my response is useful to you.
Page 1 of 1 (5 items)