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.Common;
using System.Data.OleDb;
namespace Get_Excel_Sheet_Names
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
///
/// For opening the dialog box to select the file
///
///
///
private void btnBrowse_Click(object sender, EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
DialogResult dlgResult = dlg.ShowDialog();
if (dlgResult == DialogResult.OK)
{
txtPath.Text = dlg.FileName;
}
comboBox1.DataSource = GetSheetNames(txtPath.Text);
}
///
/// Here we are reading the sheet and loading the sheet name in ddl
///
///
///
public static ListGetSheetNames(string path)
{
Listsheets = new List ();
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", path);
DbProviderFactory factory = DbProviderFactories.GetFactory("System.Data.OleDb");
DbConnection connection = factory.CreateConnection();
connection.ConnectionString = connectionString;
connection.Open();
DataTable tbl = connection.GetSchema("Tables");
connection.Close();
foreach (DataRow row in tbl.Rows)
{
string sheetName = (string)row["TABLE_NAME"];
if (sheetName.EndsWith("$"))
{
sheetName = sheetName.Substring(0, sheetName.Length - 1);
}
sheets.Add(sheetName);
}
return sheets;
}
///
/// For loading the selected sheet data
///
///
///
private void btnLoadData_Click_1(object sender, EventArgs e)
{
if (System.IO.File.Exists(txtPath.Text))
{
string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 8.0;HDR=YES;IMEX=1;""", txtPath.Text);
string query = String.Format("select * from [{0}$]", comboBox1.SelectedItem);
OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
DataSet dataSet = new DataSet();
dataAdapter.Fill(dataSet);
dataGridView1.DataSource = dataSet.Tables[0];
}
else
{
MessageBox.Show("No File is Selected");
}
}
}
}
20 Mayıs 2013 Pazartesi
C# Excel Sayfa (Sheet) içeriği görüntüleme
Excel dosya içerigi görüntüleme programı
Kaydol:
Kayıt Yorumları (Atom)
Hiç yorum yok:
Yorum Gönder