Search This Blog

Wednesday, July 17, 2013


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;
using System.IO;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;


namespace Read_Excel_Windows_Forms
{
    public partial class Form1 : Form
    {
        OleDbConnection con;
        DataTable dt;
        public Form1()
        {
            InitializeComponent();
            InitializeOpenFileDialog();
        }

        private void InitializeOpenFileDialog()
        {
            this.openFileDialog1.Title = "Select an excel file";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            GetExcelFields();
        }

        public void GetExcelFields()
        {
            DialogResult dr = this.openFileDialog1.ShowDialog();
            if (dr == System.Windows.Forms.DialogResult.OK)
            {
                string connectionString = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", openFileDialog1.FileName);
                string query = String.Format("select * from [{0}$]", "Sheet1");
                OleDbConnection con = new OleDbConnection(connectionString);
                con.Open();
                OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, connectionString);
                DataTable dt = new DataTable();
                dataAdapter.Fill(dt);
                int cnt = dt.Columns.Count;
                //int i = 1;
                List<ExcelItemPairs> data = new List<ExcelItemPairs>();
                foreach (DataColumn col in dt.Columns)
                {
                    comboBox1.DisplayMember = "Value";
                    comboBox1.Items.Add(new ExcelItemPairs() { Value= Convert.ToString(col.ColumnName), Type=Convert.ToString(col.DataType)});
                }
             
                DataSet dataSet = new DataSet();
                dataAdapter.Fill(dataSet);
                dataGridView1.DataSource = dataSet.Tables[0];
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ExcelItemPairs objType = (ExcelItemPairs)comboBox1.SelectedItem;
            label1.Text = objType.Type;
        }
    }
}

SPSiteDataQuery in SharePoint 2010


SPSiteDataQuery can be used to perform cross-site and cross-list queries. In order to use it, you must call SPWeb.GetSiteData method on the appropriate web and provide a SPSiteDataquery object.
Using SPSiteDataQuery.Webs property you can specify where the data will be retrieved from. This property can have one of the following values:
  • "" (or string.Empty) – default value : will search only the SPWeb on which you run GetSiteData
  • "<Webs Scope='SiteCollection' />" : will search on all webs in the site collection
  • "<Webs Scope='Recursive' />" : will search the SPWeb on which you run GetSiteData and all subwebs
You can restrict the search using Lists and Query properties. In the following example we query all contacts lists in the site collection for items containing John in the Last Name field:
 SPSiteDataQuery query = new SPSiteDataQuery();
            query.Lists = "<Lists ServerTemplate='105' />";
            query.ViewFields = "<FieldRef Name='Title' />";

            query.Query = "<Where><Contains>" +
                          "<FieldRef Name='Title' /><Value Type='text'>John</Value>" +
                          "</Contains></Where>";
               query.Webs = "<Webs Scope='SiteCollection' />";
            DataTable dt = SPContext.Current.Web.GetSiteData(query);
           gdView.DataSource = dt;
           gdView.DataBind();