Excelden veri alma

153 views
Skip to first unread message

osman tuzcu

unread,
Dec 22, 2010, 4:58:24 AM12/22/10
to altdotne...@googlegroups.com
Merhaba aşağıdaki kodla excelden gridview'e veri çekmek istiyorum
ancak sayfa boş geliyor. Sql dataadaptor componenti ile bağladığım
zaman veriler çıkıyor ama benim kodla yapmam lazım yardımcı
olurmusunuz
İyi çalışmalar
_______________________________________________________________________________________
string con = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=C:\\kantin2.xls;" +
"Extended Properties=Excel 8.0;";
OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM
[Sayfa2$A1:C37]", con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();

_______________________________________________________________________________________

Eralp Erat

unread,
Dec 22, 2010, 5:13:50 AM12/22/10
to altdotne...@googlegroups.com


Selamlar ;

Namespace bloğu aşağıdaki gibi olmalı.(Fazlalıkları sallayın.)

using Application = Microsoft.Office.Interop.Excel.Application;

 

using EXCEL = Microsoft.Office.Interop.Excel;

using System.Reflection;

using System.Net;

using System.Data.OleDb;

using System.Data.SqlClient;

using System.Web.Services.Protocols;


Kod kısmında da..


                EXCEL.Application excApp = new Application();

                EXCEL.Workbook excWorkBook;

                excWorkBook = excApp.Workbooks._Open(txbDosyaArsiv.Text,

                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value,

                    Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value);

                int rowOrder = startVal;

                #endregion standart

 

                Microsoft.Office.Interop.Excel.Worksheet excWorkSheet = (Microsoft.Office.Interop.Excel.Worksheet)excApp.Sheets.get_Item(1);

                bool isMasterCreate = false;

                new_importlogger xLoggerMain = null;

                Guid xLoggerMainGuid = new Guid();

                int aktarilan = 0;

                while (rowOrder <= endVal) {

                    #region start_iteration

                    if (!isMasterCreate) {

                        xLoggerMain = new new_importlogger();

                        xLoggerMain.new_aktarilmakistenenkayitsayisi = Convert.ToString(endVal - startVal);

                        xLoggerMain.new_description = "Excel'den Arşive Çekme - Zaman : " + DateTime.Now.ToString();

                        xLoggerMain.new_importedilendosya = txtDosya.Text.Trim();

                        xLoggerMainGuid = service.Create(xLoggerMain);

                        isMasterCreate = true;

                    }

                    #endregion start_iteration

 

                    #region Exceldeki Gerekli Alanlar

                    string TamAd = ((Microsoft.Office.Interop.Excel.Range)excWorkSheet.Cells[rowOrder, "a"]).Text.ToString();

                    string AbonelikDurum = ((Microsoft.Office.Interop.Excel.Range)excWorkSheet.Cells[rowOrder, "b"]).Text.ToString();

                    string AbonelikBitisTarihi = ((Microsoft.Office.Interop.Excel.Range)excWorkSheet.Cells[rowOrder, "c"]).Text.ToString();

                    #region loop_iteration

                    rowOrder++;

                    this.Text = rowOrder.ToString() + " . kayıt işleniyor!";

                    this.Refresh();

                    #endregion loop_iteration

                }

                #region end_Of_File

                if (isMasterCreate) {

                    xLoggerMain = (new_importlogger)service.Retrieve(EntityName.new_importlogger.ToString(), xLoggerMainGuid, new AllColumns());

                    xLoggerMain.new_aktarilankayitsayisi = Convert.ToString(aktarilan);

                    xLoggerMain.new_description += ",Bitiş Zaman : " + DateTime.Now.ToString();

                    service.Update(xLoggerMain);

                }

 

                excWorkBook.Close(false, Missing.Value, Missing.Value);

                excWorkSheet = null;

                excWorkBook = null;

 

                #endregion end_Of_File


Referans için de


COm kısmından

Microsoft.Office.Interop.Excel 'u bulup yükleyin , 12 veya 14.


ok ?


E2




2010/12/22 osman tuzcu <osman...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "altdotnetturkiye" group.
To post to this group, send email to altdotne...@googlegroups.com.
To unsubscribe from this group, send email to altdotnetturki...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/altdotnetturkiye?hl=en.


Enes Keskin

