Displaytag is exporting CSV into a .xls file

2019-08-30 03:51发布

I export an Excel document with DisplayTag. It saves as a .xls file to my Desktop. When I open it with Excel, this error displays:

"The file you are trying to open, 'filename.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file."

If I click "Yes", it displays just fine in Excel. If I open this with Textpad, I see that it's a binary file, but it appears to just be "." delimited strings.

Any ideas what I might have done wrong, or how next to debug this?

edit::

If I change the extension to .txt, it's not a binary file; it's simply a list of delimited strings.

3条回答
等我变得足够好
2楼-- · 2019-08-30 03:56

Are you on Excel 2007? Seems to be a known issue:

In some cases after upgrading to Excel 2007, you may get the following error: The file you are trying to open .xlsx is in a different format than specified by the file extension. verify the file is not corrupted and is from trusted source before opening the file. Do you want to open the file now?

This errors particularly occurs when you’re trying to open an XLS file (Excel 2000-2003) with Excel 2007. In most cases, the problem can be solved by defining MIME types and associating them with extensions.

...

However, if you don’t want to look for a solution, but just want to solve the problem, insert this key in your registry to suppress the notification:

[HKEY_CURRENT_USER\Software\Microsoft\Office\12.0\Excel\Security] “ExtensionHardening”=dword:00000000

You can accomplish the above by doing the following:

  1. Open your Registry (Start -> Run -> regedit.exe)
  2. Navigate to HKEY_CURRENT_USER\SOFTWARE\MICROSOFT\OFFICE\12.0\EXCEL\SECURITY
  3. Right click in the right window and choose New -> DWORD
  4. Type “ExtensionHardening” as the name (without the quotes)
  5. Verify that the data has the value “0
查看更多
Root(大扎)
3楼-- · 2019-08-30 03:59

The default export class for Excel was set to the CSV class, but giving it the .xls extension. Changing displaytag.properties was the key here, something like:

export.types=csv excel xml rtf pdf 
export.excel=true
export.xml=true
export.csv=true
export.rtf=false
export.pdf=false
export.excel.class=org.displaytag.export.excel.DefaultHssfExportView
export.pdf.class=org.displaytag.export.DefaultPdfExportView
export.rtf.class=org.displaytag.export.DefaultRtfExportView
export.excel.filename=data.xls
export.pdf.filename=data.pdf
export.xml.filename=data.xml
export.csv.filename=data.csv
export.rtf.filename=data.rtf
查看更多
趁早两清
4楼-- · 2019-08-30 04:13

You need to add the below lines in your properties file:

export.excel.class=org.displaytag.export.excel.DefaultHssfExportView
export.pdf.class=org.displaytag.export.DefaultPdfExportView

But this outputs the current page record only and not the complete list.

查看更多
登录 后发表回答