Export Excel plugin--export-csv---20150610

Source: Internet
Author: User

Label:

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

    1. <?php
    2. $csv = $_post[' csv '];
    3. if ($csv) {
    4. Header (' content-type:text/csv ');
    5. Header (' content-disposition:attachment;filename=chart.csv ');
    6. Echo $csv;
    7. }
    8. ?>
Copy Code


Custom Export Events

    1. $ ("#download"). Click (function () {
    2. Highcharts.post (' export-cvs.php ', {//where export-cvs.php is the code above
    3. Csv:chart.getCSV ()
    4. });
    5. })
Copy Code


3, the Chinese network provides the export server
Address: http://export.hcharts.cn/cvs.php

Instance:

    1. Highcharts.post (' http://export.hcharts.cn/cvs.php ', {
    2. Csv:chart.getCSV ()
    3. });
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:
    1. <?php
    2. /**
    3. * disclaimer:don ' t use [url=http://www.highcharts.com/studies/csv-export/csv.php]www.highcharts.com/studies/ Csv-export/csv.php[/url] In
    4. * production! This file is removed at any time.
    5. * Highcharts Chinese language network to provide Chinese garbled solution
    6. */
    7. $csv = $_post[' csv '];
    8. $csv = Iconv ("Utf-8", "GBK", $csv);//Convert to GBK code
    9. if ($csv) {
    10. Header (' CONTENT-TYPE:TEXT/CSV;CHARSET=GBK ');
    11. Header (' content-disposition:attachment;filename=chart.csv ');
    12. Echo $csv;
    13. }
    14. ?>
Copy Code

Java Edition:
    1. <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
    2. <%
    3. string csv = new String (Request.getparameter ("CSV"). GetBytes ("Utf-8"), "GBK");
    4. if (csv!=null &&! "". Equals (CSV)) {
    5. Response.setheader ("Content-type", "text/csv");
    6. Response.setheader ("Content-disposition", "attachment;filename=chart.csv");
    7. Response.getwriter (). print (CSV);
    8. }
    9. %>
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:
  1. <%@ page language= "java" import= "java.util.*" pageencoding= "UTF-8"%>
  2. <%
  3. String csv = request.getparameter ("CSV");
  4. if (! "". Equals (CSV)) {
  5. Response.setheader ("Content-type", "text/csv");
  6. Response.setheader ("Content-disposition", "attachment;filename=chart.csv");
  7. Response.getwriter (). print (CSV);
  8. }
  9. %>
Copy Code

Export Excel plugin--export-csv---20150610

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: