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

Read and Write Excel File Dynamically

, 7 Jul 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
This tip describes how to read Excel files and make an array of objects from Excel.


This tip discusses reading and writing Excel files dynamically. This tip can help you extract Excel data and make a list of objects that is generated for that Excel file.


At first, I wrote this library for report result, but now I use it more for converting Excel to JSON!

Using the Code

In this tip, I decided to describe two parts, one part related to how I can read Excel file and the second part describes how I can generate a class dynamically and fill it.

When an Excel file is passed to the code, the first row of Excel file chooses as a class properties that we want to generate it. This functionality is implemented in GetRowValues static method in Wisgance.Office.Excel.Reader.Read class:

 private static List<string> GetColumnValues(Stream file, string sheetName, string reference)
            var result = new List<string>();
            //Read Excel File by OpenXml Library
            using (var document = SpreadsheetDocument.Open(file, false))
                var workbook = document.WorkbookPart;
                var theSheet = workbook.Workbook.Descendants<Sheet>().FirstOrDefault(s => s.Name == sheetName) ??
                                 workbook.Workbook.Descendants<Sheet>().FirstOrDefault(sheet => true);
                var worksheet = (WorksheetPart)(workbook.GetPartById(theSheet.Id));
                var cells = worksheet.Worksheet.Descendants<Cell>().Where(c => GetCellCol(c.CellReference).ToUpper() == reference);
                //get cell data by calling ExtractCellValue function
                result.AddRange(from theCell in cells where theCell != null select ExtractCellValue(theCell, workbook));
            return result;

In this function and some others like GetRowValues or GetCellData exists argument named references. This argument gets Excel cell address, for example for first row data, we send "1" but for first column, we send "A".

After extracting first row data, we passed these data to the CreateNewObject function in Wisgance.Reflection.WisganceTypeGenerator class.

  public static object CreateNewObject(List<FieldMask> props)
            var myType = CompileResultType(props);
            var myObject = Activator.CreateInstance(myType);
            return myObject;

In the first line, we generate a type via extracted data from Excel (CompileResultType) . In the second line, we create an instance of this class and return it.

while (true)
                var obj = WisganceTypeGenerator.CreateNewObject(objProp);
                var values = GetRowValues(stream, "", (k + 1).ToString());
                if (!values.Any())
                for (var c = 0; c < objProp.Count; c++)
                        var propertyInfo = obj.GetType().GetProperty(objProp[c].FieldName);
                        propertyInfo.SetValue(obj, values[c], null);
                    catch (Exception) { }

While true loop, this part is for reading data till it reaches the empty row. In each iteration, an object instantiates and fills properties value by selected row cell values.

For writing a list of objects in Excel, call Do function in Wisgance.Office.Excel.Writer.Write.

if (headerNames == null)
	headerNames = new ExcelHeaderList();
	foreach (var o in ObjUtility.GetPropertyInfo(objects[0]))
       		headerNames.Add(o, o);

If headerNames is null, automatically set the property name as an Excel header, and also you can pass headerList if you want to customize the header name.

You can download it in Nuget here or see the source in GitHub here.


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


About the Author

Behrooz Bahrameh

Iran (Islamic Republic Of) Iran (Islamic Republic Of)
No Biography provided

Comments and Discussions

GeneralMy vote of 2 PinmemberArslan Elahi26-Feb-14 2:52 
GeneralRe: My vote of 2 PinmemberBehrooz Bahrameh9-Apr-14 10:34 
GeneralMy vote of 5 PinprofessionalAmir Mohammad Nasrollahi27-Jul-13 23:14 
GeneralMy vote of 5 PinmemberM.Kouchi9-Jul-13 1:55 
GeneralMy vote of 5 PinmemberAfshin Mehrabani8-Jul-13 21:03 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150129.1 | Last Updated 7 Jul 2013
Article Copyright 2013 by Behrooz Bahrameh
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid