I got this Error
:
System.TypeInitializationException was unhandled HResult=-2146233036 Message=The type initializer for 'ClosedXML.Excel.XLWorkbook' threw an exception. Source=ClosedXML TypeName=ClosedXML.Excel.XLWorkbook
StackTrace: at ClosedXML.Excel.XLWorkbook..ctor() at ClosedXml.Form1.ExportDataSetToExcel(DataSet ds) in c:\users\test\documents\visual studio 2010\Projects\ClosedXml\ClosedXml\Form1.cs:line 41 at ClosedXml.Form1.Form1_Load(Object sender, EventArgs e) in c:\users\test\documents\visual studio 2010\Projects\ClosedXml\ClosedXml\Form1.cs:line 30 at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(Message& m) at System.Windows.Forms.Form.WndProc(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) InnerException: System.IO.FileNotFoundException HResult=-2147024894 Message=Could not load file or assembly 'DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The system cannot find the file specified. Source=ClosedXML FileName=DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 FusionLog==== Pre-bind state information === LOG: DisplayName = DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 (Fully-specified) LOG: Appbase = file:///c:/users/test/documents/visual studio 2010/Projects/ClosedXml/ClosedXml/bin/Debug/ LOG: Initial PrivatePath = NULL Calling assembly : ClosedXML, Version=0.69.1.0, Culture=neutral, PublicKeyToken=fd1eb21b62ae805b. === LOG: This bind starts in default load context. LOG: No application configuration file found. LOG: Using host configuration file: LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config. LOG: Post-policy reference: DocumentFormat.OpenXml, Version=2.0.5022.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 LOG: Attempting download of new URL file:///c:/users/test/documents/visual studio 2010/Projects/ClosedXml/ClosedXml/bin/Debug/DocumentFormat.OpenXml.DLL. LOG: Attempting download of new URL file:///c:/users/test/documents/visual studio 2010/Projects/ClosedXml/ClosedXml/bin/Debug/DocumentFormat.OpenXml/DocumentFormat.OpenXml.DLL. LOG: Attempting download of new URL file:///c:/users/test/documents/visual studio 2010/Projects/ClosedXml/ClosedXml/bin/Debug/DocumentFormat.OpenXml.EXE. LOG: Attempting download of new URL file:///c:/users/test/documents/visual studio 2010/Projects/ClosedXml/ClosedXml/bin/Debug/DocumentFormat.OpenXml/DocumentFormat.OpenXml.EXE.StackTrace: at ClosedXML.Excel.XLWorkbook..cctor() InnerException:
And I am using the Code below:
private void Form1_Load(object sender, EventArgs e)
{
DataSet ds = new DataSet();
DataTable dt = new DataTable();
dt.Columns.Add("Name");
dt.Columns.Add("Country");
dt.Rows.Add("Venkatesh", "India");
dt.Rows.Add("Santhosh", "USA");
dt.Rows.Add("Venkat Sai", "Dubai");
dt.Rows.Add("Venkat Teja", "Pakistan");
ds.Tables.Add(dt);
ExportDataSetToExcel(ds);
}
public void ExportDataSetToExcel(DataSet ds)
{
string AppLocation = "";
AppLocation = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
AppLocation = AppLocation.Replace("file:\\", "");
string date = DateTime.Now.ToShortDateString();
date = date.Replace("/", "_");
string filepath = AppLocation + "\\ExcelFiles\\" + "RECEIPTS_COMPARISON_" + date + ".xlsx";
using (ClosedXML.Excel.XLWorkbook wb = new ClosedXML.Excel.XLWorkbook())
{
for (int i = 0; i < ds.Tables.Count; i++)
{
wb.Worksheets.Add(ds.Tables[i], ds.Tables[i].TableName);
}
wb.Style.Alignment.Horizontal = ClosedXML.Excel.XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
wb.SaveAs(filepath);
}
}
PM> Install-Package ClosedXML
here bro github.com/ClosedXML/ClosedXML – Tấn Nguyên