unread,
Dec 22, 2010, 5:35:12 AM12/22/10
to altdotnetturkiye
Selamlar,

Eralp'ın gönderdiği koddaki yöntem, ilgili serverde office'in yüklü
olmasını gerektirir.
Ayrıca EXCEL.Application excApp'ın bir hata durumunda dispose
edilmemesi bir zaman sonra serverda bir yığın excel processesinin
birikmesine neden olabilir.

A1:C37 arasını almak istemişsiniz.
Bu konuda yardımcı olur mu bilemem.
Generic tüm worksheetleri data set'e alan metod aşağıdadır.

-> HDR ve IMEX ifadeleri de sizde problem oluyor olabilir
HDR -> Has headers (ilgili alanda kolon başlığı var mı, varsa onları
caption olarak alıyor, row'lara eklemiyor)
IMEX -> Import-Export Mode ( =0 olursa -> Treat everything as string
her cell'i string olarak alıyor)

public DataSet ReadIntoDataSet(string fileName, bool
hasHeaders)
{
string HDR = hasHeaders ? "Yes" : "No";
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=" +
fileName + ";Extended Properties=\"Excel
8.0;HDR=" +
HDR + ";IMEX=1\"";

DataSet output = new DataSet();

using (OleDbConnection conn = new
OleDbConnection(strConn))
{
conn.Open();

DataTable schemaTable = conn.GetOleDbSchemaTable(
OleDbSchemaGuid.Tables, new object[] { null, null,
null, "TABLE" });

foreach (DataRow schemaRow in schemaTable.Rows)
{
string sheet = schemaRow["TABLE_NAME"].ToString();

OleDbCommand cmd = new OleDbCommand("SELECT * FROM
[" + sheet + "]", conn);
cmd.CommandType = CommandType.Text;

DataTable outputTable = new DataTable(sheet);
output.Tables.Add(outputTable);
new OleDbDataAdapter(cmd).Fill(outputTable);
}
}
return output;
}

Umarım yardımcı olur.
Kolay gelsin.
> 2010/12/22 osman tuzcu <osmantu...@gmail.com>
>
> >  Merhaba aşağıdaki kodla excelden gridview'e veri çekmek istiyorum
> > ancak sayfa boş geliyor. Sql dataadaptor componenti ile bağladığım
> > zaman veriler çıkıyor ama benim kodla yapmam lazım yardımcı
> > olurmusunuz
> > İyi çalışmalar
>
> > _______________________________________________________________________________________
> > string con = "Provider=Microsoft.Jet.OLEDB.4.0;" +
> >            "Data Source=C:\\kantin2.xls;" +
> >            "Extended Properties=Excel 8.0;";
> >            OleDbDataAdapter da = new OleDbDataAdapter("SELECT * FROM
> > [Sayfa2$A1:C37]", con);
> >            DataTable dt = new DataTable();
> >            da.Fill(dt);
> >            GridView1.DataSource = dt;
> >            GridView1.DataBind();
>
> > _______________________________________________________________________________________
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "altdotnetturkiye" group.
> > To post to this group, send email to altdotne...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > altdotnetturki...@googlegroups.com<altdotnetturkiye%2Bunsu...@googlegroups.com>
> > .

Eralp Erat

unread,
Dec 22, 2010, 5:52:22 AM12/22/10
to altdotne...@googlegroups.com
Evet Enes haklı.Ben daha cok excel'in bazı calculationlarını resolve edemediğim için (median vs) sıklıkla ilgili hücreleri ters besleme yaparak sonucları okuyup alıyordum.Excel'i kill etmek win app'da sorun olmuyordu ama böyle bir sıkıntı cikabiliyor.

Ek olaraksunu soleyebilirim , OleDbConnection her zaman sağlıklı okuma yapmıyor..Abuk subuk karakterler ciktiğini gordum defalarca.Zaten Enes de soruyu yazan arkadaşın metodunun daha generic daha derli toplu halini gondermiş ki utility lib'de benzer bir metodu bende kullanıyorum.

E2

2010/12/22 Enes Keskin <nske...@gmail.com>
To unsubscribe from this group, send email to altdotnetturki...@googlegroups.com.

osman tuzcu

unread,
Dec 22, 2010, 6:15:14 AM12/22/10
to altdotne...@googlegroups.com
Enes bey anladığım kadartıyla aralık belirtmeye gerek kalmayacak bir
kod yazmışsınız ama "SELECT * FROM [Sayfa2$A1:C37]" cümlesi
sqldataadapter'a yazdığımda çalışıyorda neden kod tarafında
çalışmıyor.
Birde verdiğiniz kodu tam olarak ne şekilde kullancağımı anlayamadım malesef

Eralp Erat

unread,
Dec 22, 2010, 6:20:54 AM12/22/10
to altdotne...@googlegroups.com
Bu şekilde cevap dönerseniz kimse yardımcı olamaz.

"çalışmıyor " çok generic bir ifade.Hatamı veriyor , veriyorsa ne veriyor vs...?

Kod cok acik.Siz excel'i veriyorsunuz , 1. satırlar dahil edilsin mi diye bir de parametre veriliyor ( o connection string'e has www.connectionstrings.com dan check edebilirsiniz. , malum 1.satırlar genelde title bilgisi olur...) O excel'de yer alan tüm sheetleri okuyup size bir dataset olarak ( aslında table Array de olabilirmis List<DataTable> :)) (muhtemelen DS'e özel birşeyler kullanabilme ihtimaline karşı yada .net 1.1 den kalma bir geliştirme olduğundan (malum genericler sonradan geldi) :)) ) ) geri dönüyor..Hepsi bu...
2010/12/22 osman tuzcu <osman...@gmail.com>
--
You received this message because you are subscribed to the Google Groups "altdotnetturkiye" group.
To post to this group, send email to altdotne...@googlegroups.com.
To unsubscribe from this group, send email to altdotnetturki...@googlegroups.com.

