I have an angular / dotnet core application where i need to generate a excel / csv file on server and then download it.
Tried tons of solutions including epplus.core but none seem to return the file (download it). For some reason i always get a hashed value in my network response.
UI:
employeesDownload(account: string, sid: string): Observable<any> {
let params = new HttpParams();
params = params.set("sid", sid);
let headers = new HttpHeaders();
return this.http.get(`em/api/v1/employees/${account}/download`, { headers:headers, params: params })
.pipe(
map((response: any) => response)
);
}
Server:
[HttpGet("{account}/download")]
public async Task<IActionResult> EmployeesDownloadResult(string sid)
{
int _sid = Convert.ToInt32(sid);
if (_sid > 0)
{
//procedure call by sid
StoredProcedureResponse legacyResponse = await this._legacyUdtApiClient.ApiV1EmployeeGetImportedResultAsync(_sid);
if (legacyResponse.ReturnValue.Equals("OK", StringComparison.OrdinalIgnoreCase))
{
DataSet result = JsonConvert.DeserializeObject<DataSet>(legacyResponse.OutputParameter.ToString());
// DataSet To CSV conversion needs to be tested
string CSV = ConversionHelper.ConvertWorkersDataSetToCSV(result);
string[] strings = CSV.Split(',');
byte[] file1;
byte[] bytes;
MemoryStream stream = new MemoryStream();
using (ExcelPackage package = new ExcelPackage(stream))
{
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Employee");
//First add the headers
worksheet.Cells[1, 1].Value = "ID";
worksheet.Cells[1, 2].Value = "Name";
worksheet.Cells[1, 3].Value = "Gender";
worksheet.Cells[1, 4].Value = "Salary (in $)";
//Add values
worksheet.Cells["A2"].Value = 1000;
worksheet.Cells["B2"].Value = "Jon";
worksheet.Cells["C2"].Value = "M";
worksheet.Cells["D2"].Value = 5000;
worksheet.Cells["A3"].Value = 1001;
worksheet.Cells["B3"].Value = "Graham";
worksheet.Cells["C3"].Value = "M";
worksheet.Cells["D3"].Value = 10000;
worksheet.Cells["A4"].Value = 1002;
worksheet.Cells["B4"].Value = "Jenny";
worksheet.Cells["C4"].Value = "F";
worksheet.Cells["D4"].Value = 5000;
bytes = package.GetAsByteArray();
}
HttpContext.Response.Headers.Add("Content-Disposition", "inline; filename=[filename]");
return File(bytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "test.xlsx");
}
}
else
throw new Exception($"download failed");
return null;
}
}
Network http response:
Code is still under construction so bare in mind any unused variables etc.
The basic idea is that I have a array of strings, I will need to download that array in a excel/csv file. We are using micro service arch via docker. Any help would be highly appreciated.