1

Okay, this might be a bit unusual. If there are better ways to do this (that are just as easy, I'm open to ideas). I found a while ago that I could open a web page consisting of a Gridview or a table, with titles, etc. as an excel file and it worked great! It formatted the Excel file with colors and alignment similar to the html from the page. With later versions of excel, though, it gives me a warning that the format time isn't valid before opening it, though it still seemed to work. So I tried changing the content type to a more current version of excel, but then I don't get anything at all. Here's what I have been doing (below).

Does anyone know how to change it so that I can open the page in a current version of Excel without getting the warning?

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

    Response.AppendHeader("Content-disposition", "attachment; filename=Filename.xls")
    Response.ContentType = "application/vnd.ms-excel"


End Sub
Steve
  • 531
  • 5
  • 16

1 Answers1

3

You are not creating an Excel file.

You are creating a HTML file with a .xls file extension. That's the wrong extension for a HTML file, and that's why Excel gives you a warning. The correct extension would be .html or .htm. Unfortunately, .html files don't automatically open in Excel, so changing the extension would require your users to manually open the file in Excel instead of just double-clicking it.

I'm afraid there's no easy way to solve this. We had the same problem, and we solved it by creating a real Excel file. There are lots of Excel libraries for .NET available. We used SpreadsheetLight, because it easily allows you to copy a DataTable to an Excel file and send that file to the web client.

Community
  • 1
  • 1
Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • I've used the excel library before, and I've used Aspose.net (nice product), but this is a personal project and I don't want to sink a lot of money into it. I thought it worked years ago, and it is so easy. I'll look at the links, though. Thanks! – Steve Jan 03 '15 at 03:04
  • 1
    Spreadsheet light looks great! I think I'll try that. Thanks again! – Steve Jan 03 '15 at 03:12