0

I am following this example but I don't know how to make a header row in the final outputed excel file.

1
  • Your link is to the main FileHelpers page. Commented Mar 21, 2012 at 9:45

1 Answer 1

1

You have to provide a 'template' Excel file with just the headers.

// Class record
[DelimitedRecord("|")]
public class MyClass
{
    public string Field1 { get; set; }
    public int Field2 { get; set; }
    public string Field3 { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        // create some data to export
        MyClass[] rows = new MyClass[2] { 
          new MyClass() { Field1 = "Apples",  Field2 = 23, Field3 = "Yes" },
          new MyClass() { Field1 = "Oranges", Field2 = 17, Field3 = "No"} 
        };

        ExcelStorage provider = new ExcelStorage(typeof(MyClass));
        // Set the destination Excel spreadsheet
        provider.FileName = @"MyClass.xlsx";

        // Template.xlsx contains just the column headers on row 1
        provider.TemplateFile = @"Template.xlsx"; 
        // StartRow is after the header row
        provider.StartRow = 2; 

        provider.OverrideFile = true;
        provider.InsertRecords(rows);
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

So I have to have another excel file with just the headers in it? It can't figure out based on the fields?
That's correct. The current (2.9.9) implementation of ExcelStorage.InsertRecords() does not generate any headers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.