Skip to content

Instantly share code, notes, and snippets.

@jbollman7
Last active October 12, 2021 00:27
Show Gist options
  • Save jbollman7/9d327fa68b5024e105be10b9e84f9490 to your computer and use it in GitHub Desktop.
Save jbollman7/9d327fa68b5024e105be10b9e84f9490 to your computer and use it in GitHub Desktop.
Datatable to excel file
using System;
using System.Data;
using ClosedXML.Excel;
using System.Linq;
using System.IO;
namespace datatablepractice
{
class Program
{
static void Main(string[] args)
{
DataTable dataTable = new DataTable();
dataTable.Columns.Add("zpid", typeof(System.Int32));
dataTable.Columns.Add("bed", typeof(System.Double));
dataTable.Columns.Add("bath", typeof(System.Double));
dataTable.Columns.Add("livingarea", typeof(System.Double));
dataTable.Columns.Add("homeType", typeof(System.String));
dataTable.Columns.Add("taxAssessedValue", typeof(System.Double));
dataTable.Columns.Add("lotSize", typeof(System.Double));
dataTable.Columns.Add("rentEstimate", typeof(System.Double));
dataTable.Columns.Add("rentToPricepercent", typeof(System.Double));
dataTable.Columns.Add("GrossRentMultiplier", typeof(System.Double));
dataTable.Columns.Add("priceEstimate", typeof(System.Double));
dataTable.Columns.Add("priceListed", typeof(System.Double));
dataTable.Columns.Add("linkToListing", typeof(System.String));
dataTable.Rows.Add(new object[] { 1, 3.0,2.5,1854,118000,9522,1400,1,8,14000,150000,23423,"linkhere" });
using (var workbook = new XLWorkbook())
{
var worksheet = workbook.Worksheets.Add(dataTable, "testSheetName");
string filepath = "C:\\Users\\joseph.bollman\\Documents\\testy.xlsx";
//string fileName = "Sample.xlsx";
workbook.SaveAs(filepath);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment