Use this package to easily (and fast) export a DataTable to Excel (XLSX)
This component is Lightweight
Code Samples:
class Program
{
static void Main(string[] args)
{
// get some data to work with
// Load DataTable (usally this comes from SQL, but here we load it from binary serialized datatable)
//DataTable dt = GDS.XLSX.DataTableHelper.LoadDataTableFromBinary(@"D:\dt\c48242bad49f49899a139714c97a0790");
DataTable dt = new DataTable("SomeTable");
dt.Columns.Add("Product Name", typeof(string));
dt.Columns.Add("Downloads", typeof(int));
dt.Columns.Add("Freeware", typeof(bool));
dt.Rows.Add("Easy Poster Printer", 1_427_144, false);
dt.Rows.Add("Shield Defender", 450, true);
/********************************************************************************
* THE SIMPLEST SOLUTION
********************************************************************************/
// Step 1. Create an instance of XLSXGenerator
GDS.XLSX.XLSXGenerator xlsx = new GDS.XLSX.XLSXGenerator();
// Step 2. Save the xlsx document
xlsx.SaveXLSX(dt, System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),"myfile.xlsx"));
// Step 3. There is no step 3!
// done
/********************************************************************************
* The build in class helper for DataTable:
********************************************************************************/
// Get a DataTable bytes
//var arr = GDS.XLSX.DataTableHelper.GetDataTableToBytes(dt);
// Save DataTable to file.
//System.IO.File.WriteAllBytes(@"D:\dt\c48242bad49f49899a139714c97a0790", arr);
/********************************************************************************
* The more advanced example, with custom rendering etc.
*********************************************************************************/
xlsx.HeaderBold = true; // set column header bold (default is true)
xlsx.HeaderVisible = true; // set if header should be shown at all (default is true)
// Column Render
xlsx.OnColumnHeaderRender += (ref string columnHeaderText, int columnIndex, ref bool bold)=>
{
// explic change column header text, and bold-strategy pr. column
if (columnHeaderText == "Downloads")
{
columnHeaderText = "* Downloads *";
//bold = false;
}
};
// Cell Render
xlsx.OnCellRender += (int rowIndex, int columnIndex, ref Type dataType, ref object data, ref bool suppress) =>
{
// change cell rendering strategy (suppress or/and alter data and data type)
if (dataType == typeof(bool))
{
dataType = typeof(string);
data = ((bool)data) ? "Yes" : "No";
}
};
// Row Render
xlsx.OnRowRender += (int rowIndex, ref bool skip) =>
{
// change row render strategy (skip) - If you want to skip specific rows for some reason
// Example: skip = (rowIndex % 2 == 0);
};
/********************************************************************************
* SAVING THE DOCUMENT (Several ways to do it, depending on your needs)
********************************************************************************/
// save to file
xlsx.SaveXLSX(dt, System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "myfile2.xlsx"));
// save to Stream
//using (MemoryStream stream = new MemoryStream())
//{
// xlsx.SaveXLSX(dt, stream);
// System.IO.File.WriteAllBytes(@"d:\dt\test2-mem-stream.xlsx", stream.ToArray());
//}
// get bytes
//var bytes = xlsx.GetXLXSBytes(dt);
//System.IO.File.WriteAllBytes(@"d:\dt\test3-bytes.xlsx", bytes);
}
}
Download the demo source (GDSXLSXDemo.zip) here