【1】Web端文字、表格、图片导出

 

在做项目的过程中,往往客户需要系统(无论是Browser还是Client)可以将数据以所需求的形式下载下来。今天呢,我就前端方面,介绍如何将前端的信息以各种格式导出。

1、文字导出

这里介绍到一个工具,是Jquery的插件,名字叫wordexport.它将所选择的区域以word文档的形式下载下来,但只包含文本信息,其他如图片等无效,且对于表格,只会将表格里的信息一行叠在一起。

1.1、插件引用

1 <script type="text/javascript" src="js/FileSaver.js"></script>
2 <script type="text/javascript" src="js/jquery.wordexport.js"></script>

 

1.2、使用

1 $(function() { 
2   $(".word-export").click(function() {
3     $("#pagecontent").wordExport("fileName"); //fileName为导出的word文件的命名
4   });
5 });

 

 

 

1.3资源下载

http://www.jq22.com/demo/exportWord20161214/js/FileSaver.js 
http://www.jq22.com/demo/exportWord20161214/js/jquery.wordexport.js

 

2、表格导出

一开始我同样介绍一个Jquery插件,名字叫做Tableexport。它可以将HTML中Table标签下的表格数据以多种格式下载到本地。

2.1、插件引用

 

1 <script type="text/javascript" src="libs/FileSaver/FileSaver.min.js"></script>
2 <script type="text/javascript" src="libs/js-xlsx/xlsx.core.min.js"></script>
3 <script type="text/javascript" src="libs/jsPDF/jspdf.min.js"></script>
4 <script type="text/javascript" src="libs/jsPDF-AutoTable/jspdf.plugin.autotable.js"></script>
5 <script type="text/javascript" src="libs/html2canvas/html2canvas.min.js"></script>
6 <script type="text/javascript" src="tableExport.min.js"></script>

 

2.2、使用

 1 // CSV format
 2 $('#tableID').tableExport({type:'csv'});
 3 
 4 // Excel 2000 html format
 5 $('#tableID').tableExport({type:'excel'});
 6 
 7 // XML Spreadsheet 2003 file format with multiple worksheet support
 8 $('table').tableExport({type:'excel',
 9                         excelFileFormat:'xmlss',
10                         worksheetName: ['Table 1','Table 2', 'Table 3']});
11 
12 // PDF export using jsPDF only
13 $('#tableID').tableExport({type:'pdf',
14                            jspdf: {orientation: 'p',
15                                    margins: {left:20, top:10},
16                                    autotable: false}
17                           });
18 
19 // PDF format using jsPDF and jsPDF Autotable 
20 $('#tableID').tableExport({type:'pdf',
21                            jspdf: {orientation: 'l',
22                                    format: 'a3',
23                                    margins: {left:10, right:10, top:20, bottom:20},
24                                    autotable: {styles: {fillColor: 'inherit', 
25                                                         textColor: 'inherit'},
26                                                tableWidth: 'auto'}
27                                   }
28                           });
29 
30 // PDF format with callback example
31 function DoCellData(cell, row, col, data) {}
32 function DoBeforeAutotable(table, headers, rows, AutotableSettings) {}
33 $('table').tableExport({fileName: sFileName,
34                         type: 'pdf',
35                         jspdf: {format: 'bestfit',
36                                 margins: {left:20, right:10, top:20, bottom:20},
37                                 autotable: {styles: {overflow: 'linebreak'},
38                                             tableWidth: 'wrap',
39                                             tableExport: {onBeforeAutotable: DoBeforeAutotable,
40                                                           onCellData: DoCellData}}}
41                        });

 

2.3、资源下载

https://github.com/hhurz/tableExport.jquery.plugin

 

