A 2D array to Excel file converter. Generate Microsoft Excel file from a 2D array in C#. Support making the first row headers and custom worksheet name. .Net Core 3.0 framework and depend on the Open Xml SDK.
.Net Core ≥ 3.0
DocumentFormat.OpenXml = 2.10.1
- 2D array to Excel
- Making the first row headers
Create a 2D array first.
string[,] datas = new string[,]
{
{ "Name", "Country", "Age", "Career" },
{ "Helen", "U.S.", "21", "Police" },
{ "Jucia", "Canada", "34", "Dancer" },
{ "Erik", "Canada", "13", "Student" },
{ "Bob", "British", "26", "Business person" },
{ "Nancy", "Russia", "64", "Fisherman" },
};
Convert it to a Excel file and save to a local file.
datas.GenerateExcel(filename); //First row as non-headers
datas.GenerateExcel(filename, true); //First row as headers
Or save it to a MemoryStream.
//Option 1
MemoryStream ms = datas.GenerateExcel(); //First row as non-headers
MemoryStream ms = datas.GenerateExcel(true); //First row as headers
//Option 2
using (MemoryStream ms = new MemoryStream())
{
datas.GenerateExcel(ms); //First row as non-headers
datas.GenerateExcel(ms, true); //First row as headers
}
This project is under the MIT License.