Source: http://bbs.hcharts.cn/thread-99-1-1.htmlExportExcelPlugins--Export-csv
First, plug-in information
- Plug-in Name: Export-csv (export execl file)
- Plugin Address: Https://rawgithub.com/highslide-software/export-csv/master/export-csv.js
- Plug-in use: on the page to introduce Export-csv.js, see the online demo platform "Export Excel table"
Second, the principle of analysis
By analyzing the source code, the principle is to traverse the Highcharts data column, and to construct a comma-separated string, and submit the download to the specified export server to save as a. csv file.
1. CSV
CSV is a comma-separated value file format, usually a plain text file. If you have Microsoft Excel installed on your machine, the. csv file is opened by Excel by default.
2. Custom Export Server
Export-csv export server is relatively simple, the implementation code is as follows
- <?php
- $csv = $_post[' csv '];
- if ($csv) {
- Header (' content-type:text/csv ');
- Header (' content-disposition:attachment;filename=chart.csv ');
- Echo $csv;
- }
- ?>
Copy Code
Custom Export Events
- $ ("#download"). Click (function () {
- Highcharts.post (' export-cvs.php ', {//where export-cvs.php is the code above
- Csv:chart.getCSV ()
- });
- })
Copy Code
3, the Chinese network provides the export server
Address: http://export.hcharts.cn/cvs.php
Instance:
- Highcharts.post (' http://export.hcharts.cn/cvs.php ', {
- Csv:chart.getCSV ()
- });
Copy Code supplement: Office Excel opens with garbled problem resolution
1, garbled analysis
I use Notepad or wps Excel to open the exported CVS file, Chinese is not garbled, and when the Office of Excel opened, Chinese is garbled. Later on the web, the reason is that Office Excel by default is open in ANSI form, that is, the corresponding Chinese GBK encoding, and the default export is the UTF-8 format, the solution is the encoding conversion.
2. Solutions
Convert the obtained CVS code to GBK encoding, and the modified export server code is as follows:
PHP Version:
- <?php
- /**
- * disclaimer:don ' t use [url=http://www.highcharts.com/studies/csv-export/csv.php]www.highcharts.com/studies/ Csv-export/csv.php[/url] In
- * production! This file is removed at any time.
- * Highcharts Chinese language network to provide Chinese garbled solution
- */
- $csv = $_post[' csv '];
- $csv = Iconv ("Utf-8", "GBK", $csv);//Convert to GBK code
- if ($csv) {
- Header (' CONTENT-TYPE:TEXT/CSV;CHARSET=GBK ');
- Header (' content-disposition:attachment;filename=chart.csv ');
- Echo $csv;
- }
- ?>
Copy Code
Java Edition:
- <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
- <%
- string csv = new String (Request.getparameter ("CSV"). GetBytes ("Utf-8"), "GBK");
- if (csv!=null &&! "". Equals (CSV)) {
- Response.setheader ("Content-type", "text/csv");
- Response.setheader ("Content-disposition", "attachment;filename=chart.csv");
- Response.getwriter (). print (CSV);
- }
- %>
Copy Code
In addition, transcoding from the commit CVS code can also be implemented.
Add: The Java version of the export server code is as follows:
- <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
- <%
- String csv = request.getparameter ("CSV");
- if (! "". Equals (CSV)) {
- Response.setheader ("Content-type", "text/csv");
- Response.setheader ("Content-disposition", "attachment;filename=chart.csv");
- Response.getwriter (). print (CSV);
- }
- %>
Copy Code |
Export Excel plugin--export-csv---20150610