Default.aspx.cs


// Excel’den Veri Okumak | www.aliakyildirim.com | Ali AKYILDIRIM
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
// Excel’den Veri Okumak |
www.aliakyildirim.com | Ali AKYILDIRIM


public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnYenile.Enabled = false;
ltrlAciklama.Text = "<font color=’White’><b>" + "App_Data" + " içerisinde bulunan " + "’Ogrenciler.xlsx’" + " dosyasindaki bilgiler okunacak..!" + "</b> </font>";
}
protected void btnOku_Click(object sender, EventArgs e)
{
gvOgrenciler.Visible = true;
ltrlAciklama.Text = "<font color=’White’><b>" + "
www.aliakyildirim.com" + "</b> </font>";
string dosya = "~/App_Data/Ogrenciler.xlsx";
string connString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;",
Server.MapPath(dosya));

OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sayfa1$]", conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataSet dt = new DataSet();
da.Fill(dt);

gvOgrenciler.DataSource = dt;
gvOgrenciler.DataBind();
btnOku.Enabled = false;
btnYenile.Enabled = true;
}
protected void btnYenile_Click(object sender, EventArgs e)
{
gvOgrenciler.Visible = false;
btnOku.Enabled = true;
btnYenile.Enabled = false;
}
protected void btnStatistics_Click(object sender, EventArgs e)
{
Response.Redirect("Istatistikler.aspx");
}
}
// Excel’den Veri Okumak |
www.aliakyildirim.com | Ali AKYILDIRIM



Istatistikler.aspx.cs


// Excel’den Veri Okumak | www.aliakyildirim.com | Ali AKYILDIRIM
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
// Excel’den Veri Okumak |
www.aliakyildirim.com | Ali AKYILDIRIM


public partial class Istatistikler : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string dosya = "~/App_Data/Ogrenciler.xlsx";
string connString = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=Excel 12.0;",
Server.MapPath(dosya));

OleDbConnection conn = new OleDbConnection(connString);
conn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sayfa1$]", conn);
OleDbDataAdapter da = new OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt = new DataTable();
da.Fill(dt);

var ogrenciler = from o in dt.AsEnumerable()
select new
{
SiraNo = o.Field<double>("Sira No"),
Numara = o.Field<double>("Numara"),
AdSoyad = o.Field<string>("Adi Soyadi"),
Vize = o.Field<double>("Vize"),
Final = o.Field<double>("Final"),
Bütünleme = o.Field<double>("Bütünleme"),
Ortalama = o.Field<double>("Ortalama")
};

// Sinif ortalamasi
var ort = ogrenciler.Average(x => x.Ortalama);
lblSoru1.Text = ort.ToString();

// Vize’den en yüksek alan ögrenci
var ogr = (from o in ogrenciler
where o.Vize == ogrenciler.Max(x => x.Vize)
select o).First();
lblSoru2.Text = ogr.AdSoyad;

// Ögrenci sayisi
var toplam = ogrenciler.Count();
lblSoru3.Text = toplam.ToString();

//Vize-Final ortalamasi en yüksek ilk 3 ögrenci:

var ogr2 = (from o in ogrenciler.AsEnumerable()
let ortalama = (o.Vize + o.Final) / 2
orderby ortalama descending
select o).Take(3);

gvSoru4.DataSource = ogr2;
gvSoru4.DataBind();

gvOgrenciler.DataSource = ogrenciler;
gvOgrenciler.DataBind();
}
}
protected void btnYazilimcik_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
}
// Excel’den Veri Okumak |
www.aliakyildirim.com | Ali AKYILDIRIM




Uygulamada kullanilan Excel’i Indir