Hello Everyone,
I am new to c# and am trying to use Open XML to pull data from a SQL server into a spreadsheet. I followed some examples I found online but had to alter them to fit the test scenario I have set up and have run into an error I am not sure how to debug.

I am receiving error code CS0120, "An object reference is required for the non-static field, method or property 'Program.CreateContentRow(int, int, string, string, string)'"

I tried adding code along the lines of "Row contentRow = new Row()", because that is pretty much the extent of what my current knowledge is regarding object references, but that does not fix the problem. Is the issue with my code that I need to pull the code from the static main method? Any help or information would be greatly appreciated. I am trying to understand what I did wrong here so I know how to do things differently in the future.


using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using DocumentFormat.OpenXml;
    using DocumentFormat.OpenXml.Packaging;
    using DocumentFormat.OpenXml.Spreadsheet;
    using System.IO;

    namespace ExcellTakeTwo
    class Program
        private const string ExcelTemplateFile = @"Template.xlsx";
        private const string GeneratedExcelFile = @"Security.xlsx";
        string[] headerColumns = new string[] { "A", "B", "C", "D" };
        static void Main(string[] args)
            // Make a copy of template file
            File.Copy(ExcelTemplateFile, GeneratedExcelFile, true); 

            //Open the copied template workbook           
            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(GeneratedExcelFile, true))
                // Access the main workbook part with all of the references
                WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;

                // Grab the first worksheet
                WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

                // SheetData will contain all the data
                SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();

                //Connect to the database Security
                WyattsLinqDataContext wyattsLinqDataContext = new WyattsLinqDataContext();

                // Data starts on row 2
                int index = 2;

                // Select * from Security table
                var securityQuery =
                    from Security
                    in wyattsLinqDataContext.Securities
                    select Security;

                // For each row in database add a row to spreadsheet
                foreach (var item in securityQuery)
                    int personId = item.PersonID;
                    string lastName = item.LastName;
                    string firstName = item.FirstName;
                    string userPass = item.Password;

                    //Add a new row                    
                    Row contentRow = CreateContentRow(index, personId, lastName, firstName, userPass);

                    // Append new row to data sheet


        Row CreateContentRow(int index, int personId, string lastName, string firstName, string userPass)
            // Create new row
            Row row = new Row();

            row.RowIndex = (UInt32)index;

            // first cell is a text cell, so create it and append it

            Cell firstCell = CreateTextCell(headerColumns[0], personId.ToString(), index);


            // Create cells that contain data

            for (int i=1; i < headerColumns.Length; i++)
                Cell cell = new Cell();

                cell.CellReference = headerColumns[i] + index;

                CellValue cellValue = new CellValue();

                if (i == 1)
                    cellValue.Text = lastName.ToString();
                else if (i == 2)
                    cellValue.Text = firstName.ToString();
                    cellValue.Text = userPass.ToString();
            return row;
        Cell CreateTextCell(string header, string text, int index)
            // Create new inline string cell
            Cell cell = new Cell();

            cell.DataType = CellValues.InlineString;

            cell.CellReference = header + index;

            // Add text to text cell
            InlineString inlineString = new InlineString();

            Text newText = new Text();

            newText.Text = text;


            return cell;
9 Months
Discussion Span
Last Post by tinstaafl

Try declaring your methods as static:

static Row CreateContentRow(int index, int personId, string lastName, string firstName, string userPass)

static Cell CreateTextCell(string header, string text, int index)
Votes + Comments
This lead me to a working solution! Thank you so much!
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.