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

时间:2013-09-25 08:58:23

标签: c# asp.net-mvc filenames export-to-excel

我的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);
        }
    }

1 个答案:

答案 0 :(得分:0)

好的,那么在返回行中删除对BLConstants.ControlTemplateFileName的引用,并将其替换为(我假设)var数据的字符串,最后是.xlsx?像data.CompanyName +“。xlsx”或许?

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

如果重新使用此方法导出其他数据类型,则需要考虑该方法并根据导出的内容追加文件类型

e.g。 data.CompanyName + exportTypeExtension