You're attempting to open the Excel file
on the server. When you run the site locally, that works, since the client and server are the same computer. When you deploy the site to a real server, it's not going to work. You need to send the Excel file to the client using
Response.TransmitFile
.
Also, based on your comment, you're trying to write the file to the root of the
C:
drive. Since your site won't be running as an administrative user, it won't have permission to write to that location. Pick a folder to which it does have access - for example, the
App_Data
folder within the site.
DataSet ds = CreateSampleData();
string targetFolder = Server.MapPath("~/App_Data/");
string excelFilename = Path.GetRandomFileName() + ".xlsx";
string excelPath = Path.Combine(targetFolder, excelFilename);
CreateExcelFile.CreateExcelDocument(ds, excelPath);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("Content-Disposition", "attachment;filename=Sample.xlsx");
Response.TransmitFile(excelPath);
Response.Flush();
File.Delete(excelPath);