I am exporting a HTML table to and Excel format file and then downloading it as an .xls. This works fine in Firefox, Chrome etc. but not as expected in IE.
Below is the function I am using. The final if statement finds out if the browser is IE or otherwise.
function exportTable(obj) {
var tab_text="<table border='2px'><tr>";
var textRange; var j=0;
tab = obj;
for(j = 0 ; j < tab.rows.length ; j++){
tab_text=tab_text+tab.rows[j].innerHTML+"</tr>";
//tab_text=tab_text+"</tr>";
}
tab_text=tab_text+"</table>";
tab_text= tab_text.replace(/<A[^>]*>|<\/A>/g, "");//remove if you want links in your table
tab_text= tab_text.replace(/<img[^>]*>/gi,""); // remove if you want images in your table
tab_text= tab_text.replace(/<input[^>]*>|<\/input>/gi, ""); // removes input params
var ua = window.navigator.userAgent;
var msie = ua.indexOf("MSIE ");
if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) // If Internet Explorer
{
txtArea1 = window.open();
txtArea1.document.open("txt/html","replace");
txtArea1.document.write(tab_text);
txtArea1.document.close();
txtArea1.focus();
sa=txtArea1.document.execCommand("SaveAs",false,"export.xls");
txtArea1.window.close()
} else { //other browser not tested on IE 11
sa = window.open('data:application/vnd.ms-excel,' + encodeURIComponent(tab_text));
return (sa);
}
}
If it's IE, it creates a new window, writes the HTML output to it and then asks the user where to save the file. When the dialog appears, the user is forced to save the file as .html or .txt.
This is where it gets weird. Although the save as box forces the user to download it as either .html or .txt the file itself is saved as an .xls.
Is it possible to disable this prompt? Or is there another work around for this? I do not want the end user to be confused by this.
Try using a hidden iframe instead of a new window. That solved alot of issues for me. To support IE9, I ended up with this snippet:
if (event.data.browser !== 'IE') {
$.util.open('data:application/vnd.ms-excel,' + event.data.content);
return true;
}
else {
// The iframe already has to be included in the HTML, or you'll get a 'no access error'.
frame.document.open("txt/html","replace");
frame.document.write(event.data.content);
frame.document.close();
frame.focus();
command = frame.document.execCommand("SaveAs", true, "data_table.xls");
return command;
}
The only downside is that its not a 'real' excel .xls file yet until you open it the first time and resave it as a .xls
This technique is just leveraging excels ability to understand basic html markup.
A better, but way more complicated method is to actually construct a base64 encoded 'string' as shown in msdn documentation, but in the end this sufficed for the project.
Or as with all things, there's lotsa libraries that will do this for you.
Related
My appcelerator alloy app has a section that displays a list of protocols used by employees in a listview(There are about 25), when a row is tapped, it should open the corresponding PDF document in the assets/protocol_pdf folder. In iOS 11.2.1, the document viewer opens, but the PDF is not displayed, only the filename followed by PDF document(see image). Everything works just fine in iOS 10 and prior.
I used the code example from http://www.appcelerator.com/blog/2015/08/appcelerator-pdf-viewer-demo/ to open the PDF.
Additionally, using the exact code from the appcelerator documentation(http://docs.appcelerator.com/platform/latest/#!/api/Titanium.UI.iOS.DocumentViewer) in a new test project results in the same problem.
At this point i'm not sure how to solve the issue. I can only guess that something in where the file is saved and how it is accessed is different. Any suggestions are appreciated.
My Code:
function onItemClick(e){
var item = $.listView.sections[e.sectionIndex].items[e.itemIndex];
Ti.API.info('Opening ' + item.properties.file);
if(OS_IOS){
openResourcePDF(item.properties.file);
} else{
Alloy.Globals.Navigator.open("androidPDF", {file: item.properties.file});
}
}
function openResourcePDF(fileName){
var appFile;
appFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, fileName);
var appfilepath = appFile.nativePath;
viewPDF(appfilepath);
}
function viewPDF(appfilepath){
docViewer = Ti.UI.iOS.createDocumentViewer({url:appfilepath});
docViewer.show();
}
Image showing how viewer brings it up:
I guess you just want to use that on iOS. Try this:
function openResourcePDF(fileName) {
var appFile;
appFile = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, fileName);
var appfilepath = appFile.nativePath;
viewFile(appfilepath);
}
I use this script in a pdf form. The total field is blank (no zeros) until numbers are input then it adds fields to the total. It works in Adobe Reader and it works using if you open in web browser IE. It does something weird with calculations when opened to fill in using Chrome. This is the script:
var v = event.value;
if (v == 0)
{
event.target.display = display.hidden;
}
else
{
event.target.display = display.visible;
}
Can someone help? Thank you RB
I have an issue I cannot find a solution for despite scouring the internet for obvious clues.
I have a very basic PhoneGap app that uses the contacts plugin.
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
// get all contacts
var options = new ContactFindOptions();
options.filter = "";
options.multiple = true;
var filter = ["name", "phoneNumbers", "photos"];
navigator.contacts.find(filter, onContactSuccess, onError, options);
}
function onContactSuccess(contacts) {
var element = document.getElementById('contacts');
for (var i = 0; i < contacts.length; i++) {
// the following check is incomplete for brevity
if (contacts[i].photos) {
// this produces a valid file location
element.innerHTML += contacts[i].photo[0].value;
// this renders an image not found square
element.innerHTML += "<img src='" + contacts[i].photo[0].value + "'>";
}
}
}
The problem that eludes me is the insertion of the image. PhoneGap serve produces the following error
[phonegap] 404
/var/mobile/Containers/Data/Application/C7B8E714-47EE-42AD-9C58-6B86EE068110/tmp/photo_cXxyI
and yet using iExplore I can see the photo_cXxyI in the /tmp folder.
I'm following the post from Raymond Camden that has a demo of the contacts plugin but my version refuses to show the images.
I'm running the app on the iOS PhoneGap app and there is data being returned (first, last, phone numbers, images array).
Any ideas on where I'm going wrong? I did also notice that the temp image files accumulate in the /tmp folder. They don't ever seem to be deleted. Is this a clue or an 'undocumented' feature?
I am developing some enhancements for a web page and I need some assistance. I have added a button that when clicked will take the html from a jqgrid and export it to Excel. I thought I had it all working and then I did the cross browser test and Internet Explorer does not like the following method:
function exportGrid()
{
var grid = "#diamondpassConferencePreregistrantGrid";
var mya=new Array();
mya=$(grid).getDataIDs(); // Get All IDs
var data=$(grid).getRowData(mya[0]); // Get First row to get the labels
var colNames=new Array();
var ii=0;
for (var i in data){colNames[ii++]=i;} // capture col names
var html="";
var columnNames = $(grid).jqGrid('getGridParam','colNames');
for(i=0;i<columnNames.length-1;i++)
{
html = html + columnNames[i+1]+"\t";
}
html=html+"\n";
for(i=0;i<mya.length;i++)
{
data=$(grid).getRowData(mya[i]); // get each row
for(j=0;j<colNames.length-1;j++)
{
html=html+data[colNames[j+1]]+"\t"; // output each column as tab delimited
}
html=html+"\n"; // output each row with end of line
}
html=html+"\n"; // end of line at the end
alert(html);
window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
}
This all seems to work fine for me in Firefox and Chrome. The problem is that when I test in IE, the line:
window.open('data:application/vnd.ms-excel,' + encodeURIComponent(html));
opens up a new tab telling me the webpage cannot be displayed.
I got some info from the following webpage:
http://www.jquerybyexample.net/2012/10/export-table-data-to-excel-using-jquery.html
that basically tells me that line will not work in IE, that I need to set the response header as so:
Response.AddHeader("Content-Disposition", "attachment;filename=download.xls");
My problem is that I do not know what this entails. I tried adding this line and as I would have expected, I get "Response is undefined" or something of this nature.
I was just curious if anyone had an idea as to the best way to move forward from this point. I know how to detect the browser and I can always use different code to accomplish my Excel export for Internet Explorer but my problem is that I do not know the correct way to do the equivalent line in IE.
Any help would be greatly appreciated.
Thank you!
-Dave
I have site which was mainly designed for full flash with SWFAddress, therefore all links end up with hash "#" sign (eg.: www.site.com/#/folder/image.jpg).
I want to switch flash to full HTML and don't make any of the links dead (as I left a lot of links around the web), so I created htaccess file to control the incomming requests.
The problem was with the hash "#" sign because it couldn't be handled by htaccess file. I made a piece of javascript code on top of my first index.php.
So, before I load any controller, the index.php file starts with javascript to remove hash sign and redirect back to the site without "#" i n url :
<script type="text/javascript">
var link = location.href;
m = removeHash(link);
if (link != m) {
document.location.href = m;
}
function removeHash(l) {
var unhashedUrl = l;
if (l.indexOf("#") > -1) {
var url_pieces = l.split('/');
var request = url_pieces[url_pieces.length-1];
if(request.indexOf("#") == -1 && request.toLowerCase().indexOf(".jpg") > -1){
unhashedUrl = l.replace('#', "gallery");
}else{
unhashedUrl = l.replace('#/', "");
}
}
return unhashedUrl;
}
</script>
Then all php stuff begins...
Chrome, firefox, Opera, Safari, mobile browsers... They all works flawlessly. But again the IE (tested IE8 and IE9) end up crazy. It looks like IE won't fire any JS later on. Of course when I remove JS from index controller IE works good too.
Any advice how should I achieve this?