Friday, December 11, 2009

Exporting data to excel sheet

Hai,

Make sure that your excel sheet in'.xls' format........

using System.Data.OleDb;

connectionString = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=D:/Book1.xls; Extended Properties=Excel 8.0;";

using(OleDbConnection Connection = new OleDbConnection(connectionString))
{
Connection.Open();
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
try
{
command.CommandText = "drop table Sample1";
command.ExecuteNonQuery();
command.CommandText = "CREATE TABLE Sample1(FirstName Char(255), LastName char(255), Email char(255))";
command.ExecuteNonQuery();
}
catch(Exception ex)
{
command.CommandText = "CREATE TABLE Sample1(FirstName Char(255), LastName char(255), Email char(255))";
command.ExecuteNonQuery();
}
}
using(OleDbCommand command = new OleDbCommand())
{
command.Connection = Connection;
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('Anuraj','P','anuraj.p@example.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('sreekumar','VN','sreekumar.vn@example.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('jinesh','B','jinesh.b@example.com')";
command.ExecuteNonQuery();
command.CommandText = "INSERT INTO Sample1(FirstName,LastName,Email) VALUES('ratheesh','S','ratheesh.s@example.com')";
command.ExecuteNonQuery();
}
DataSet ds = new DataSet();
OleDbDataAdapter da = new OleDbDataAdapter("select * from Sample1", Connection);
da.Fill(ds);


DataGrid dgGrid = new DataGrid(); object missing = Type.Missing;

Microsoft.Office.Interop.Excel.Application oExcel=new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbooks oBooks = (Microsoft.Office.Interop.Excel.Workbooks)oExcel.Workbooks;//;
Microsoft.Office.Interop.Excel._Workbook oBook = (Microsoft.Office.Interop.Excel._Workbook)(oBooks.Add(missing));

Microsoft.Office.Interop.Excel.Sheets oSheets = (Microsoft.Office.Interop.Excel.Sheets)oBook.Worksheets;//= new Microsoft.Office.Interop.Excel._Worksheet();
Microsoft.Office.Interop.Excel._Worksheet oSheet = (Microsoft.Office.Interop.Excel._Worksheet)(oSheets.get_Item(1));

Microsoft.Office.Interop.Excel.Range oRange = oSheet.get_Range("A1", missing);
oRange.Value2 = "First Name";

oRange = oSheet.get_Range("B1", missing);
oRange.Value2 = "Last Name";

oRange = oSheet.get_Range("C1", missing);
oRange.Value2 = "Email";

oRange = oSheet.get_Range("A1", "C1");
Microsoft.Office.Interop.Excel.Font oFont = oRange.Font;
oFont.Bold = true; int j=1;
for (int i = 0; i < ds.Tables[0].Rows.Count;i++)
{
j++;
oRange = oSheet.get_Range("A"+j, missing);
oRange.Value2 = ds.Tables[0].Rows[i][0];

oRange = oSheet.get_Range("B"+j, missing);
oRange.Value2 = ds.Tables[0].Rows[i][1];

oRange = oSheet.get_Range("C" +j, missing);
oRange.Value2 = ds.Tables[0].Rows[i][2];

}

oBook.SaveAs("D:/Book11.xls", missing, missing, missing, missing, missing, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive, missing, missing, missing, missing, missing);

oBook.Close(false,missing,missing);
oExcel.Quit();
}

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home