Sayfalar

Translate Blog

18 Nisan 2013 Perşembe

C# ile Word ve Excel Den Veri Almak / Read Docx Word File Using C#

How to get data from Microsot Office Word file, means docx files.
First of all you must add referance to your project. Rigth Click to your project, click  Add Referance.
Choose the COM tab and find Microsoft Word Object Library. This is for using below code.
Dont forget to add using row.
Below code use a.docx that stay in C drive, and add the whole text into the richedit object. So you should add a button and richedit also.

Öncelikle kodları hemen vereceğim ama office referanslarını eklemeniz lazım projenize.
Proje adınıza sağ klik Add Referance, ordan COM sekmesini açın, listede Microsoft Word Object Library seçeneğini ekleyin.

Kodlarınınzın en yukarısında bulunan using satırına ise şunları ekleyin

using Word = Microsoft.Office.Interop.Word;
Sahnenize bir RichEdit ve buton ekleyin, butonunuza şu kodları ekleyin;
Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();
            object miss = System.Reflection.Missing.Value;
            object path = @"C:\a.docx";
            object readOnly = true;
            Microsoft.Office.Interop.Word.Document docs = word.Documents.Open(ref path, ref miss, ref readOnly, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss);
            string totaltext = "";
            for (int i = 0; i < docs.Paragraphs.Count; i++)
            {
                totaltext += " \r\n " + docs.Paragraphs[i + 1].Range.Text.ToString();
            }
            richTextBox1.Text = totaltext;
            docs.Close();
            word.Quit();



İşlem tamam C sürücünüzde bulunan a.docx belgesini okudunuz. Excellide siz deneyin ben denemedim ;) ONun da referansdan eklemelisiniz. Microsoft.Excel, ....

Hiç yorum yok:

Yorum Gönder

Yorumunuz