Michells Mesterværk

Læs excel fil med C#

by Administrator 28. January 2010 14:20

Jeg sidder til tider og bikser med excel ark, og nogle gange er jeg nødt til, ad-hoc, at flytte data fra excel til evt. en database eller xml fil. At arbejde med excel fra .NET er noget bøvl synes, ihvertfald med de indbyggede COM objekter. Selvom vi har fået dynamics og optionelle parametre, synes jeg stadigvæk det halter.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Interop.Excel; 
namespace ExcelReader
{
    class Program
    {
        static void Main(string[] args)
        {
            Application application = new Application();
            
            Workbook workbook =
                application.Workbooks.Open(@"C:\BloggersAndInfluencers.xlsx");

            try
            {
                Worksheet worksheet = 
                    (Worksheet)workbook.Worksheets.get_Item("Sheet1");
                
                Range range = worksheet.get_Range("A1", "A100" );

                foreach (Range item in range)
                {
                    if(item.Value2 != null)
                        Console.WriteLine(item.Value2.ToString());
                }
                Console.ReadLine();

                releaseObject(worksheet);
            }
            finally {
                workbook.Close();
                application.Quit();

                releaseObject(workbook);
                releaseObject(application);
            }
        }

        private static void releaseObject(object obj)
        {
            try
            {
                System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);
                obj = null;
            }
            catch (Exception ex)
            {
                obj = null;
            }
            finally
            {
                GC.Collect();
            }
        }
    }
}

Tags:

Comments

1/28/2010 2:45:19 PM #

Søren Larsen

Du kan jo også vælge at bruge ADO.Net istedet for excel-automation

davidhayden.com/.../2973.aspx

Søren Larsen Denmark

1/31/2010 10:58:05 PM #

Martin Madsen

En lav-praktisk-enkelt-sags løsning, som jeg ofte benytter: Start import funktionen i SQL Enterprise Manager. Angiv Excel arket som kilde og hæld alt data over i en eller flere tabeller på SQL serveren.
Og derefter kan data behandlingen foretages med t-SQL, C# og jeg skal gi' dig skal jeg.

Martin Madsen Denmark

2/1/2010 9:29:28 AM #

Thue Møller Jensen

En andel lav-praktisk, men yderst effektiv metode er, at benytte Excel som Sql-generator. Det er yderst effektivt, hvis det ikke er de samme data/tabeller, der benyttes hver gang.

Der er en funktion, der hedder "Sammenkædning". Skriv starten på et insert-statement i et felt (her A1) "INSERT INTO tabelnavn (fornavn, efternavn, alder) VALUES ("

og så sammenkædnings-funktionen ved første række ($-tegnet låser feltet når formlen kopieres til resten af rækkerne, benyt evt. F4 når feltet vælges):
=SAMMENKÆDNING($A$1;"'";A2;"', '";B2;"', ";C2;")")
INSERT INTO tabelnavn (fornavn, efternavn, alder) VALUES ('Kurt', 'Kurtsen', 36)

Kopier funktionen til alle efterfølgende rækker, og kopier så de flettede celler over i sql-enterprise manager og kør dem.

Thue Møller Jensen Denmark

2/14/2010 1:46:02 PM #

اخبار

Thank you very much for the possibility to have a look into the office. Nice boy, he has got the talent to explain.

اخبار Egypt

2/28/2010 3:14:44 AM #

stock market

I would like to use some of your material for my website. Please let me know if I can do so if I link back to this page.

stock market United States

2/28/2010 11:30:54 AM #

stock broker

I was wondering if you would be interested in becoming a guest poster on my website? and in exchange you could include a link your post? Please reply when you get a chance and I will send you my contact details - thanks.

stock broker United States

3/4/2010 6:21:54 AM #

free hd movies

I like watching movies online, it is way cheaper than going to the theaters.

free hd movies United States

3/4/2010 8:12:36 AM #

watch movies online free

I like streaming movies online, it is way cheaper than going to the theaters.

watch movies online free United States

3/7/2010 7:47:22 AM #

free online games

Hi, good day. Pretty cool work. You have gained a new reader. Pls continue this great work and I look forward to more of your excellent articles. Have a nice day, Tom..

free online games Turkey

3/9/2010 8:42:19 PM #

WoW Mobile

Do you know which mobile phone companies allow tethering?

