Wir mussten in einer Webapplikation einen Excel Report erstellen. Der Benutzer kann vorab wählen in welchem Format der Report erstellt werden soll. Er kann zwischen Excel 2003 (xls) und Excel 2007 (xlsx) wählen.
Den MIME Type haben wir zuerst so gesetzt.
response.setContentType("application/vnd.ms-excel");
Mit folgendem Statement wird erzwungen das ein File Download Dialog erscheint. Dateien mit bekanntem MIME Type werden ansonsten direkt im Browserfenster geöffnet (http://support.microsoft.com/kb/260519)
response.addHeader("Content-disposition", "attachment;filename=test.xls");
Dies funktioniert für das Excel 2003 Format ohne Probleme. Wenn wir nun eine Excel 2007 Format exportieren sendet das Programm diesen Header mit xlsx als Endung.
response.addHeader("Content-disposition", "attachment;filename=test.xlsx");
Mit diesem Setup erschien in Excel eine Warnung “The file you are to open ‘test.xlsx-1.xls’, is in a different format thant specified by the file extension”.
Das Problem ist das für Excel 2007 ein anderer MIME Type angegeben werden muss.
response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
Mit diesem MIME Type wird die generierte Datei ohne Probleme in Excel geöffnet.
Hier eine Auflistung von allen MIME Typen für die Office 2007 Formate.
| .docm | application/vnd.ms-word.document.macroEnabled.12 |
| .docx | application/vnd.openxmlformats-officedocument.wordprocessingml.document |
| .dotm | application/vnd.ms-word.template.macroEnabled.12 |
| .dotx | application/vnd.openxmlformats-officedocument.wordprocessingml.template |
| .potm | application/vnd.ms-powerpoint.template.macroEnabled.12 |
| .potx | application/vnd.openxmlformats-officedocument.presentationml.template |
| .ppam | application/vnd.ms-powerpoint.addin.macroEnabled.12 |
| .ppsm | application/vnd.ms-powerpoint.slideshow.macroEnabled.12 |
| .ppsx | application/vnd.openxmlformats-officedocument.presentationml.slideshow |
| .pptm | application/vnd.ms-powerpoint.presentation.macroEnabled.12 |
| .pptx | application/vnd.openxmlformats-officedocument.presentationml.presentation |
| .xlam | application/vnd.ms-excel.addin.macroEnabled.12 |
| .xlsb | application/vnd.ms-excel.sheet.binary.macroEnabled.12 |
| .xlsm | application/vnd.ms-excel.sheet.macroEnabled.12 |
| .xlsx | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
| .xltm | application/vnd.ms-excel.template.macroEnabled.12 |
| .xltx | application/vnd.openxmlformats-officedocument.spreadsheetml.template |