Hello friends...
I am trying to download gridview's dada in EXCEL file..
for this First I'm creating a file and using Response.Addheader to download it...
The file downaloded successfully.. no issue with this..
I have no problem while this program executes.... But when I try to open my EXCEL file it doesn't open.
For this I looked into Task Manager that already a EXCEL process working due to which I'm not able to open.. so every time I need to kill the process and then open the file..
I used Process class to kill the process through the program this helped me bit..
because I don't need to close process explicitly from Task manager...
But again... If I have opened 3 other excel file this approach closes all the previous opened files..
Help me...
I'm pasting code below
string fileName = "Record FROM " + ddlFromDate.SelectedValue.Trim() + " to " + ddlTodate.SelectedValue.Trim();
if (File.Exists(@"D:\Manish\" + fileName))
{
fileName += "1";
}
Excel.ApplicationClass excel = new Excel.ApplicationClass();
Excel.Workbook workbook = excel.Application.Workbooks.Add(true);
excel.Cells[1, 1] = "Caller ID";
excel.Cells[1, 2] = "Holder Name";
excel.Cells[1, 3] = "Job Level";
excel.Cells[1, 4] = "Org.Unit";
excel.Cells[1, 5] = "District";
excel.Cells[1, 6] = "Episode";
excel.Cells[1, 7] = "Recording Date";
excel.Cells[1, 8] = "Recording Time";
int row = 1;
foreach (DataRow currRow in exportdata.Rows)
{
row++;
for (int i = 1; i < 9; i++)
{
excel.Cells[row, i] = currRow[exportdata.Columns[i].ColumnName];
}
}
string path = @"D:\Manish\"+fileName;
object missing = System.Reflection.Missing.Value;
workbook.SaveAs(path, Excel.XlFileFormat.xlExcel7, missing, missing,false, false, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing);
excel.Quit();
excel.Application.Workbooks.Close();
foreach(Process proc in Process.GetProcessesByName("EXCEL")
{
proc.Kill();
}
FileInfo getInfo = new FileInfo(@"D:\Manish\" + fileName + ".xls");
if (getInfo.Exists)
{
Response.AddHeader("Content-Disposition", "attachment; filename=" + getInfo.Name);
Response.ContentType = "application/vnd.xls";
Response.WriteFile(getInfo.FullName);
Response.End();
}