Wednesday, December 3, 2008

import from excel in c#

private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
{
path = openFileDialog1.FileName;
}

private void btnImport_Click(object sender, EventArgs e)
{
openFileDialog1.Filter = "excel file |*.xls";
openFileDialog1.FilterIndex = 1;
openFileDialog1.InitialDirectory = "%My Documents%";
openFileDialog1.ShowDialog(this);
// path = openFileDialog1.FileName;
if (path != "")
{
try
{
oledbConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + @"; Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""";
olebdConn.ConnectionString = oledbConnectionString;
oledbCmmd.Connection = olebdConn;
oledbCmmd.CommandText = "Select * FROM [Sheet1$]";
olebdConn.Open();
da =new OleDbDataAdapter(oledbCmmd);
da.Fill(ds);

dgvExcelData.Visible = true;
////dt = dr.GetData(4);
int count = ds.Tables[0].Rows.Count;
label2.Text = count.ToString();
////dgvExcelData.Rows.Clear();

dgvExcelData.DataSource = ds.Tables[0];
////dgvExcelData.Visible = true;
olebdConn.Close();
}

Export into excel in c# through COM

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, click New, and then click Project. Select Windows Application from the Visual C# Project types. Form1 is created by default.
  3. Add a reference to the Microsoft Excel Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, locate Microsoft Excel Object Library, and click Select.
-----------------------------------------------------------------------------------------------
using System.Reflection;
Excel._Application oxl;
Excel._Workbook owb;
Excel._Worksheet oWs;
Excel.Range oRange;
methord( )
{
oxl = new Excel.Application();
oxl.Visible = true;
//Get a new workbook.
owb = (Excel._Workbook)(oxl.Workbooks.Add(Missing.Value));
oWs = (Excel._Worksheet)owb.ActiveSheet;
//Add table headers going cell by cell
oWs.Cells[1, 1] = "First Name";
oWs.Cells[1, 2] = "Last Name";
oWs.Cells[1, 3] = "Full Name";
oWs.Cells[1, 4] = "Salary";

//Format A1:D1 as bold, vertical alignment = center.
oWs.get_Range("A1", "D1").Font.Bold = true;
oWs.get_Range("A1", "D1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;
// Create an array to multiple values at once.
string[,] saNames = new string[5, 2];
saNames[0, 0] = "John";
saNames[0, 1] = "Smith";
saNames[1, 0] = "Tom";
saNames[1, 1] = "Brown";
saNames[2, 0] = "Sue";
saNames[2, 1] = "Thomas";
saNames[3, 0] = "Jane";
saNames[3, 1] = "Jones";
saNames[ 4, 0] = "Adam";
saNames[ 4, 1] = "Johnson";
//Fill A2:B6 with an array of values (First and Last Names).
oWs.get_Range("A2", "B6").Value2 = saNames;
//Fill C2:C6 with a relative formula (=A2 & " " & B2).
oRange = oxl.get_Range("C2", "C6");
oRange.Formula = "=A2 &\" \"& B2";
//Fill D2:D6 with a formula(=RAND()*100000) and apply format.
oRange = oxl.get_Range("D2", "D6");
oRange.Formula = "=RAND()*10000";
oRange.NumberFormat = "$0.00";
//AutoFit columns A:D.
oRange = oWs.get_Range("A1", "D1");
oRange.EntireColumn.AutoFit();
//Manipulate a variable number of columns for Quarterly Sales Data.
//DisplayQuarterlySales(oWs);

//Make sure Excel is visible and give the user control
//of Microsoft Excel's lifetime.
oxl.Visible = true;
oxl.UserControl = true;