Output: add output_encoding option for accented character support in Excel

See http://stackoverflow.com/q/23388490/145346
This commit is contained in:
Mottie 2014-05-04 17:43:55 -05:00
parent ca5e6beb19
commit 7650508932
2 changed files with 56 additions and 6 deletions

View File

@ -53,6 +53,9 @@
.output-separator-input, .output-replacequotes {
width: 26px;
}
.output-type {
width: 80px;
}
</style>
<!-- Tablesorter: required -->
@ -95,7 +98,12 @@
<script id="js">$(function() {
// set up demo for two table groups
var demos = ['.group1', '.group2'];
var demos = ['.group1', '.group2'],
outputTypes = [
'data:text/csv;charset=utf8,', // utf-8 no BOM
'data:text/csv;charset=utf8,%EF%BB%BF', // utf-8 with BOM
'data:text/csv;charset=windows-1252,' // windows 1252
];;
$.each(demos, function(groupIndex){
var $this = $(demos[groupIndex]);
@ -126,7 +134,12 @@
// callback executed when processing completes
// return true to continue download/output
// return false to stop delivery & do something else with the data
output_callback : function(data) { return true; }
output_callback : function(data) { return true; },
// output data type (with BOM or Windows-1252 is needed for excel)
// NO BOM : 'data:text/csv;charset=utf8,'
// With BOM : 'data:text/csv;charset=utf8,%EF%BB%BF'
// WIN 1252 : 'data:text/csv;charset=windows-1252'
output_encoding : 'data:text/csv;charset=utf8,'
}
});
@ -180,6 +193,7 @@
wo.output_wrapQuotes = $this.find('.output-wrap').is(':checked');
wo.output_headerRows = $this.find('.output-headers').is(':checked');
wo.output_saveFileName = $this.find('.output-filename').val();
wo.output_encoding = outputTypes[ parseInt( $this.find('.output-type').val(), 10) ];
$table.trigger('outputTable');
return false;
});
@ -209,6 +223,7 @@
<h3><a href="#">Notes</a></h3>
<div>
<ul>
<li>In <span class="version">v2.16.4</span>, added the <code>output_encoding</code> option.<br><br></li>
<li>This widget will <strong>only work</strong> in tablesorter version 2.8+ and jQuery version 1.7+.</li>
<li>This widget can output the table data to:
<ul>
@ -562,6 +577,22 @@ line,value1,value2,value3
</td>
</tr>
<tr id="output_encoding">
<td><a href="#" class="permalink">output_encoding</a></td>
<td>{see description}</td>
<td>Default encoding is utf-8 no BOM (<span class="version">2.16.4</span>)
<div class="collapsible">
<br>
With the default settings (utf-8 no BOM), Excel does not properly encode accented characters unless the csv file is imported. Depending on the characters used, there are various methods which will allow proper encoding, but no one method is ideal. So this option can be set to allow the user to try different encodings. Set it as follows:
<pre class="prettyprint lang-js">// output data type (with BOM or Windows-1252 is needed for excel)
// NO BOM : 'data:text/csv;charset=utf8,'
// With BOM : 'data:text/csv;charset=utf8,%EF%BB%BF'
// WIN 1252 : 'data:text/csv;charset=windows-1252,' // ANSI (subset of ISO-8859-1)
output_encoding : 'data:text/csv;charset=utf8,'</pre><span class="label label-info">Note</span> The commas are important!
</div>
</td>
</tr>
</tbody>
</table>
<span class="label label-info">Note</span> If you need to change the carriage return and/or the tab replacement strings, modify them as follows:
@ -636,7 +667,14 @@ $.tablesorter.output.replaceTab = '\\t';</pre>
<li><label title="Remove extra white space from each cell">Trim spaces: <input class="output-trim" type="checkbox" checked /></label></li>
<li><label title="Include HTML from cells in output">Include HTML: <input class="output-html" type="checkbox" /></lable></li>
<li><label title="Wrap all values in quotes">Wrap in Quotes: <input class="output-wrap" type="checkbox" /></label></li>
<li><label title="Choose a download filename">Filename: <input class="output-filename" type="text" size="15" value="mytable.csv"/></label></li>
<li>
<label title="Choose a download filename">Filename: <input class="output-filename" type="text" size="15" value="mytable.csv"/></label>
<select class="output-type">
<option value="0">utf8</option>
<option value="1">utf8-BOM</option>
<option value="2">windows-1252</option>
</select>
</li>
</ul>
</div>
</div>
@ -741,7 +779,14 @@ $.tablesorter.output.replaceTab = '\\t';</pre>
<li><label title="Include HTML from cells in output">Include HTML: <input class="output-html" type="checkbox" /></lable></li>
<li><label title="Wrap all values in quotes">Wrap in Quotes: <input class="output-wrap" type="checkbox" /></label></li>
<li><label title="Include both header rows in output">Include both header rows: <input class="output-headers" type="checkbox" checked /></label></li>
<li><label title="Choose a download filename">Filename: <input class="output-filename" type="text" size="15" value="mytable.csv"/></label></li>
<li>
<label title="Choose a download filename">Filename: <input class="output-filename" type="text" size="15" value="mytable.csv"/></label>
<select class="output-type">
<option value="0">utf8</option>
<option value="1">utf8-BOM</option>
<option value="2">windows-1252</option>
</select>
</li>
</ul>
</div>

View File

@ -206,7 +206,7 @@ output = ts.output = {
// modified from https://github.com/PixelsCommander/Download-File-JS
download : function (wo, data){
var e, link,
processedData = 'data:text/csv;charset=utf8,' + encodeURIComponent(data);
processedData = wo.output_encoding + encodeURIComponent(data);
// iOS devices do not support downloading. We have to inform user about this.
if (/(iP)/g.test(navigator.userAgent)) {
@ -258,7 +258,12 @@ ts.addWidget({
// return false to stop delivery & do something else with the data
output_callback : function(data){ return true; },
// JSON callback executed when a colspan is encountered in the header
output_callbackJSON : function($cell, txt, cellIndex) { return txt + '(' + (cellIndex + col) + ')'; }
output_callbackJSON : function($cell, txt, cellIndex) { return txt + '(' + (cellIndex + col) + ')'; },
// output data type (with BOM or Windows-1252 is needed for excel)
// NO BOM : 'data:text/csv;charset=utf8,'
// With BOM : 'data:text/csv;charset=utf8,%EF%BB%BF'
// WIN 1252 : 'data:text/csv;charset=windows-1252'
output_encoding : 'data:text/csv;charset=utf8,'
},
init: function(table, thisWidget, c) {
output.init(c);