5
I am generating Excel files in my application using Epplus. Now there is a need to automatically store these files in a folder, regardless of whether the user downloads them or not, in order to make a history of these generated 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? Automatically save the file to a directory before giving return
function. I am doing this:
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 files (images, documents, etc) uploaded by the user (for example in a input type="file"
), I tried to do the same to record Excel but it’s not working.