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:
12<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 ">3<meta name= "Author" content= "oscar999" >4<title>5</title>6<script>7 functionclickdownload (ALink)8 {9 varstr = "Col1,col2,col3\nvalue1,value2,value3";Tenstr =encodeuricomponent (str); OneAlink.href = "Data:text/csv;charset=utf-8," +str; A Alink.click (); - } -</script> the -<body> -<a id= "test" onclick= "Clickdownload (This)" download= "Downlaod.csv" href= "#" >download</a> -</body> +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 columnsCSV 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/
function clickdownload (ALink) { var str = "Col1,col2,col3\nvalue1,value2,value3"; = encodeuricomponent (str); = "Data:text/csv;charset=utf-8," +str; Alink.click (); } </script>
CSV export with Chinese issuesThe 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.
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 "> <meta name= "Author" content= "oscar999" > <title> </title> <script> functionClickdownload (aLink) {varstr = "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> <a id= "test" onclick= "Clickdownload (This)" download= "Downlaod.csv" href= "#" >download</a> </body>& Lt;/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 downloadUse 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:
1 var New // New 2 var csvurl = url.createobjecturl (BLOB); 3
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:
data = "\ufeff" +data;
var blob = new BLOB ([data], {type: ' Text/csv,charset=utf-8 '});
This article turns from (HTTP://BLOG.CSDN.NET/OSCAR999/ARTICLE/DETAILS/16342699#T2)
Web-side JS export CSV file (using a tag)