Hi all,
To create the Excel files (.XLSX) using C# ASPNET I have added the Open XML and Closed XML reference from nugget packages.
The purpose is to generate them and store them on the folder shared with other users.
I can not understand why happen that when try delete files xlsx in folder.
"the file is being used by another process."
The process is w3wp.exe
I need restart IIS on the server for delete all files in the folder.
Any suggestion?
public static void MTemptyxlxs()
{
string AppLocation = "";
AppLocation = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);
AppLocation = AppLocation.Replace("file:\\", "");
foreach (string file in Directory.GetFiles(AppLocation, "*.xlsx").Where(item => item.EndsWith(".xlsx")))
{
File.Delete(file);
}
}
public static 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 (XLWorkbook wb = new XLWorkbook())
{
for (int i = 0; i < ds.Tables.Count; i++)
{
wb.Worksheets.Add(ds.Tables[i], ds.Tables[i].TableName);
}
wb.Style.Alignment.Horizontal = XLAlignmentHorizontalValues.Center;
wb.Style.Font.Bold = true;
wb.SaveAs(filepath);
}
}