Export.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // JavaScript Document
  2. /**
  3. * allows for downloading of grid data (store) directly into excel
  4. * Method: extracts data of gridPanel store, uses columnModel to construct XML excel document,
  5. * converts to Base64, then loads everything into a data URL link.
  6. *
  7. * @author Animal <extjs support team>
  8. *
  9. */
  10. /**
  11. * base64 encode / decode
  12. *
  13. * @location http://www.webtoolkit.info/
  14. *
  15. */
  16. var Base64 = (function() {
  17. // Private property
  18. var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  19. // Private method for UTF-8 encoding
  20. function utf8Encode(string) {
  21. string = string.replace(/\r\n/g,"\n");
  22. var utftext = "";
  23. for (var n = 0; n < string.length; n++) {
  24. var c = string.charCodeAt(n);
  25. if (c < 128) {
  26. utftext += String.fromCharCode(c);
  27. }
  28. else if((c > 127) && (c < 2048)) {
  29. utftext += String.fromCharCode((c >> 6) | 192);
  30. utftext += String.fromCharCode((c & 63) | 128);
  31. }
  32. else {
  33. utftext += String.fromCharCode((c >> 12) | 224);
  34. utftext += String.fromCharCode(((c >> 6) & 63) | 128);
  35. utftext += String.fromCharCode((c & 63) | 128);
  36. }
  37. }
  38. return utftext;
  39. }
  40. // Public method for encoding
  41. return {
  42. encode : (typeof btoa == 'function') ? function(input) {
  43. return btoa(utf8Encode(input));
  44. } : function (input) {
  45. var output = "";
  46. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  47. var i = 0;
  48. input = utf8Encode(input);
  49. while (i < input.length) {
  50. chr1 = input.charCodeAt(i++);
  51. chr2 = input.charCodeAt(i++);
  52. chr3 = input.charCodeAt(i++);
  53. enc1 = chr1 >> 2;
  54. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  55. enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
  56. enc4 = chr3 & 63;
  57. if (isNaN(chr2)) {
  58. enc3 = enc4 = 64;
  59. } else if (isNaN(chr3)) {
  60. enc4 = 64;
  61. }
  62. output = output +
  63. keyStr.charAt(enc1) + keyStr.charAt(enc2) +
  64. keyStr.charAt(enc3) + keyStr.charAt(enc4);
  65. }
  66. return output;
  67. }
  68. };
  69. })();
  70. Ext.override(Ext.grid.GridPanel, {
  71. getExcelXml: function(title, includeHidden) {
  72. var worksheet = this.createWorksheet(title, includeHidden);
  73. return '<?xml version="1.0" encoding="utf-8"?>' +
  74. '<ss:Workbook xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:o="urn:schemas-microsoft-com:office:office">' +
  75. '<o:DocumentProperties><o:Title>' + '123' + '</o:Title></o:DocumentProperties>' +
  76. '<ss:ExcelWorkbook>' +
  77. '<ss:WindowHeight>' + worksheet.height + '</ss:WindowHeight>' +
  78. '<ss:WindowWidth>' + worksheet.width + '</ss:WindowWidth>' +
  79. '<ss:ProtectStructure>False</ss:ProtectStructure>' +
  80. '<ss:ProtectWindows>False</ss:ProtectWindows>' +
  81. '</ss:ExcelWorkbook>' +
  82. '<ss:Styles>' +
  83. '<ss:Style ss:ID="Default">' +
  84. '<ss:Alignment ss:Vertical="Top" ss:WrapText="1" />' +
  85. '<ss:Font ss:FontName="arial" ss:Size="10" />' +
  86. '<ss:Borders>' +
  87. '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Top" />' +
  88. '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Bottom" />' +
  89. '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Left" />' +
  90. '<ss:Border ss:Color="#e4e4e4" ss:Weight="1" ss:LineStyle="Continuous" ss:Position="Right" />' +
  91. '</ss:Borders>' +
  92. '<ss:Interior />' +
  93. '<ss:NumberFormat />' +
  94. '<ss:Protection />' +
  95. '</ss:Style>' +
  96. '<ss:Style ss:ID="title">' +
  97. '<ss:Borders />' +
  98. '<ss:Font />' +
  99. '<ss:Alignment ss:WrapText="1" ss:Vertical="Center" ss:Horizontal="Center" />' +
  100. '<ss:NumberFormat ss:Format="@" />' +
  101. '</ss:Style>' +
  102. '<ss:Style ss:ID="headercell">' +
  103. '<ss:Font ss:Bold="1" ss:Size="10" />' +
  104. '<ss:Alignment ss:WrapText="1" ss:Horizontal="Center" />' +
  105. '<ss:Interior ss:Pattern="Solid" ss:Color="#A3C9F1" />' +
  106. '</ss:Style>' +
  107. '<ss:Style ss:ID="even">' +
  108. '<ss:Interior ss:Pattern="Solid" ss:Color="#CCFFFF" />' +
  109. '</ss:Style>' +
  110. '<ss:Style ss:Parent="even" ss:ID="evendate">' +
  111. '<ss:NumberFormat ss:Format="yyyy-MM-dd" />' +
  112. '</ss:Style>' +
  113. '<ss:Style ss:Parent="even" ss:ID="evenint">' +
  114. '<ss:NumberFormat ss:Format="0" />' +
  115. '</ss:Style>' +
  116. '<ss:Style ss:Parent="even" ss:ID="evenfloat">' +
  117. '<ss:NumberFormat ss:Format="0.00" />' +
  118. '</ss:Style>' +
  119. '<ss:Style ss:ID="odd">' +
  120. '<ss:Interior ss:Pattern="Solid" ss:Color="#CCCCFF" />' +
  121. '</ss:Style>' +
  122. '<ss:Style ss:Parent="odd" ss:ID="odddate">' +
  123. '<ss:NumberFormat ss:Format="yyyy-MM-dd" />' +
  124. '</ss:Style>' +
  125. '<ss:Style ss:Parent="odd" ss:ID="oddint">' +
  126. '<ss:NumberFormat ss:Format="0" />' +
  127. '</ss:Style>' +
  128. '<ss:Style ss:Parent="odd" ss:ID="oddfloat">' +
  129. '<ss:NumberFormat ss:Format="0.00" />' +
  130. '</ss:Style>' +
  131. '</ss:Styles>' +
  132. worksheet.xml +
  133. '</ss:Workbook>';
  134. },
  135. createWorksheet: function(title, includeHidden) {
  136. var cellType = [];
  137. var cellTypeClass = [];
  138. var cm = this.columns;
  139. var totalWidthInPixels = 0;
  140. var colXml = '';
  141. var headerXml = '';
  142. var visibleColumnCountReduction = 0;
  143. var colCount = cm.length;
  144. for (var i = 0; i < colCount; i++) {
  145. cm[i].header = cm[i].header == null ? cm[i].text : cm[i].header;
  146. if ((cm[i].dataIndex != '') && (includeHidden || cm[i].width != 0)) {
  147. var w = cm[i].width;
  148. totalWidthInPixels += w;
  149. if (cm[i].header === ""){
  150. cellType.push("None");
  151. cellTypeClass.push("");
  152. ++visibleColumnCountReduction;
  153. }
  154. else
  155. {
  156. colXml += '<ss:Column ss:AutoFitWidth="1" ss:Width="' + w + '" />';
  157. headerXml += '<ss:Cell ss:StyleID="headercell">' +
  158. '<ss:Data ss:Type="String">' + cm[i].header + '</ss:Data>' +
  159. '<ss:NamedCell ss:Name="Print_Titles" /></ss:Cell>';
  160. if(this.store.fields){
  161. var fld = this.store.fields.get(cm[i].dataIndex);
  162. switch(fld.type) {
  163. case "int":
  164. cellType.push("Number");
  165. cellTypeClass.push("int");
  166. break;
  167. case "float":
  168. cellType.push("Number");
  169. cellTypeClass.push("float");
  170. break;
  171. case "bool":
  172. case "boolean":
  173. cellType.push("String");
  174. cellTypeClass.push("");
  175. break;
  176. case "date":
  177. /*cellType.push("DateTime");
  178. cellTypeClass.push("date");*/
  179. cellType.push("String");
  180. cellTypeClass.push("");
  181. break;
  182. default:
  183. cellType.push("String");
  184. cellTypeClass.push("");
  185. break;
  186. }
  187. } else {
  188. switch(cm[i].xtype) {
  189. case "numbercolumn":
  190. cellType.push("Number");
  191. cellTypeClass.push("float");
  192. break;
  193. case "booleancolumn":
  194. cellType.push("String");
  195. cellTypeClass.push("");
  196. break;
  197. case "datecolumn":
  198. /* cellType.push("DateTime");
  199. cellTypeClass.push("date");*/
  200. cellType.push("String");
  201. cellTypeClass.push("");
  202. break;
  203. default:
  204. cellType.push("String");
  205. cellTypeClass.push("");
  206. break;
  207. }
  208. }
  209. }
  210. }
  211. }
  212. var visibleColumnCount = cellType.length - visibleColumnCountReduction;
  213. var result = {
  214. height: 9000,
  215. width: Math.floor(totalWidthInPixels * 30) + 50
  216. };
  217. if(!title){
  218. title = "优软ERP系统导出数据";
  219. }
  220. var t = '<ss:Worksheet ss:Name="' + title + '">' +
  221. '<ss:Names>' +
  222. '<ss:NamedRange ss:Name="Print_Titles" ss:RefersTo="=\'' + title+ '\'!R1:R2" />' +
  223. '</ss:Names>' +
  224. '<ss:Table x:FullRows="1" x:FullColumns="1"' +
  225. ' ss:ExpandedColumnCount="' + (visibleColumnCount + 2) +
  226. '" ss:ExpandedRowCount="' + (this.store.getCount() + 2) + '">' +
  227. colXml +
  228. '<ss:Row ss:Height="38">' +
  229. '<ss:Cell ss:StyleID="title" ss:MergeAcross="' + (visibleColumnCount - 1) + '">' +
  230. '<ss:Data xmlns:html="http://www.w3.org/TR/REC-html40" ss:Type="String">' +
  231. '<html:B>' + title + '</html:B></ss:Data><ss:NamedCell ss:Name="Print_Titles" />' +
  232. '</ss:Cell>' +
  233. '</ss:Row>' +
  234. '<ss:Row ss:AutoFitHeight="1">' +
  235. headerXml +
  236. '</ss:Row>';
  237. for (var i = 0, it = this.store.data.items, l = it.length; i < l; i++) {
  238. t += '<ss:Row>';
  239. var cellClass = (i & 1) ? 'odd' : 'even';
  240. r = it[i].data;
  241. var k = 0;
  242. for (var j = 0; j < colCount; j++) {
  243. if ((cm[j].dataIndex != '')
  244. && (includeHidden || cm[j].width != 0)) {
  245. var v = r[cm[j].dataIndex];
  246. if (cellType[k] !== "None") {
  247. t += '<ss:Cell ss:StyleID="' + cellClass + cellTypeClass[k] + '"><ss:Data ss:Type="' + cellType[k] + '">';
  248. if (cm[j].xtype == 'datecolumn') {
  249. if(v != null && v != '' && !v.toString().match(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/)){
  250. t += Ext.Date.toString(new Date(v));//v.format('Y-m-d');
  251. } else {
  252. t += v;
  253. }
  254. } else {
  255. t += v;
  256. }
  257. t +='</ss:Data></ss:Cell>';
  258. }
  259. k++;
  260. }
  261. }
  262. t += '</ss:Row>';
  263. }
  264. result.xml = t + '</ss:Table>' +
  265. '<x:WorksheetOptions>' +
  266. '<x:PageSetup>' +
  267. '<x:Layout x:CenterHorizontal="1" x:Orientation="Landscape" />' +
  268. '<x:Footer x:Data="Page &amp;P of &amp;N" x:Margin="0.5" />' +
  269. '<x:PageMargins x:Top="0.5" x:Right="0.5" x:Left="0.5" x:Bottom="0.8" />' +
  270. '</x:PageSetup>' +
  271. '<x:FitToPage />' +
  272. '<x:Print>' +
  273. '<x:PrintErrors>Blank</x:PrintErrors>' +
  274. '<x:FitWidth>1</x:FitWidth>' +
  275. '<x:FitHeight>32767</x:FitHeight>' +
  276. '<x:ValidPrinterInfo />' +
  277. '<x:VerticalResolution>600</x:VerticalResolution>' +
  278. '</x:Print>' +
  279. '<x:Selected />' +
  280. '<x:DoNotDisplayGridlines />' +
  281. '<x:ProtectObjects>False</x:ProtectObjects>' +
  282. '<x:ProtectScenarios>False</x:ProtectScenarios>' +
  283. '</x:WorksheetOptions>' +
  284. '</ss:Worksheet>';
  285. return result;
  286. }
  287. });