除去这个插件,我自己介绍一种将Jquery-EasyUI中DataGrid中数据以Excel形式下载到本地的方法。实例代码如下:

 1 $("#SaveBtn").click(function() {  
 2             var data = JSON.stringify($('#table').datagrid('getData').rows);  
 3             alert(data);  
 4             if (data == '')  
 5                 return;  
 6             JSONToCSVConvertor(data, "Download", true);  
 7         });  
 8         
 9         function JSONToCSVConvertor(JSONData, ReportTitle, ShowLabel) {  
10             //If JSONData is not an object then JSON.parse will parse the JSON string in an Object  
11             var arrData = typeof JSONData != 'object' ? JSON.parse(JSONData)  
12                     : JSONData;  
13   
14             var CSV = '';  
15             //Set Report title in first row or line  
16   
17 //          CSV += ReportTitle + '\r\n\n';  
18   
19             //This condition will generate the Label/Header  
20             if (ShowLabel) {  
21                 var row = "";  
22   
23                 //This loop will extract the label from 1st index of on array  
24                 for ( var index in arrData[0]) {  
25   
26                     //Now convert each value to string and comma-seprated  
27                     row += index + ',';  
28                 }  
29   
30                 row = row.slice(0, -1);  
31   
32                 //append Label row with line break  
33                 CSV += row + '\r\n';  
34             }  
35   
36             //1st loop is to extract each row  
37             for (var i = 0; i < arrData.length; i++) {  
38                 var row = "";  
39   
40                 //2nd loop will extract each column and convert it in string comma-seprated  
41                 for ( var index in arrData[i]) {  
42                     row += '"' + arrData[i][index] + '",';  
43                 }  
44   
45                 row.slice(0, row.length - 1);  
46   
47                 //add a line break after each row  
48                 CSV += row + '\r\n';  
49             }  
50   
51             if (CSV == '') {  
52                 alert("Invalid data");  
53                 return;  
54             }  
55   
56             //Generate a file name  
57             var fileName = "MyReport_";  
58             //this will remove the blank-spaces from the title and replace it with an underscore  
59             fileName += ReportTitle.replace(/ /g, "_");  
60   
61             //Initialize file format you want csv or xls  
62             var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);  
63   
64             // Now the little tricky part.  
65             // you can use either>> window.open(uri);  
66             // but this will not work in some browsers  
67             // or you will not get the correct file extension      
68   
69             //this trick will generate a temp <a /> tag  
70             var link = document.createElement("a");  
71             link.href = uri;  
72   
73             //set the visibility hidden so it will not effect on your web-layout  
74             link.style = "visibility:hidden";  
75             link.download = fileName + ".csv";  
76   
77             //this part will append the anchor tag and remove it after automatic click  
78             document.body.appendChild(link);  
79             link.click();  
80             document.body.removeChild(link);  
81         }  

 

  注释:这里主要用到的datagrid的data属性获取表的字符串,在将字符串编辑后赋予到HTML的标签<a>,以CSV的格式下载到本地。

3、图片导出

在Web前端中,可以展现图片形式的有img、canvas与svg三种标签形式。一般情况下,都可以将其转换<a>标签的形式,以jpg格式下载下来。

4、甭管是啥,都以图片导出

这里介绍的是一种插件叫做html2canvas。

4.1、插件引用

 

1 <script type="text/javascript" src="../../mui.js" ></script>
2 <script type="text/javascript" src="../../html2canvas.js" ></script>

 

4.2、使用

 1 html2canvas(document.getElementsByClassName("pvtRendererArea"), {
 2           allowTaint: true,
 3           taintTest: false,
 4           onrendered: function(canvas) {
 5                             canvas.id = "mycanvas";
 6                             var image = new Image();
 7                             ////指定格式PNG
 8                             //image.src = canvas.toDataURL("image/jpg");
 9 
10                             //console.log("ASd");
11                             //document.body.appendChild(canvas);
12                             //生成base64图片数据
13                             image.src = canvas.toDataURL("image/jpg");
14                             //window.open(image);
15     
16                             var link = document.createElement("a");  
17                             link.href = image.src;  
18   
19                             //set the visibility hidden so it will not effect on your web-layout  
20                             link.style = "visibility:hidden";  
21                             link.download = "pivotTable" + ".jpg";  
22   
23                             //this part will append the anchor tag and remove it after automatic click  
24                             document.body.appendChild(link);  
25                             link.click();  
26                             document.body.removeChild(link);  
27                                
28                         }
29 });

 

4.3、资源下载

http://html2canvas.hertzen.com/

 

以上就是Web端数据导出的功能介绍,后续继续补充。。。

 

posted @ 2018-01-29 13:34  闻名棒冰  阅读(540)  评论(0编辑  收藏  举报