Spire.XLS is a professional Excel API that enables developers to create, manage, manipulate, convert and print Excel worksheets. Get free and professional technical support for Spire.XLS for .NET, Java, Android, C++, Python.

Thu Sep 15, 2022 8:34 am

Is it possible to create an excel file from a list of objects in c#?

Documentation only shows how to create it from a DataTable

xel_wise
 
Posts: 13
Joined: Thu Sep 15, 2022 8:33 am

Thu Sep 15, 2022 10:05 am

Hello xel_wise,

Thanks for your inquiry.
You can use my code below to import data from the list collection into Excel. If there are any problems after the test, please feel free to contact us.
Code: Select all
             //Create a workbook
            Workbook workbook = new Workbook();
            //Create an empty worksheet
            workbook.CreateEmptySheets(1);
            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Create an ArrayList object
            ArrayList list = new ArrayList();
            //Add strings in list
            list.Add("Spire.Doc for .NET");
            list.Add("Spire.XLS for .NET");
            list.Add("Spire.PDF for .NET");
            list.Add("Spire.Presentation for .NET");
            //Insert array list in worksheet
            sheet.InsertArrayList(list, 1, 1, true);
            //Save the Excel file
            string result = "ImportDataFromArrayList_out.xlsx";
            workbook.SaveToFile(result, ExcelVersion.Version2013);


Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Thu Sep 15, 2022 10:32 am

Thank you for your reply.

However this is not what I'm looking for.

I have this list of MySheet object and I want it to convert to Excel:

Code: Select all
var list = new List<MySheet> { new MySheet { Value1 = 1, Value2 = 2, Value3 = 3 } };

xel_wise
 
Posts: 13
Joined: Thu Sep 15, 2022 8:33 am

Fri Sep 16, 2022 9:34 am

Hello xel_wise,

As the above sample code, our product supports inserting data from ArrayList, for your situation, you need to convert your data to ArrayList first. If there is still any issues, just feel free to contact.

Sincerely,
Simple
E-iceblue support team
User avatar

Simple.Li
 
Posts: 248
Joined: Fri Jul 01, 2022 2:33 am

Return to Spire.XLS