从该文件夹下载最新的excel文件

时间:2018-03-20 10:20:50

标签: c# asp.net excel download

我将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也

1 个答案:

答案 0 :(得分: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();