Click here to Skip to main content
Licence CPOL
First Posted 8 Jan 2009
Views 54,539
Downloads 1,722
Bookmarked 66 times

Import Excel File to DataSet

By ColinBashBash | 8 Jan 2009
Allows for inputting Excel tables into a DataSet.
 
Part of The SQL Zone sponsored by
See Also

1

2

3
2 votes, 33.3%
4
4 votes, 66.7%
5
4.44/5 - 6 votes
μ 4.44, σa 0.90 [?]

Introduction

I just wanted to put out two ways to import data from Excel into a DataSet.

  1. Preferred - Importing from an XLS file using the OLE Jet engine (simple).
  2. Importing from an Excel XML file. (The XML format that Excel uses, not just any XML file.)
    1. Note: This is a long-winded custom solution. Should work, but might require tweaks.
    2. This works well if you're sure the data will be valid, or if you don't require it to do type-detection (flag for this on procedure).
    3. Exporting to an Excel XML file can be found here.

Using the code

Download the file for specifics, but here's a summary:

XLS Import

private static DataSet ImportExcelXLS(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;
}

Excel XML Import (Summary)

public static DataSet ImportExcelXML(Stream inputFileStream, 
                      bool hasHeaders, bool autoDetectColumnType) {
    XmlDocument doc = new XmlDocument();
    doc.Load(new XmlTextReader(inputFileStream));
    XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);

    nsmgr.AddNamespace("o", "urn:schemas-microsoft-com:office:office");
    nsmgr.AddNamespace("x", "urn:schemas-microsoft-com:office:excel");
    nsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");

    DataSet ds = new DataSet();

    foreach (XmlNode node in 
      doc.DocumentElement.SelectNodes("//ss:Worksheet", nsmgr)) {
        DataTable dt = new DataTable(node.Attributes["ss:Name"].Value);
        ds.Tables.Add(dt);
        XmlNodeList rows = node.SelectNodes("ss:Table/ss:Row", nsmgr);
        if (rows.Count > 0) {

            //*************************
            //Add Columns To Table from header row
            //*************************
            List<ColumnType> columns = new List<ColumnType>();
            int startIndex = 0;
            if (hasHeaders) {
                foreach (XmlNode data in rows[0].SelectNodes("ss:Cell/ss:Data", nsmgr)) {
                    columns.Add(new ColumnType(typeof(string)));//default to text
                    dt.Columns.Add(data.InnerText, typeof(string));
                }
                startIndex++;
            }
            //*************************
            //Update Data-Types of columns if Auto-Detecting
            //*************************
            if (autoDetectColumnType && rows.Count > 0) {
                XmlNodeList cells = rows[startIndex].SelectNodes("ss:Cell", nsmgr);
                int actualCellIndex = 0;
                for (int cellIndex = 0; cellIndex < cells.Count; cellIndex++) {
                    XmlNode cell = cells[cellIndex];
                    if (cell.Attributes["ss:Index"] != null)
                        actualCellIndex = 
                          int.Parse(cell.Attributes["ss:Index"].Value) - 1;

                    ColumnType autoDetectType = 
                      getType(cell.SelectSingleNode("ss:Data", nsmgr));

                    if (actualCellIndex >= dt.Columns.Count) {
                        dt.Columns.Add("Column" + 
                          actualCellIndex.ToString(), autoDetectType.type);
                        columns.Add(autoDetectType);
                    } else {
                        dt.Columns[actualCellIndex].DataType = autoDetectType.type;
                        columns[actualCellIndex] = autoDetectType;
                    }

                    actualCellIndex++;
                }
            }
            //*************************
            //Load Data
            //*************************
            for (int i = startIndex; i < rows.Count; i++) {
                DataRow row = dt.NewRow();
                XmlNodeList cells = rows[i].SelectNodes("ss:Cell", nsmgr);
                int actualCellIndex = 0;
                for (int cellIndex = 0; cellIndex < cells.Count; cellIndex++) {
                    XmlNode cell = cells[cellIndex];
                    if (cell.Attributes["ss:Index"] != null)
                        actualCellIndex = int.Parse(cell.Attributes["ss:Index"].Value) - 1;

                    XmlNode data = cell.SelectSingleNode("ss:Data", nsmgr);

                    if (actualCellIndex >= dt.Columns.Count) {
                        for (int i = dt.Columns.Count; i < actualCellIndex; i++) {
                            dt.Columns.Add("Column" + 
                                       actualCellIndex.ToString(), typeof(string));
                            columns.Add(getDefaultType());
                        }
                        ColumnType autoDetectType = 
                           getType(cell.SelectSingleNode("ss:Data", nsmgr));
                        dt.Columns.Add("Column" + actualCellIndex.ToString(), 
                                       typeof(string));
                        columns.Add(autoDetectType);
                    }
                    if (data != null)
                        row[actualCellIndex] = data.InnerText;

                    actualCellIndex++;
                }

                dt.Rows.Add(row);
            }
        }
    }
    return ds;
}
    //*************************
    //Format of file, in case you're wondering
    //*************************

    //<?xml version="1.0"?>
    //<?mso-application progid="Excel.Sheet"?>
    //<Workbook>
    // <Worksheet ss:Name="Sheet1">
    //  <Table>
    //   <Row>
    //    <Cell><Data ss:Type="String">Item Number</Data></Cell>
    //    <Cell><Data ss:Type="String">Description</Data></Cell>
    //    <Cell ss:StyleID="s21"><Data ss:Type="String">Item Barcode</Data></Cell>
    //   </Row>
    // </Worksheet>
    //</Workbook>

License

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

About the Author

ColinBashBash

Software Developer

United States United States

Member
likes boardgames, computer games, and enjoys his .net programming job.

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
QuestionI recive this error message "Could not find installable ISAM" Pinmemberpachiflyer4:45 5 Apr '11  
AnswerRe: I recive this error message "Could not find installable ISAM" PinmemberColinBashBash5:25 5 Apr '11  
GeneralexcException.Message = "Data at the root level is invalid. Line 1, position 1." Pinmemberjohn_172612:21 7 Jan '11  
GeneralRe: excException.Message = "Data at the root level is invalid. Line 1, position 1." PinmemberColinBashBash12:29 7 Jan '11  
GeneralRe: excException.Message = "Data at the root level is invalid. Line 1, position 1." PinmemberColinBashBash12:32 7 Jan '11  
GeneralMy vote of 5 PinmemberMember 740954121:15 3 Nov '10  
GeneralExternal Table not supported Pinmembersujit.bhujbal19:29 23 Feb '10  
GeneralRe: External Table not supported PinmemberHardy198819:37 24 Oct '10  
GeneralList columns = new List(); Pinmemberfargodude6:11 12 Feb '10  
GeneralRe: List columns = new List(); PinmemberColinBashBash6:24 12 Feb '10  
GeneralRe: List columns = new List(); PinmemberColinBashBash6:26 12 Feb '10  
GeneralI did the opposite some time ago PinmvpSacha Barber22:58 12 Jan '09  

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
Web02 | 2.5.120210.1 | Last Updated 9 Jan 2009
Article Copyright 2009 by ColinBashBash
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid