Click here to Skip to main content
15,882,113 members
Articles / Programming Languages / C#

How to import Excel XML spreadsheets without interop

Rate me:
Please Sign up or sign in to vote.
2.29/5 (4 votes)
28 Aug 2006CPOL 53K   588   15   7
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.

C#
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)


Written By
Architect ABB
Czech Republic Czech Republic
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRun time error Pin
Behrooz_Kashani2-Oct-08 5:22
Behrooz_Kashani2-Oct-08 5:22 
QuestionSimple Question Pin
dandi1830-Apr-07 12:53
dandi1830-Apr-07 12:53 
AnswerRe: Simple Question Pin
Ondra Spilka14-May-07 20:34
professionalOndra Spilka14-May-07 20:34 
GeneralVB 1.1 and add values to cols Pin
Holpit12-Sep-06 23:23
Holpit12-Sep-06 23:23 
Questionhmm [modified] Pin
jw12328-Aug-06 4:17
jw12328-Aug-06 4:17 
AnswerRe: hmm Pin
Ondra Spilka28-Aug-06 19:25
professionalOndra Spilka28-Aug-06 19:25 
Generalfv Pin
one_minute_man28-Aug-06 4:02
one_minute_man28-Aug-06 4:02 

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

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