【问题标题】:Download the latest excel file from the folder从文件夹中下载最新的excel文件
【发布时间】:2018-03-20 10:20:49
【问题描述】:

我将 excel 文件存储在基于SYSDATE(当前日期)的文件夹中。现在我不想下载文件夹中的所有文件。

但我想从基于时间的文件夹中下载最新文件。这是我的代码的样子。目前我正在从 zip 文件夹中下载所有 excel 文件。

else if (strSelectedReportType == "RCOMReports")
                {
                    string strReportFile = ConfigurationManager.AppSettings["RCOMReports"].ToString();
                    string strFilePath = ConfigurationManager.AppSettings["ReportDirectory"].ToString() + "\\" + DateTime.Now.ToString("dd-MM-yyyy");

                    using (ZipFile zip = new ZipFile())
                    {
                        zip.AlternateEncodingUsage = ZipOption.AsNecessary;

                        if (Directory.Exists(strFilePath))
                        {
                            DirectoryInfo di = new DirectoryInfo(strFilePath);

                            FileInfo[] FileInfo = di.GetFiles();

                            if (FileInfo.Length > 0)
                            {
                                foreach (FileInfo item in FileInfo)
                                {
                                    zip.AddFile(item.FullName, "Files");
                                }
                            }
                            MemoryStream ms = new MemoryStream();

                            Stream Objstream = new MemoryStream(ms.ToArray());
                            Objstream.Seek(0, SeekOrigin.Begin);

                            zip.AddEntry(strReportFile, Objstream);

                            HttpContext.Current.Response.Clear();
                            HttpContext.Current.Response.BufferOutput = false;
                            string zipName = String.Format("Zip_{0}.zip", strReportFile);
                            HttpContext.Current.Response.ContentType = "application/zip";
                            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + zipName);
                            zip.Save(HttpContext.Current.Response.OutputStream);
                            HttpContext.Current.Response.End();
                        }
                        else
                        {
                            ScriptManager.RegisterStartupScript(Page, Page.GetType(), "Alert", "alert('No Reports as on today's date..!!');", true);
                        }

                    }

                }

现在我也不想要 Zip

【问题讨论】:

  • @nikhilmehta:谢谢 nikhil,我到了这里myFile。现在我应该如何让它下载。 ??
  • @nikhilmehta:我在FileInfo file = new FileInfo(myFile.Name); 有文件名,但它在if (file.Exists) 给我的结果是假的

标签: c# asp.net excel download


【解决方案1】:

找到最后一个文件并下载。

    var directory = new DirectoryInfo(ConfigurationManager.AppSettings["ReportDirectory"]);

    // the latest excel file
    var file = directory.GetFiles().OrderByDescending(c => c.LastWriteTime).FirstOrDefault();

    if (file == null)
    {
        return;
    }

    var content = File.ReadAllBytes(file.FullName);

    HttpContext.Current.Response.ContentType = "text/csv";
    HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + file.Name + ".xlsx");
    HttpContext.Current.Response.BufferOutput = true;
    HttpContext.Current.Response.OutputStream.Write(content, 0, content.Length);
    HttpContext.Current.Response.End();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多