MVC4 文件下载controller

MVC4 文件下载controller
public ActionResult DownloadOwnerOutPutExcel()
        {
            FilePathResult ret =  null;

             string sourceFile = Server.MapPath("~/App_Data/Shinrin/ExcelTemplate/") + "森林の土地の所有者届出書.xls";
             string destFile = Server.MapPath("~/App_Data/Download/") + "森林の土地の所有者届出書" + Session.SessionID + DateTime.Now.Ticks + ".xls";

            IWorkbook objBook =  null;
            IWorksheets objSheets =  null;
            IWorksheet objSheet =  null;

             try
            {
                 if (System.IO.File.Exists(destFile))
                    System.IO.File.Delete(destFile);

                 // ファイル形式を変えるため、保存します。
                objBook = SpreadsheetGear.Factory.GetWorkbook(sourceFile);
                objBook.SaveAs(destFile, SpreadsheetGear.FileFormat.Excel8);
                objBook = SpreadsheetGear.Factory.GetWorkbook(destFile);
                objSheets = objBook.Worksheets;
                objSheet = objSheets["所有者届出書"];

                objBook.Save();
            }
             catch
            {
            }

            ret =  new FilePathResult(destFile, "application/vnd.ms-excel");
            ret.FileDownloadName = Server.UrlEncode(System.IO.Path.GetFileName("森林の土地の所有者届出書.xls"));
             return ret;
        }

你可能感兴趣的:(MVC4 文件下载controller)