0

我有一个从我的 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);
        }
    }
4

1 回答 1

0

好的,所以删除返回行中对 BLConstants.ControlTemplateFileName 的引用,并将其替换为(我假设)var 数据中的字符串,最后是 .xlsx?像 data.CompanyName + ".xlsx" 也许?

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

如果此方法被重新用于导出其他数据类型,您将需要考虑到这一点并根据导出的内容附加文件类型

例如 data.CompanyName + exportTypeExtension

于 2013-09-25T09:32:20.517 回答