I have an ASP.Net MVC site that generates a Microsoft Excel 2003 XML formatted spreadsheet. The spreadsheet looks good, the controller and views both work, but the file won't open in Excel. It opens in the browser because it is an XML document.
I tried changing the ContentType to be the Excel XLS format (application/excel) and that made Excel open the file but it gives a warning message that the file is an XML document, not an XLS document.
How do you make an Excel XML document open in Excel from a web site?
<%# Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %><%
this.Context.Response.Clear();
this.Context.Response.AddHeader("content-disposition", "attachment;filename=State_Report_" + this.ViewData["State"] + ".xml");
this.Context.Response.Charset = "";
this.Context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
this.Context.Response.ContentType = "application/excel";
%><?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
Try with
Response.ContentType = "application/vnd.ms-excel"
And keep the .XML extension.
Your problem may be in using the file extension .xml, which you're adding in your header.
My code that does this same thing does not have a file extension at all.
You might try dropping that .xml extension, or changing it to .xls or .csv.
Related
I am a grails beginner, and finding a way to view the doc/pdf files in the view. I tried embedding the file but got no output for it. How can i view the file?
Assuming that you don't just want to link to some other file on your server (if you do, Rahul's answer is perfectly fine) then you may want to consider something like this in a controller:
def retrieveFile() {
File fileToReturn = new File("${params.filePath}")
String filename = "whatever your filename is"
byteOutput = fileToReturn.readBytes()
response.setHeader("Content-disposition", "attachment; filename=\"${filename}\"");
outputStream << byteOutput
}
Then link to that (g.createLink or whatever) with the appropriate file path.
Add <embed> or <object>tag in your gsp file
<embed> tag Example:
<embed src="http://example.com/your-file.pdf" width="800" height="500" type='application/pdf'>
<object> tag Example:
<object width="800" height="500" type="application/pdf" data="http://example.com/your-file.pdf">
<p>Insert your error message here, if the PDF cannot be displayed.</p>
</object>
Above code tested and working with chrome and Firefox.
Basically there are many solutions, where you can you third party plugins and or using <iframe>
Thanks, Hope it helps you
This is what I need to implement. I need to create a batch which:
1. READ: will read mutiple xml files from a folder
2. PROCESS: extract the values of some tags from one of these xmls (as explained below)and save the extracted data in a DB.
3. WRITE: move the processed xml file as it is to another directory.
There are no repeating tags in the xml.
For example if this is my XML:
<?xml version="1.0" encoding="UTF-8"?>
<report>
<info>
<ssn>5214365214356</ssn>
<name>abc</name>
<age>12</age>
<gender>male</gender>
</info>
<address>
<street>abc</street>
<city>atrdtysaf</city>
<state>abcsvc</state>
<country>USA</country>
</address>
<healthinfo>
<smoking>no</smoking>
<drinking>no</drinking>
</healthinfo>
</report>
I want to extract the values of "ssn, gender and country tags only". Please note that the actual xml would be relatively huge. I am supposed to use StaxEvenItemReader provided by spring batch.
I have a xml spreadsheetml document. This is used to create an excel document through my silverlight application. When the file created through this template is saved as *.xlsx Excel 2010 throws an error
"Excel cannot open file '*.xlsx' because the file format or extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
However, if the file is saved as .xls or .xml then there is no problem and it can be opened through Excel 2010.
<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<DocumentProperties xmlns="urn:schemas-microsoft-com:office:office">
<Author></Author>
<LastAuthor></LastAuthor>
<Created>2010-10-25T14:17:17Z</Created>
<LastSaved>2010-10-25T14:17:17Z</LastSaved>
<Company></Company>
<Version></Version>
</DocumentProperties>
<ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel">
<WindowHeight>12015</WindowHeight>
<WindowWidth>20055</WindowWidth>
<WindowTopX>480</WindowTopX>
<WindowTopY>150</WindowTopY>
<ProtectStructure>False</ProtectStructure>
<ProtectWindows>False</ProtectWindows>
</ExcelWorkbook>
<Styles>
<Style ss:ID="Default" ss:Name="Normal">
<Alignment ss:Horizontal="Left" ss:Vertical="Center"/>
<Borders/>
<Font ss:Color="#000000"/>
<Interior ss:Color="#FFFFFF" ss:Pattern="Solid"/>
<NumberFormat/>
<Protection/>
</Style>
<Style ss:ID="s21">
<NumberFormat ss:Format="Short Date"/>
</Style>
<Style ss:ID="s22">
<NumberFormat ss:Format="0"/>
</Style>
</Styles>
<Worksheet ss:Name="Sheet1">
<Table>
***
</Table>
</Worksheet>
</Workbook>
How can this template be upgraded to match the Excel 2010 format?
This warning is a result of Extension Hardening in Excel 2007+.
How can this template be upgraded to match the Excel 2010 format?
It can't be upgraded without using some external application that understands both formats. The SpreadsheetXML and XLSX formats are very different.
When using the example app from Ryan Bates' Railscasts Episode #362 about exporting to Excel (https://github.com/railscasts/362-exporting-csv-and-excel), I've noticed that Excel 2010 (which is on Windows) gives me a warning message when opening up the .xls file I've downloaded using the "Download as Excel" link.
The warning reads:
"The file you are trying to open ... 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. Do you want to open the file now?"
I can open the file just fine when I click 'Yes.' And I don't even get the warning message when using Excel 2011 (on the Mac). But I'd like to be able to provide an Excel file that won't prompt that warning when a user downloads such a file from my site.
Note: I've also tried replacing all the references in the app from .xls to .xlsx, but then Excel can't open the file at all. It complains: "Excel cannot open this file. The file format or file extension is not valid. Verify that the file has not been corrupted and that the file extension matches the format of the file."
I am aware of gems such as AXLSX (https://github.com/randym/axlsx), but was hoping I could just use the Railscasts method and make a quick fix to eliminate the warning message from Excel 2010.
Thank you so much for your help!
The xls file that is generated by the Railscasts application is actually an XML file in the old Excel 2003 XML format.
Recent versions of Excel have a feature called Extension Hardening that generate this warning when the file format doesn't match the file extension:
The alert is a new security feature in Excel 2007 called Extension Hardening, which ensures that the file content being opened matches the extension type specified in the shell command that is attempting to open the file. Because the MIME types listed above are associated with the .XLS extension, the file must be in XLS (BIFF8) file format to open without this warning prompt.
In order to avoid this warning you will have to generate an Excel file in a format that matches the file extension. Editing the registry as suggested as a workaround in the above link probably isn't workable in practice. Changing the extension to xml might also work.
As alternatives writeexcel for xls, write_xlsx for xlsx and AXLSX (that you mention above) are good options.
In fact, the code generated:
<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:o="urn:schemas-microsoft-com:office:office"
xmlns:x="urn:schemas-microsoft-com:office:excel"
xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
xmlns:html="http://www.w3.org/TR/REC-html40">
<Worksheet ss:Name="Sheet1">
<Table>
<Row>
<Cell><Data ss:Type="String">ID</Data></Cell>
<Cell><Data ss:Type="String">Name</Data></Cell>
<Cell><Data ss:Type="String">Release Date</Data></Cell>
<Cell><Data ss:Type="String">Price</Data></Cell>
</Row>
<% #products.each do |product| %>
<Row>
<Cell><Data ss:Type="Number"><%= product.id %></Data></Cell>
<Cell><Data ss:Type="String"><%= product.name %></Data></Cell>
<Cell><Data ss:Type="String"><%= product.released_on %></Data></Cell>
<Cell><Data ss:Type="Number"><%= product.price %></Data></Cell>
</Row>
<% end %>
</Table>
</Worksheet>
</Workbook>
is XML (XLS is a binary format, XLST a zipped format)
Renaming the file to .xml should work
I'm studying Apple's LazyTableImages sample code. I'd like to understand how the app is pulling data from the RSS feed included in the app:
http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/xml
How are the contents at the above url parsed? Viewing the page source reveals HTML with no apparent xml section. While looking through the sample parsing code I found a few symbols like im:name. However these symbols are not in the contents of the above url.
I tried to host the contents of the above url locally (w/ limit=1). However pointing the sample code to #"~/Desktop/a.xml" causes the application to throw the error unsupported url.
More info: While reading http://en.wikipedia.org/wiki/Rss I came across what I expected to see at phobos link above. Something like this:
<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title>RSS Title</title>
<description>This is an example of an RSS feed</description>
<link>http://www.someexamplerssdomain.com/main.html</link>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
<item>
<title>Example entry</title>
<description>an interesting description</description>
<link>http://www.wikipedia.org/</link>
<guid>unique string per item</guid>
<pubDate>Mon, 06 Sep 2009 16:45:00 +0000 </pubDate>
</item>
</channel>
</rss>
Is there an equivalent "human-readable" xml document corresponding to the above phobos link somewhere?
You're right, the feed you're looking at technically isn't an RSS Feed. It's an Atom 1.0 Feed, but both are popular XML-based feed formats.
If you view the source of the feed you will see the XML elements you're looking for, like:
<entry>
<updated>2011-12-09T16:15:32-07:00</updated>
<id>http://itunes.apple.com/us/app/tetris/id479943969?mt=8&uo=2</id>
<title>TETRISĀ® - Electronic Arts</title>
<summary>Long summary here</summary>
<im:name>TETRISĀ®</im:name>
...
</entry>
Some browser versions parse RSS/Atom feeds into user-friendly HTML pages and present them instead of the actual feed, it sounds like that's the type of HTML page you're viewing.
On a OS X, you could use a command like Curl to download the feed in a Terminal:
curl -o feed.xml http://phobos.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/toppaidapplications/limit=75/xml