Click here to Skip to main content
Click here to Skip to main content

Import Excel File to DataSet

By , 29 May 2012
 

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).
    1. UPDATED: I'm now setting the IMEX flag to 0. This may cause worse performance. Check out  http://www.connectionstrings.com/excel for more information.
    2. UPDATED: I switched it to use ACE driver if it has an XLSX extension. You will need to ensure ACE is installed on the server you deploy to.
      1. http://www.microsoft.com/en-us/download/details.aspx?id=13255 
    3. UPDATED: Please refer to these links for good information about connecting to xls and xlsx files. 
      1. http://www.connectionstrings.com/excel
      2. http://www.connectionstrings.com/excel-2007  
  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 

public static DataSet ImportExcelXLS(string FileName, bool hasHeaders) {
    string HDR = hasHeaders ? "Yes" : "No";
    string strConn;
    if (FileName.Substring(FileName.LastIndexOf('.')).ToLower() == ".xlsx")
        strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + FileName + ";Extended Properties=\"Excel 12.0;HDR=" + HDR + ";IMEX=0\"";
    else
        strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + FileName + ";Extended Properties=\"Excel 8.0;HDR=" + HDR + ";IMEX=0\"";

    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();

            if (!sheet.EndsWith("_")) {
                try {
                    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);
                } catch (Exception ex) {
                    throw new Exception(ex.Message + string.Format("Sheet:{0}.File:F{1}", sheet, FileName), ex);
                }
            }
        }
    }
    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.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionVoted 5 Pinmemberantew17 Dec '12 - 1:38 
GeneralMy vote of 5 Pinmemberparslej15 Oct '12 - 6:17 
QuestionHttpPostedFile Pinmemberluffyripper29 Aug '12 - 3:46 
AnswerRe: HttpPostedFile PinmemberColinBashBash29 Aug '12 - 3:50 
GeneralRe: HttpPostedFile Pinmemberluffyripper29 Aug '12 - 5:43 
GeneralMy 5! Pinmember_Amy10 Aug '12 - 3:11 
GeneralWorking very well! PinmemberPavan Gayakwad6 Aug '12 - 20:50 
GeneralMy Vote of 5 PinmemberSrinivasan from Chennai20 Jun '12 - 19:33 
GeneralMy vote of 5 Pinmemberkaedei29 May '12 - 15:47 
QuestionData is not filled in enough into Dataset Pinmemberbotngot8328 May '12 - 20:37 
AnswerRe: Data is not filled in enough into Dataset PinmemberColinBashBash29 May '12 - 3:39 
GeneralRe: Data is not filled in enough into Dataset Pinmemberbotngot8329 May '12 - 22:00 
GeneralRe: Data is not filled in enough into Dataset Pinmemberbotngot8331 May '12 - 17:18 
GeneralRe: Data is not filled in enough into Dataset PinmemberBruce Goodman10 Jun '12 - 22:32 
Bug'inputFileStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException' PinmemberMember 35080023 Apr '12 - 21:59 
GeneralRe: 'inputFileStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException' PinmemberColinBashBash4 Apr '12 - 3:54 
QuestionI recive this error message "Could not find installable ISAM" Pinmemberpachiflyer5 Apr '11 - 3:45 
AnswerRe: I recive this error message "Could not find installable ISAM" PinmemberColinBashBash5 Apr '11 - 4:25 
GeneralexcException.Message = "Data at the root level is invalid. Line 1, position 1." Pinmemberjohn_17267 Jan '11 - 11:21 
GeneralRe: excException.Message = "Data at the root level is invalid. Line 1, position 1." PinmemberColinBashBash7 Jan '11 - 11:29 
GeneralRe: excException.Message = "Data at the root level is invalid. Line 1, position 1." PinmemberColinBashBash7 Jan '11 - 11:32 
GeneralMy vote of 5 PinmemberMember 74095413 Nov '10 - 20:15 
GeneralExternal Table not supported Pinmembersujit.bhujbal23 Feb '10 - 18:29 
GeneralRe: External Table not supported PinmemberHardy198824 Oct '10 - 18:37 
GeneralList<ColumnType> columns = new List<ColumnType>(); Pinmemberfargodude12 Feb '10 - 5:11 
GeneralRe: List columns = new List(); PinmemberColinBashBash12 Feb '10 - 5:24 
GeneralRe: List columns = new List(); PinmemberColinBashBash12 Feb '10 - 5:26 
GeneralI did the opposite some time ago PinmvpSacha Barber12 Jan '09 - 21:58 
GeneralRe: I did the opposite some time ago Pinmemberwizardzz17 Apr '12 - 7:44 
GeneralRe: I did the opposite some time ago PinmvpSacha Barber17 Apr '12 - 10:35 
GeneralRe: I did the opposite some time ago Pinmemberwizardzz17 Apr '12 - 10:42 
GeneralRe: I did the opposite some time ago PinmemberSlacker00729 May '12 - 5:16 

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
Web03 | 2.6.130516.1 | Last Updated 29 May 2012
Article Copyright 2009 by ColinBashBash
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid