Click here to Skip to main content
Licence CPOL
First Posted 28 Aug 2006
Views 33,232
Downloads 344
Bookmarked 14 times

How to import Excel XML spreadsheets without interop

By | 28 Aug 2006 | Article
Simple way to import XML Excel spreadsheets into a DataTable.

Introduction

Did you ever need to import Excel spreadsheets? If you did, you know there're sometimes problems with interop assemblies deploying to desktops and so.

Office 2003 offers to store spreadsheets in XML. So store your spreadsheet as XML and try to import them. Just include ExcelReader.cs in your project and use the SPOTX namespace. Then you'll be able to add the imported DataTable as datasource to your grid, like in the included example.

using SPOTX;

namespace ExcelImportTest
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void importXMLSpreadsheetToolStripMenuItem_Click(object sender, 
                     EventArgs e)
        {
            //select file in dialog and then fill DataSOurce of main grid 
            if (ctlOfn.ShowDialog() == DialogResult.OK)
                this.ctlGridImport.DataSource = 
                          SPOTX.ExcelReader.ReadExcelXML(ctlOfn.FileName);
        }
    }
}

The first row in the imported Excel file are column headers. The method was tested on contiguous spreadsheets only. The code is clear enough I think, so adapting for .NET 1.1 consists only in replacing the Dictionary by Hashtable. Also modifying import to return collections or to omit column headers seems easy.

Hope this will help somebody.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ondra Spilka

Software Developer (Senior)
eLinkx
Czech Republic Czech Republic

Member

Currently senior developer at eLinkx, started with C\C++ in industry software companies. Mainly .NET GUI, OR and application server frameworks.
Hobbies - Astronomy (selfmade Dobson f/6 1200mm), HW (Atmel51, ARM), Sport (paragliding, footbal, tennis, skiing, skating ... definitely I like all sports).
My family is wide - with my wife we have 3 kids.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralRun time error PinmemberBruce_Kashani5:22 2 Oct '08  
QuestionSimple Question Pinmemberdandi1812:53 30 Apr '07  
AnswerRe: Simple Question PinmemberOndra Spilka20:34 14 May '07  
GeneralVB 1.1 and add values to cols PinmemberHolpit23:23 12 Sep '06  
Questionhmm [modified] Pinmemberjw1234:17 28 Aug '06  
AnswerRe: hmm PinmemberOndra Spilka19:25 28 Aug '06  
Generalfv Pinmemberone_minute_man4:02 28 Aug '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 28 Aug 2006
Article Copyright 2006 by Ondra Spilka
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid