如何打开/保存为XLSM

时间:2018-01-25 10:43:55

标签: c# asp.net-mvc excel model-view-controller

您好我有一种方法将数据写入excel报告,此报告包含宏,因此,它必须是.xlsm,但是当我返回文件路径以打开已保存的.xlsm文件时,它默认为.xlsx并且报告无法打开

public ActionResult CustomDateReport(string datepicker, string datepicker1)
{
    using (new ExcelPackage())
    {
        var startDate = DateTime.Parse(datepicker).AddHours(7);
        var endDate = DateTime.Parse(datepicker1).AddHours(7);

        var dataSet = CheckRecords.GetListofRecordsForExcelReport(startDate, endDate);

        var path = "C:\\checks\\AtoBReport.xlsm";

        FileInfo Files = new FileInfo(path);
        ExcelPackage excel = new ExcelPackage(Files);

        var worksheet = excel.Workbook.Worksheets["Sheet1"];

        var row = 2;

        for (var i = 0; i < dataSet.Count; i++)
        {
            worksheet.Cells[row, 1].Value = dataSet[i].DateLogged.ToString();
            worksheet.Cells[row, 2].Value = dataSet[i].Area;
            worksheet.Cells[row, 3].Value = dataSet[i].Checks_Records_Check;
            worksheet.Cells[row, 4].Value = dataSet[i].Green;
            worksheet.Cells[row, 5].Value = dataSet[i].Yellow;
            worksheet.Cells[row, 6].Value = dataSet[i].Red;
            worksheet.Cells[row, 7].Value = dataSet[i].Checks_Records_Department;
            worksheet.Cells[row, 8].Value = dataSet[i].Checks_Records_Comments;
            worksheet.Cells[row, 9].Value = dataSet[i].Value;
            worksheet.Cells[row, 10].Value = dataSet[i].Grey;

            row++;
        }

        worksheet.Cells[1, 14].Value = startDate.ToString("dd/MM/yyyy");
        worksheet.Cells[1, 16].Value = endDate.ToString("dd/MM/yyyy");

        excel.SaveAs(new FileInfo(@"c:\checks\\customReport\AtoBReport1.xlsm"));

        var filePath = @"c:\checks\\customReport\AtoBReport1.xlsm";

        return File(filePath, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    }
    return error;
}

0 个答案:

没有答案