通过创建日期文件夹并保存在其中来存储文件 [英] Storing file by creating datefolders and saving inside it

查看:103
本文介绍了通过创建日期文件夹并保存在其中来存储文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

但是即使未创建文件夹,也会出现错误as

But even the folder is not created and I get error as


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

cannot implicitly convert type system io directoryinfo to string

在以下行:-

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

推荐答案

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

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.

这篇关于通过创建日期文件夹并保存在其中来存储文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