WoW Mobile United States

3/17/2010 2:09:08 AM #

ssk sorgulama

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

ssk sorgulama United States

3/17/2010 11:54:58 AM #

arac sorgulama

I admire the valuable information you offer in your articles. I will bookmark your blog and have my children check up here often. I am quite sure they will learn lots of new stuff here than anybody else!

arac sorgulama United States

3/19/2010 10:42:11 AM #

wow leveling guide

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

wow leveling guide United States

3/20/2010 3:30:50 PM #

ehliyet

Excellent post.I want to thank you for this informative read, I really appreciate sharing this great post. Keep up your work.

ehliyet Denmark

3/22/2010 1:42:03 AM #

south park s14e5

I love this website. Expecting more like this.

south park s14e5 United States

3/23/2010 12:43:40 AM #

Carl from Miracle Traffic Bot has said

Do you plan to produce any kind of video tutorials? Maybe you should!

Carl from Miracle Traffic Bot has said Bulgaria

3/23/2010 6:24:38 AM #

cigarette e-cig

On That Point there r certainly a lot of details like that to take into consideration

cigarette e-cig United States

3/28/2010 11:55:36 AM #

Boyce Gerry

My friend and I were just arguing about the topic you referred to in your blog post and apparently, I am the winner of the argument! Thank you for clearing it out.

Boyce Gerry United States

3/29/2010 2:41:42 AM #

privat Krankenversicherung

I identified your blog on yahoo and discover a few of the posts. Excellent work!

privat Krankenversicherung Austria

3/29/2010 5:07:11 PM #

crane hire

Hi vey nice interesting blog im from london i found this on aol I'm in the crane hire company business,i found this blog very interesting good luck with it i will return to this blog soon

crane hire United Kingdom

3/30/2010 8:55:45 AM #

Harrison Eckler

This is a nice website. Are you using Wordpress as your main platform? I ask that because your website looks a little different...

Harrison Eckler United States

4/3/2010 9:51:58 AM #

wow leveling guide

Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I'll be subscribing to your feed and I hope you post again soon.

wow leveling guide United States

4/3/2010 11:35:12 AM #

Kegel exercises for men

Hi there, just wanted to leave a quick comment about the interface of your blog. It is really easy on the eye while also being catchy. I think I will do something similar for my blog as well. Thanks for the nice blog share.

Kegel exercises for men United States

4/5/2010 1:32:35 AM #

link building profile

Great job on maintaining this website. Do you have an RSS feed I can register with?

link building profile United States

4/6/2010 6:29:18 AM #

0% credit cards

Needless to say, what a vital net website and revealing posts, I will put back link - bookmark this site? Regards

0% credit cards United States

4/6/2010 8:00:25 PM #

horde leveling guide

Admiring the time and effort you put into your blog and detailed information you offer! I will bookmark your blog and have my children check up here often. Thumbs up!

horde leveling guide United States

4/7/2010 2:04:04 AM #

Niomi Banks

Maybe it would be just a little better to use a more comprehensive website design for your blog. It will get you more visitors and inbound links.

Niomi Banks United States

4/7/2010 4:01:59 AM #

Annelle Cabrero

Stumbled into this site by chance but I’m sure glad I clicked on that link. You definitely answered all the questions I’ve been dying to answer for some time now.  Will definitely come back for more of this. Thank you so much

Annelle Cabrero United States

4/7/2010 8:03:25 PM #

canon ef 50mm f/1.8 ii camera lens

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

canon ef 50mm f/1.8 ii camera lens United States

4/7/2010 9:27:53 PM #

New York Payday Loans

Nothing gives a person so much advantage over another as to remain always cool and unruffled under all circumstances.

New York Payday Loans United States

4/7/2010 10:31:15 PM #

flavored coffee

Good post, good job. It Couldn't be written any improved. Reading this post reminds me of my former employer! He always kept talking about this. I will forward this article to him. Pretty sure he will have a superb read. Thanks for posting!

flavored coffee United States

Add comment


(Will show your Gravatar icon)

(De 3 specielle karaktere i det danske alfabet?)
  Country flag

biuquote
  • Comment
  • Preview
Loading



Powered by BlogEngine.NET 1.6.0.0
Theme by Mads Kristensen

Om Mr. Frost

Developer Evangelist i Microsoft Danmark.