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
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en">
- <html>
- <head>
- <meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <meta name= "author" content="oscar999">
- <title></title>
- </head>
- <body>
- <a id="test" download="downlaod.txt" href= "data:text/txt;charset=utf-8, Download Test Data ">download</a>
- </Body>
- </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
- <head>
- <meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <meta name= "author" content="oscar999">
- <title>
- </title>
- <script>
- function Clickdownload (ALink)
- {
- var str = "Col1,col2,col3\nvalue1,value2,value3";
- str = encodeuricomponent (str);
- alink.href = "Data:text/csv;charset=utf-8," +STR;
- Alink.click ();
- }
- </Script>
- </head>
- <body>
- <a id="test" onclick="Clickdownload (This)" download="Downlaod.csv" href= "#">download</a>
- </Body>
- </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
- <head>
- <meta http-equiv= "content-type" content= "text/html; charset=gb2312 ">
- <meta name= "author" content="oscar999">
- <title>
- </title>
- <script>
- function Clickdownload (ALink)
- {
- var str = "Field 1, Field 2, field 3\n value 1, value 2, value 3";
- str = encodeuricomponent (str);
- alink.href = "Data:text/csv;charset=utf-8,\ufeff" +STR;
- Alink.click ();
- }
- </Script>
- </head>
- <body>
- <a id="test" onclick="Clickdownload (This)" download="Downlaod.csv" href= "#">download</a>
- </Body>
- </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
- var blob = new blob ([data], {type: ' Text/csv '});//new
- var csvurl = url.createobjecturl (BLOB);
- 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
- data = "\ufeff" +data;
- var blob = new blob ([data], {type: ' Text/csv,charset=UTF-8 '});
Web front end Export CSV file