Hello friends, I have here a method where I build an excel using the Excel Package , but I have the following situation, I need to give a different name to each file that I make, this is my code.
public string SaveCreditQuotation(SolicitudCreditoCalculadora creditQuo) {
var response = "";
var message = creditQuo.fullmsn.ToString();
var mail_user = creditQuo.mail_user.ToString();
var lapsodias = creditQuo.lapsos;
var username = creditQuo.username;
var totalapagar = creditQuo.totalapagar;
DateTime dtime = new DateTime();
int idint = RandomNumber(1, 1000000000);
string id = idint.ToString() ;
string filename = "cotizacion" + "_" + id + "_" +
String.Format("{0:d}", dtime) + ".xls";
var plength = creditQuo.payments.Count;
using(ExcelPackage excelPackage = new ExcelPackage()) {
//Set some properties of the Excel document
excelPackage.Workbook.Properties.Author = "xxxx";
excelPackage.Workbook.Properties.Title = "xxxx";
excelPackage.Workbook.Properties.Subject = "xxx";
excelPackage.Workbook.Properties.Created = DateTime.Now;
//Create the WorkSheet
OfficeOpenXml.ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets.Add("Sheet 1");
//Add some text to cell A1
for (int i = 0; i < plength; i++) {
worksheet.Cells[1 + i, 1].Value = creditQuo.payments[i].IndicePago;
worksheet.Cells[1 + i, 2].Value = creditQuo.payments[i].Fecha;
worksheet.Cells[1 + i, 3].Value = creditQuo.payments[i].Cantidad;
}
//Save your file
FileInfo fi = new FileInfo(@ "wwwroot\filescreated\"+filename);
excelPackage.SaveAs(fi);
}
response = "";
return response;
}
I made some edits and changes to my code, now this throws me in the
excelPackage.SaveAs(fi);
the value of filename is :
"quotation_462038704_01/01/0001.xls"
error that throws me:
DirectoryNotFoundException: Cannot find part of path 'C:\Users\miuser\source\repos\myproject\myproject\wwwroot\filescreated\quotation_462038704_01\01\0001.xls
It sends me an error, does anyone know how I can rename my created file?
Well everything seemed to be a problem with the dates, since some formats bring
/ ó \
them so I solved it by doing this:And that's it, the date was already a problem to save the file
If you are in a web environment with asp.net, use the
to have the physical route based on a virtual one
Now, if you are going to create several files, I think you should generate it with a different name and not step on the previous one.
so add a date and time so that each file is different