osman tuzcu

unread,
Dec 22, 2010, 6:26:07 AM12/22/10
to altdotne...@googlegroups.com
"boş sayfa geliyor" demiştim en başta o yüzden öyle yazdım kusura
bakmayın.sizde baştan bütün mailleri okumazsızınız haklı olarak :).

Eralp Erat

unread,
Dec 22, 2010, 6:40:15 AM12/22/10
to altdotne...@googlegroups.com
Sayfa boş geliyor bir hata değildir :) (bence)
debug esnasında iglili datatable'i retrieve ettiğini meta data'dan  anlayabiliyorum ama kayıt olmadığı için ekrana kayıt basmıyor gibi bir ifade olursa daha  da yardımcı olabiliriz.. :)

2010/12/22 osman tuzcu <osman...@gmail.com>
"boş sayfa geliyor" demiştim en başta o yüzden öyle yazdım kusura
bakmayın.sizde baştan bütün mailleri okumazsızınız haklı olarak :).

--

osman tuzcu

unread,
Dec 22, 2010, 6:49:31 AM12/22/10
to altdotne...@googlegroups.com
o kadar profesyonel değilim dediklerinizden birşey anlamadım :(

Eralp Erat

unread,
Dec 22, 2010, 7:46:47 AM12/22/10
to altdotne...@googlegroups.com
humm üzgünüm ancak müsait oldukca yazabiliyorum.Cok detaylı aciklama için vaktim yok malesef :(

2010/12/22 osman tuzcu <osman...@gmail.com>
o kadar profesyonel değilim dediklerinizden birşey anlamadım :(

--

osman tuzcu

unread,
Dec 22, 2010, 8:26:12 AM12/22/10
to altdotne...@googlegroups.com
kendimce bir çözüm buldum. datasource nesnesi çalışıyordu bende onun connection stringini degiştirdim. onuda başka bir talodan aldım tabiki
SqlDataReader dr = cmd.ExecuteReader();
               while (dr.Read())
               {                    
                   surl = dr["dosya_url"].ToString();
               }
           
               SqlDataSource1.ConnectionString="DSN=gg;DBQ=C:\\USERS\\OSMAN\\DOCUMENTS\\VISUAL STUDIO 2008\\PROJECTS\\BUTCE2\\BUTCE2\\ADMIN\\DOSYALAR\\" +surl+ ";DefaultDir=C:\\USERS\\OSMAN\\DOCUMENTS\\VISUAL STUDIO 2008\\PROJECTS\\BUTCE2\\BUTCE2\\ADMIN\\DOSYALAR;DriverId=790;FIL=excel 8.0;MaxBufferSize=2048;PageTimeout=5;";
Reply all
Reply to author
Forward
0 new messages