Supress message: he file you are trying to open Test1.xls is in a different format than specified by the file extension.

Last Reply on Nov 21, 2013 07:13 AM By Mudassar

Posted on Nov 21, 2013 07:10 AM

Hi

     I have to export a table to excel, which is dynamically generated.

    I herewith attched my code.  While exporting i am getting the error.

" The file you are trying to open, 'Test[1].xls', is in a different format than specified by the file extension. Verify that the file is not corupted and is from a trusted source before opening the file. Do you want to open the file now?"

 

can anyone help me out.

thanks in advance

  

 

protected void Button1_Click(object sender, EventArgs e)
{

string fileName = "C:\Test.xls";

StreamWriter SWriter = new StreamWriter(fileName);
string str = string.Empty;
string tableName = string.Empty;

str += "<Table><TR><TD align='center'>" + tableName + "</TD></tr>";
str += "<tr><TD align='left' style='font-size:14px' style='width:200px' >" + "Date of meeting or visit " + "</TD> <td style='width:500px'></td><td style='width:500px'></td> <TD align='left' style='font-size:14px' style='width:200px' >" + "Report issued on " + "</TD> </tr>";
str += "<tr><TD align='left' style='font-size:14px' style='width:200px' >" + "rdpMeetingDate.SelectedDate.Value.ToString CultureInfo.CurrentCulture)" + "</TD> <td style='width:800px'></td><td style='width:500px'></td> <TD align='left' style='font-size:14px' style='width:200px' >" + "rdpIssueDate.SelectedDate.Value.ToString(CultureInfo.CurrentCulture)" + "</TD> </tr>";
str += "<tr></tr>";
str += "<tr><TD align='left' style='font-size:14px' style='width:200px' >" + "Place of meeting or visit " + "</TD> <td style='width:500px'></td><td style='width:500px'></td> <TD align='left' style='font-size:14px' style='width:200px' >" + "By " + "</TD> </tr>";
str += "<tr><TD align='left' style='font-size:14px' style='width:200px' >" + "txtPlace.Text" + "</TD> <td style='width:500px'></td><td style='width:500px'></td> <TD align='left' style='font-size:14px' style='width:200px' >" + "txtBy.Text" + "</TD> </tr>";
str += "</table><br/><br/>";
str += "<table><tr><td align='left' style='font-size: 14px' style='width: 100px'>Subject</td> <td align='left' style='font-size: 14px' style='width: 800px'>" + "txtSubject.Text" + "</td></tr>";
str += "<tr><td align='left' style='font-size: 14px; vertical-align: top'>Attended by</td> <td align='left' style='font-size: 14px; vertical-align: top; height: 50px'>" + "txtAttend.Text" + "</td></tr>";
str += "<tr><td align='left' style='font-size: 14px; vertical-align: top'>Copy to</td> <td align='left' style='font-size: 14px; vertical-align: top; height: 50px'>" + "txtCopy.Text" + "</td></tr></TABLE>";

str += "</TABLE>";
SWriter.WriteLine(str);
SWriter.Flush();
SWriter.Close();

FileInfo file = new FileInfo(filePath);

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.WriteFile(file.FullName);
Response.End();

}

 

Posted on Nov 21, 2013 07:13 AM

This is a warning by excel as you ar eexporting HTML to Excel, thus you can ignore it and if you don't want it then you need to use OpenXML

[Solution] ASP.Net GridView Export to Excel: The file you are trying to open is in a different format than specified by the file extension

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html