问题
I want to store excel file Inside date folder which I will create dynamically. So I wrote the code Like below
public void ExportExcel(string strWorkbookName, DataSet ds)
{
string strFilePath = "";
string strDateFolder = "";
try
{
using (XLWorkbook wb = new XLWorkbook())
{
wb.Worksheets.Add(ds);
//wb.SaveAs(ConfigurationRead.GetAppSetting("ReportDirectory") + "Report.xlsx");
strDateFolder = Directory.CreateDirectory(DateTime.Now.ToString("dd-MM-yyyy"));
strFilePath = ConfigurationRead.GetAppSetting("ReportDirectory") + "\\" + strDateFolder + "\\" + "Report.xlsx";
}
}
catch (Exception)
{
throw;
}
}
But even the folder is not created and I get error as
cannot implicitly convert type system io directoryinfo to string
at line:-
strDateFolder = Directory.CreateDirectory(DateTime.Now.ToString("dd-MM-yyyy"));
回答1:
I think you need something like that:
string reportDirectory = ConfigurationRead.GetAppSetting("ReportDirectory");
strDateFolder = Directory.CreateDirectory(
Path.Combine(reportDirectory, DateTime.Now.ToString("dd-MM-yyyy"))).Name;
strFilePath = Path.Combine(reportDirectory, strDateFolder, "Report.xlsx");
Hope this works. Could not fully check it.
来源:https://stackoverflow.com/questions/49187909/storing-file-by-creating-datefolders-and-saving-inside-it