Web front end Export CSV file

Source: Internet
Author: User

Objective

The export file is processed in the most ways or on the server side. For example, the way to use response in JSP.

However, there may be times when you want to use the Web frontend to export the content on the page as well. For example, export a table on a page.

This requirement must have an answer, but it will be slightly different for each browser. (mainly the difference between IE and other browsers).

Using the ActiveXObject implementation in IE, using the a tag (or JS) in Firefox and Chrome can be achieved. Here's a look at the implementations in other browsers.

Using the A-label implementation method

Directly on the example:

[HTML]View PlainCopy 
  1. <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">
  2. <html>
  3. <head>
  4. <meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  5. <meta name= "author" content="oscar999">
  6. <title></title>
  7. </head>
  8. <body>
  9. <a id="test" download="downlaod.txt" href= "data:text/txt;charset=utf-8, Download Test Data ">download</a>
  10. </Body>
  11. </html>

Explain:

1. Download set the downloaded file name.

2. href plus data:text/txt;charset=utf-8 respectively set click link is the download file, encoding is utf-8. This comma is followed by the content saved in the file.

Export a CSV file in multiple rows, multiple columns

CSV files can be opened in Excel, and if you export a table, it is much easier to use Excel.

The question is: How to Branch, breakdown?

In theory: the use of columns, number division, branch with \ n.

You can use these methods to find that columns can be separated, but not wrapped. I don't seem to know \ nthe.

The solution is to encode using encodeuricomponent/

[HTML]View PlainCopy 
  1. <head>
  2. <meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
  3. <meta name= "author" content="oscar999">
  4. <title>
  5. </title>
  6. <script>
  7. function Clickdownload (ALink)
  8. {
  9. var str = "Col1,col2,col3\nvalue1,value2,value3";
  10. str = encodeuricomponent (str);
  11. alink.href = "Data:text/csv;charset=utf-8," +STR;
  12. Alink.click ();
  13. }
  14. </Script>
  15. </head>
  16. <body>
  17. <a id="test" onclick="Clickdownload (This)" download="Downlaod.csv" href= "#">download</a>
  18. </Body>
  19. </html>

CSV export with Chinese issues

The above use is UTF-8 encoding, theoretically exporting Chinese should not be a problem.

But export CSV format, use Excel Open will find Chinese is garbled, but it is normal to open with other text program.

The reason is that a BOM header is missing. \ufeff.

Plus, everything's fine.

[HTML]View PlainCopy 
  1. <head>
  2. <meta http-equiv= "content-type" content= "text/html; charset=gb2312 ">
  3. <meta name= "author" content="oscar999">
  4. <title>
  5. </title>
  6. <script>
  7. function Clickdownload (ALink)
  8. {
  9. var str = "Field 1, Field 2, field 3\n value 1, value 2, value 3";
  10. str = encodeuricomponent (str);
  11. alink.href = "Data:text/csv;charset=utf-8,\ufeff" +STR;
  12. Alink.click ();
  13. }
  14. </Script>
  15. </head>
  16. <body>
  17. <a id="test" onclick="Clickdownload (This)" download="Downlaod.csv" href= "#">download</a>
  18. </Body>
  19. </html>

There are two changes here.

1. The charset of the page needs to be set to gb2312

2. Add \ufeff BOM Header

File name for chrome download

Use the Download property of a To specify the file name and suffix of the download. But when Chrome executes, it finds out that Chrome doesn't even bother.

The download name is "Download" or "Download".

Search the Internet, there are said to be chrome bug.

See two articles in StackOverflow:

Http://stackoverflow.com/questions/23962284/download-attribute-on-a-tag-doesnt-work-in-chrome
http://stackoverflow.com/questions/23816005/anchor-tag-download-attribute-not-working-bug-in-chrome-35-0-1916-114

The above two articles can not pay attention to, need to focus on is this problem can be solved, and how to solve it?

Direct Sticker Solution:

[HTML]View PlainCopy  
    1. var blob = new blob ([data], {type: ' Text/csv '});//new
    2. var csvurl = url.createobjecturl (BLOB);
    3. document.getElementById ("MyLink"). href = csvurl;


Use BLOBs and URLs to encapsulate and transform. Problem solving.

Here, in the case of Chinese problems, and the above is the same way of processing:

1. The charset of the page needs to be set to gb2312 (set to UTF-8)

2. Add \ufeff BOM Header

The exact code is similar:

[HTML]View PlainCopy 
      1. data = "\ufeff" +data;
      2. var blob = new blob ([data], {type: ' Text/csv,charset=UTF-8 '});

Web front end Export CSV file

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.