Export Excel to SQL Databse Using Csharp
|
|
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");
}
Post a Comment
Oops!
The words you entered did not match the given text. Please try again.
Oops!
Oops, you forgot something.