How to display Excel document in c# window application

Posted by Bageshkumarbagi under C# on 5/21/2013 | Points: 10 | Views : 5999 | Status : [Member] | Replies : 5
Hi all,

I am creating one application. i want to display Excel document in c# window appliction.Can you please help me.

Thanks,
Bagesh Kumar Singh
Mark as answer if you have been satisfied



Responses

Posted by: Satyapriyanayak on: 5/21/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Retrieving_excel_data_to_combobox
{
public partial class Form1 : Form
{
public OleDbConnection con;
public void pintu(string s)
{
con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 8.0;");

}
public OleDbCommand com;
public DataSet ds;
public OleDbDataAdapter oledbda;
public DataTable dt;
public string str;

public Form1()
{
InitializeComponent();
}

private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.ShowDialog();
openfiledialog1.Filter = "allfiles|*.xls";
TextBox1.Text = openfiledialog1.FileName;

}



private void btndisplay_Click(object sender, EventArgs e)
{
pintu(TextBox1.Text);
try
{
con.Open();
str = "select * from [sheet1$]";
com = new OleDbCommand(str, con);
ds = new DataSet();
oledbda = new OleDbDataAdapter(com);
oledbda.Fill(ds, "[sheet1$]");
con.Close();
DataGridView1.DataSource = ds;
DataGridView1.DataMember = "[sheet1$]";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}




}
}
}


If this post helps you mark it as answer
Thanks

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bageshkumarbagi on: 5/21/2013 [Member] Starter | Points: 25

Up
0
Down
Hi Sathya,

I try your code but i got "External table is not in the expected format." error when i execute the code.

Thanks,
Bagesh Kumar Singh
Mark as answer if you have been satisfied

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 5/21/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
Change

//con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 8.0;");

to

con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 12.0;");

If this post helps you mark it as answer
Thanks

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 5/22/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace
{
public partial class Form1 : Form
{
public OleDbConnection con;
public void pintu(string s)
{
//con = new OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 8.0;");
con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; " + "data source='" + s + " '; " + "Extended Properties=Excel 12.0;");

}
public OleDbCommand com;
public DataSet ds;
public OleDbDataAdapter oledbda;
public DataTable dt;
public string str;

public Form1()
{
InitializeComponent();
}

private void btnbrowse_Click(object sender, EventArgs e)
{
OpenFileDialog openfiledialog1 = new OpenFileDialog();
openfiledialog1.ShowDialog();
openfiledialog1.Filter = "allfiles|*.xls";
TextBox1.Text = openfiledialog1.FileName;

}


private void btndisplay_Click(object sender, EventArgs e)
{
pintu(TextBox1.Text);
try
{
con.Open();
str = "select * from [sheet1$]";
com = new OleDbCommand(str, con);
ds = new DataSet();
oledbda = new OleDbDataAdapter(com);
oledbda.Fill(ds, "[sheet1$]");
con.Close();
DataGridView1.DataSource = ds;
DataGridView1.DataMember = "[sheet1$]";
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

}
}
}




If this post helps you mark it as answer
Thanks

If this post helps you mark it as answer
Thanks

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Rebecca Yang on: 9/27/2013 [Member] Starter | Points: 25

Up
0
Down
You can choose using Spire.XLS for .NET,You can download here :http://spreadsheet.codeplex.com/ ,hope it helps

Bageshkumarbagi, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response