C#/-MVC :导出 Excel 文件的文件名

本文关键字:文件 文件名 Excel 导出 -MVC | 更新日期: 2023-09-27 18:32:52

我有一个从我的MVC应用程序中导出的excel文件。导出有效,但我很难给出 Excel 文件的名称以在下拉列表中选择列表。用户选择公司名称,然后执行 Excel 的导出。我希望我能将所选公司的名称提供给Excel,但我不知道该怎么做。

public class BLConstants
{
    public static readonly string DashboardCookieName = "dashboard-filters";
    public static readonly string EnumResourcePrefix = "ENUM_";
    public static readonly int RecentDaysCount = 30;
    public static readonly int Minimal_Score = 13;
    public static readonly int Control_Default_ControlAction = 2;
    public static readonly int Control_Default_Sensitivity = 3;
    public static readonly int Control_Default_Status = 3;
    public static readonly int Control_Default_Frequency = 4;
    public static readonly int Control_Default_ControlState = 1;
    public static readonly int CompliancePercentage_Good = 100;
    public static readonly int CompliancePercentage_Average = 54;
    public static readonly int CompliancePercentage_Bad = 13;
    public static readonly int CompliancePercentage_NotApplicable = 0;
    #region Mappings
    public static readonly string GoodLetter = "J";
    public static readonly string AverageLetter = "K";
    public static readonly string BadLetter = "L";
    public static readonly Dictionary<string, Frequencies> FrequenciesMapping = new Dictionary<string, Frequencies>
        {
            {"Monthly", Frequencies.Monthly},
            {"Quarterly", Frequencies.Quarterly},
            {"Semi-annually", Frequencies.BiAnnualy},
            {"Yearly", Frequencies.Yearly}
        };
    public static readonly Dictionary<string, Sensitivities> SensitivitiesMapping = new Dictionary<string, Sensitivities>
        {
            {"HIGH", Sensitivities.High},
            {"MEDIUM", Sensitivities.Medium},
            {"LOW", Sensitivities.Low}
        };
    public static readonly Dictionary<string, Statuses> StatusesMapping = new Dictionary<string, Statuses>
        {
            {"J", Statuses.Good},
            {"K", Statuses.Average},
            {"L", Statuses.Bad},
            {"NA", Statuses.NotApplicable}
        };
    #endregion
    public static readonly int MaxCalendarMonthsBeforeNow = 11;    //Max : 23
    public static readonly string Reset_Password_Status = "InitPassword";
    public static readonly string HelpFilePath = "~/Content/Documents";
    public static readonly string HelpFileContentType = "application/pdf";
    public static readonly int ControlFirstDataLine = 4;
    public static readonly string ControlTemplate = "~/Content/Documents/ControlTemplate.xlsx";
    public static readonly string ControlTemplateContentType = "application/vnd.ms-excel.12";
    public static readonly string ControlTemplateFileName = "Controls.xlsx";      
    public static readonly int RegulationFirstDataLine = 4;
    public static readonly string RegulationTemplate = "~/Content/Documents/RegulationTemplate.xlsx";
    public static readonly string RegulationTemplateContentType = "application/vnd.ms-excel.12";
    public static readonly string RegulationTemplateFileName = "Regulations.xlsx";
    public static readonly string ExcelFrenchUrlValue = "French";
    public static readonly string ExcelEnglishUrlValue = "English";
}

这是我想更改的部分:

public static readonly string ControlTemplateFileName = "Controls.xlsx";

我有一个公司列表,我希望导出的 excel 文件的名称与所选公司的名称相匹配。

对于下拉菜单:

<select>
  <label for="companySelect">ADMIN_CTRL_EXPORT_COMPANY</label>
  <select id="SelectedCompany" class="ui-state-valid" name="SelectedCompany">
    <option>Select a company</option>
    <option value="1">Company1</option>
    <option value="2">Company2</option>
    <option value="3">Company3</option>
  </select>
</select>

实现这一点的最佳方法是什么?

对于导出 Excel :

        [HttpPost]
    [ValidateAntiForgeryToken]
    public FileStreamResult _ControlsExport(ControlsExportModel model)
    {
        try
        {
            if (!ModelState.IsValid)
                //TODO
                return null;
            LoggingService.Write(string.Format("Exporting the controls for the company ID: {0}", model.SelectedCompany));
            int parsedId;
            if (!int.TryParse(model.SelectedCompany, out parsedId))
                throw new ArgumentException("model.SelectedCompany is not an int.");
            var data = ContextDataService.GetModuleExportData(parsedId);
            var stream = ExcelService.ExportModule(data);
            return File(stream, BLConstants.ControlTemplateContentType, BLConstants.ControlTemplateFileName);
        }
        catch (Exception e)
        {
            throw new ApplicationException("Error in AdminController._ControlsExport(ControlsExportModel model)", e);
        }
    }

C#/-MVC :导出 Excel 文件的文件名

好的,

所以删除返回行中对 BLConstants.ControlTemplateFileName 的引用,并将其替换为(我假设)var 数据中的字符串,末尾是 .xlsx?公司名称+".xlsx"也许?

我所做的假设是var数据包含有关公司的信息

但是,如果重复使用此方法导出其他数据类型,则需要考虑到这一点,并根据要导出的内容附加文件类型

例如数据。公司名称 + 导出类型扩展