繁体   English   中英

从文件夹下载最新的Excel文件

[英]Download the latest excel file from the folder

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM