Export Excel to SQL Databse Using Csharp

Posted by imomins on May 12, 2011 at 6:32 AM

 int row = 2;

            string Path = @"f:\ACC_LEDGER_TROYEE.xls";

            Excel.Application app = new Excel.Application();

            Excel.Workbook workBook = app.Workbooks.Open(Path, 0, true, 5, "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0);

            Excel.Worksheet workSheet = (Excel.Worksheet)workBook.ActiveSheet;

 object colIndex1 = 1;

            object colIndex2 = 2;

            object colIndex3 = 3;

            object colIndex4 = 4;

clsDatabaseConnectorForTroyee databaseConnectorForTroyee = new clsDatabaseConnectorForTroyee();

            databaseConnectorForTroyee.mpubvConnectDatabaseForTroyee();

            clsDatabaseConnectorForTroyee.gmpubstasqlconnSQLConnectionForTroyee.Open();

            SqlCommand thisCommand = clsDatabaseConnectorForTroyee.gmpubstasqlconnSQLConnectionForTroyee.CreateCommand();

            string lstrSQL = "SELECT * FROM ACC_LEDGER";

            thisCommand.CommandText = lstrSQL;

            SqlDataReader thisReader = thisCommand.ExecuteReader();

            while (thisReader.Read())

            {

                workSheet.Cells[row, colIndex1].Value = Convert.ToString(thisReader["LEDGER_SERIAL"]);

                workSheet.Cells[row, colIndex2].Value = Convert.ToString(thisReader["LEDGER_NAME"]);

                workSheet.Cells[row, colIndex3].Value = Convert.ToString(thisReader["LEDGER_PARTY_TYPE"]);

 

                row++;

            }

            thisReader.Close();

            clsDatabaseConnectorForTroyee.gmpubstasqlconnSQLConnectionForTroyee.Close();

            MessageBox.Show("Excel file created , you can find the file F:\\ACC_LEDGER_TROYEE.xls");

        }


 

 

Categories: Personal, C#, SQL

Post a Comment

Oops!

Oops, you forgot something.

Oops!

The words you entered did not match the given text. Please try again.

You must be a member to comment on this page. Sign In or Register

0 Comments