通过创建日期文件夹并在其中保存来存储文件

时间:2018-03-09 06:37:17

标签: c# excel console-application

我想存储excel文件内部日期文件夹,我将动态创建。所以我写了代码如下

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;
        }
    }

但即使是文件夹也没有创建,我收到错误

  

无法将类型系统io directoryinfo隐式转换为字符串

在行: -

strDateFolder = Directory.CreateDirectory(DateTime.Now.ToString("dd-MM-yyyy"));

1 个答案:

答案 0 :(得分:1)

我认为你需要这样的东西:

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");

希望这有效。无法完全检查它。