using Microsoft.Office.Interop.Excel;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace dt_to_excel
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string path = @"C:\Users\deepika.W109\Documents\Visual Studio 2012\Projects\Websitechangesdemo1\dt to excel\Book2.xls";
System.Data.DataTable dt = new System.Data.DataTable();
dt = exceldata(path);
dataGridView1.DataSource = dt;
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("No", typeof(int));
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Date", typeof(string));
// Here we add five DataRows.
table.Rows.Add(1, "David", "1987,10,20");
table.Rows.Add(2, "Sam", "1990,2,21");
table.Rows.Add(3, "Christoff", "1991,2,25");
table.Rows.Add(4, "Janet", "1988,10,15");
table.Rows.Add(5, "Melanie", "1987,10,14");
for (int i = 1; i <= 9; i++)
{
int Dosage = i;
string date = (from DataRow dr in table.Rows
where (int)dr["Sheet 1 content"] == Dosage
select (string)dr["F3"]).FirstOrDefault();
string Name = (from DataRow dr in table.Rows
where (int)dr["Sheet 1 content"] == Dosage
select (string)dr["F2"]).FirstOrDefault();
DateTime Btime = Convert.ToDateTime(date);
DateTime Todaydate = DateTime.Today;
if (Todaydate.Month == Btime.Month && Todaydate.Day == Btime.Day)
MessageBox.Show("" + Name + " is celebrating B'day today");
}
}
public static System.Data.DataTable exceldata(string filePath)
{
System.Data.DataTable dtexcel = new System.Data.DataTable();
bool hasHeaders = false;
string HDR = hasHeaders ? "Yes" : "No";
string strConn;
if (filePath.Substring(filePath.LastIndexOf('.')).ToLower() == ".xlsx")
strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
else
strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";
OleDbConnection conn = new OleDbConnection(strConn);
conn.Open();
System.Data.DataTable schemaTable = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, new object[] { null, null, null, "TABLE" });
//Looping Total Sheet of Xl File
/*foreach (DataRow schemaRow in schemaTable.Rows)
{
}*/
//Looping a first Sheet of Xl File
DataRow schemaRow = schemaTable.Rows[0];
string sheet = schemaRow["TABLE_NAME"].ToString();
if (!sheet.EndsWith("_"))
{
string query = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter daexcel = new OleDbDataAdapter(query, conn);
dtexcel.Locale = CultureInfo.CurrentCulture;
daexcel.Fill(dtexcel);
}
conn.Close();
return dtexcel;
}
private void Form3_Load(object sender, EventArgs e)
{
}
}
}
No comments:
Post a Comment