Hello Sir,
Please suggest i want to download excel file in asp.net core
but giving error
IOException: The process cannot access the file 'E:\Backup Project\TESTEYNEW1\WebApplication1\WebApplication1\wwwroot\Data\FinalReport1.xlsx' because it is being used by another process.
this is when i click buttin inside index Exporrt data method called when this method called report generated insde a data folder then after download action button click first time it will work when again if i click generate report or download report then this mesage come.
[HttpPost]
public IActionResult Index(OutWardReport outWard)
{
DataTable dt = new DataTable();
DataSet Ds = new DataSet();
string query = "";
int id = 0;
dt = obj.GetReport(id);
if (dt.Rows.Count > 0)
{
Ds.Tables.Add(dt);
ExportDataSet(Ds, "FinalReport.xlsx");
ViewBag.Message = String.Format("Hurrah!!!!!! report generated");
}
return View();
}
public void ExportDataSet(DataSet ds, string destination)
{
using (var workbook = SpreadsheetDocument.Create(destination, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
{
var workbookPart = workbook.AddWorkbookPart();
workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();
foreach (System.Data.DataTable table in ds.Tables)
{
var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);
DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);
uint sheetId = 1;
if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
{
sheetId =
sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}
DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
sheets.Append(sheet);
DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
List<String> columns = new List<string>();
foreach (System.Data.DataColumn column in table.Columns)
{
columns.Add(column.ColumnName);
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
headerRow.AppendChild(cell);
}
sheetData.AppendChild(headerRow);
foreach (System.Data.DataRow dsrow in table.Rows)
{
DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
foreach (String col in columns)
{
DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
newRow.AppendChild(cell);
}
sheetData.AppendChild(newRow);
}
}
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot","Data", "FinalReport1.xlsx");
FileInfo file = new FileInfo(path);
//file.OpenRead();
if (file.Exists)//check file exsit or not
{
file.Delete();
workbook.SaveAs(path);
}
else
{
workbook.SaveAs(path);
}
workbook.Close();
GC.WaitForFullGCComplete();
GC.WaitForPendingFinalizers();
GC.Collect();
//ExcelEngineObject.Dispose();
}
}
public async Task<IActionResult> Download()
{
var path = Path.Combine(
Directory.GetCurrentDirectory(), "wwwroot", "Data", "FinalReport1.xlsx");
var memory = new MemoryStream();
//memory.Close();
using (var stream = new FileStream(path, FileMode.Open,FileAccess.ReadWrite,FileShare.ReadWrite))
{
await stream.CopyToAsync(memory);
stream.Close();
}
memory.Position = 0;
return File(memory, GetContentType(path), Path.GetFileName(path));
}
private string GetContentType(string path)
{
var types = GetMimeTypes();
var ext = Path.GetExtension(path).ToLowerInvariant();
return types[ext];
}
private Dictionary<string, string> GetMimeTypes()
{
return new Dictionary<string, string>
{
{".txt", "text/plain"},
{".pdf", "application/pdf"},
{".doc", "application/vnd.ms-word"},
{".docx", "application/vnd.ms-word"},
{".xls", "application/vnd.ms-excel"},
{".xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"},
{".png", "image/png"},
{".jpg", "image/jpeg"},
{".jpeg", "image/jpeg"},
{".gif", "image/gif"},
{".csv", "text/csv"}
};
}