Save excel automatically generated in ASP MVC application

5

I am generating Excel files in my application using EPPlus . There is now a need to automatically save these files to a folder, regardless of whether or not you download them, so that you can keep track of these files.

In my code I have:

public ActionResult ExportToExcel(string serie, int numDoc, int servicoID, int agrupamento)
        {
            ARTSOFT.dal.ViewModels.GetDadosComerciais dbArt = new ARTSOFT.dal.ViewModels.GetDadosComerciais();

            var contratoPai = db.DadosComerciais.Where(d => d.Serie == serie && d.NumDoc == numDoc).FirstOrDefault();
            var servicoPai = db.Servicos.Find(servicoID);

            using (var package = new ExcelPackage())
            {
                package.Workbook.Worksheets.Add("Test");
                ExcelWorksheet ws = package.Workbook.Worksheets[1];
                ws.Name = "Folha1"; //Setting Sheet's name
                ws.Cells.Style.Font.Size = 10; //Default font size for whole sheet
                ws.Cells.Style.Font.Name = "Calibri"; //Default Font name for whole sheet

                //Merging cells and create a center heading for out table
                ws.Cells[1, 1].Value = "Relatório - Cópia de Contratos"; // Heading Name
                ws.Cells[1, 1, 1, 10].Merge = true; //Merge columns start and end range
                ws.Cells[1, 1, 1, 10].Style.Font.Bold = true; //Font should be bold
                ws.Cells[1, 1, 1, 10].Style.Font.Size = 13;
                Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#D1F5C6");
                ws.Cells["A1:J1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
                ws.Cells["A1:J1"].Style.Fill.BackgroundColor.SetColor(colFromHex);
                ws.Cells[1, 1, 1, 10].Style.HorizontalAlignment = ExcelHorizontalAlignment.Center; // Aligmnet is center

                #region Dados contrato Pai

                #region lista de contratos copiados

                var memoryStream = package.GetAsByteArray();
                var fileName = string.Format("CopiaContrato_" + serie + "_" + numDoc + "-{0:yyyy-MM-dd-HHmmss}.xlsx", DateTime.UtcNow);
                // mimetype from http://stackoverflow.com/questions/4212861/what-is-a-correct-mime-type-for-docx-pptx-etc

                return base.File(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", fileName);
            }
        }

The end result of the function is to allow the user to download the generated file. What am I trying to do now? Save the file automatically to a directory before giving return of the function. For this I am doing:

HttpPostedFileBase filepath = (HttpPostedFileBase)Request.Files[0];
var fileName2 = Path.GetFileName(fileName);
var path = Path.Combine((ConfigurationManager.AppSettings["pathFiles"]), fileName);//ir buscar ao xml
filepath.SaveAs(path);

This code above is what I use to save user-uploaded files (images, documents, etc.) (for example in input type="file" ), I tried to do the same to save Excel but it is not working.

    
asked by anonymous 24.04.2014 / 11:15

1 answer

5

I already have the solution. To automatically save Excel generated in the application just do:

System.IO.File.WriteAllBytes(@"D:\Report.xlsx", memoryStream);//Guardar ficheiro automaticamente
    
24.04.2014 / 11:33