When using Guriddo jqGrid, it is often useful and important to export your data to MS Excel, PDF or CSV file formats in order to make it accessible offline, share it with other users, etc.
The Guriddo jqGrid JS grid provides client Excel, PDF an CSV export functionality (server-agnostic) which can be directly utilized to serve the purpose to share data in the aforementioned ways. To enable it, you can trigger export by invoking the exportToExcel, exportToPdf and exportToCsv methods from the client API of the grid.
Additionally, you have the option to customize the rows/columns and cells of the exported file by intercepting the export event.
Below are common rules which are valid for all export methods.
A CSV is a comma separated values file/string which allows data to be saved in a table structured format. CSVs look like a garden-variety spreadsheet but with a .csv extension (Traditionally they take the form of a text file containing information separated by commas, hence the name).
CSV files can be used with any spreadsheet program, such as Microsoft Excel, Open Office Calc, or Google Spreadsheets. They differ from other spreadsheet file types in that you can only have a single sheet in a file, they can not save cell, column, or row styling, and can not save formulas
This method does not require additional plugin or external programs. When called the method either propmt to save the exported data to a CSV file or it can return the exported data as CSV string depending on configuration (see below).
To export to CSV file format simple call the method:
$("#grid_id").jqGrid('exportToCsv', options);
Where options is a object with a following default properties:
options = separator: ",", separatorReplace : " ", quote : '"', escquote : '"', newLine : "\r\n", replaceNewLine : " ", includeCaption : true, includeLabels : true, includeGroupHeader : true, includeFooter: true, includeHeader: true, fileName : "jqGridExport.csv", mimetype : "text/csv;charset=utf-8", onBeforeExport : null, returnAsString : false, loadIndicator : true, treeindent : ' ' >
A file with the XLSX file extension is a Microsoft Excel Open XML Format Spreadsheet file. It's an XML-based spreadsheet file created by Microsoft Excel version 2007 and later.
XLSX files organize data in cells that are stored in worksheets, which are in turn stored in workbooks, which are files that contain multiple worksheets. The cells are positioned by rows and columns and can contain styles, formatting, math functions, and more.
Spreadsheet files made in earlier versions of Excel are saved in the XLS format. Excel files that support macros are XLSM files
jqGrid export the data to Microsoft Excel Open XML Format only.
In order to use the method additional module is needed to be loaded. The name of the module is JSZip. The module is included into the package. More about the installation of this module can be seen here. The JSZip can be loaded via cdnjs.
If the JSZip module is not loaded the export to Excel will fail.
To export to Excel it is needed to load JSZip
script type="text/javascript" language="javascript" src="//cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js">script>
and call the method
$("#grid_id").jqGrid('exportToExcel', options);
Where options is a object with the following default properties:
options = includeLabels : true, includeGroupHeader : true, includeFooter: true, includeHeader: true, fileName : "jqGridExport.xlsx", mimetype : "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", maxlength : 40, onBeforeExport : null, replaceStr : null, customizeData : null, loadIndicator : true, treeindent : ' ' >
function _replStrFunc (v) return v.replace(/, '<') .replace(/>/g, '>') .replace(/[\x00-\x09\x0B\x0C\x0E-\x1F\x7F-\x9F]/g, ''); >
When called the method open a save dialog with the exported data. Select Save to save the file or Open to open it with the associated application.
The standard behavior of export to Excel method recognizes data types with help of parser and format it according to defined formats. Currently we recognize the following formats - string, Percent with d.p., Percent, Dollars, Pounds, Euros, Numbers without thousand separators, Numbers 2 d.p. without thousands separators, Negative numbers indicated by brackets, Negative numbers indicated by brackets - 2d.p., Numbers with thousand separators, Numbers with 2 d.p. and thousands separators and dates. The format in Excel is as follow:
formatCode="#,##0.00_-\ [$$-45C]" formatCode=""£"#,##0.00" formatCode="[$€-2]\ #,##0.00" formatCode="0.0%" formatCode="#,##0;(#,##0)" formatCode="#,##0.00;(#,##0.00)" formatCode="yyyy/mm/dd;@"
If this is not acceptable, then it is possible to disable the parser for certain field. This can be done with the help of the option excel_parser set to false in colModel exportoptions object like this:
$("#grid").jqGrid( . colModel: [ . name :'field'. exportoptions: excel_parser: false>. >, . ] >)
In this case we detect numbers not matching leading zeros or a negative and text. If this is not acceptable a custom export formatting can be defined - see above.
By default the string fields are parsed for "" and these are replaced with their HTML equivalent < and > with build in replaceStr function. See option replaceStr above.
Custom export to excel allow to format the data as defined in exportoptions property in colModel by the developer. The default properties in export options have the following values:
exportoptions : excel_parser : true, excel_format : '', replace_format : null >
Example: Our standard formatting of dollars places a dollar sign at end of value. In case there is a need to put it at the beginning of the value the following code do this:
$("#jqGrid").jqGrid( . colModel: [ label: 'Price', name: 'Price', formatter: 'number', align: 'right', exportoptions : excel_parsers: false, excel_format : "[$$-45C]\ #,##0.00_-" > >, . ], . >);
Added is a new function which enable adding custom styles (font and color only) during export - i.e the function can be called in onbeforeExport event and will return the new added style number which then can be used in the export.
The new function is called newExcelStyle and should be called like this within onbeforeExport event
var new_style_id = $.jgrid.newExcelStyle( xlsx, options);
The parameter xlsx is the Excel xml document with the data and styles - this parameter is assed to onBeforeExport event.
The options parameter is object has the following options.
options = font : size : 11, name : 'Calibri', options :"">, // options color : patternType : "solid", fgColor : "FFFFFFF", bgColor : 64 > // bgColor if number 0-64 >
The font option:
The color property has the following parameters
using our demo example we can change the color to red with font DejaVu Sans which is bold
$("#export").on("click", function() $("#jqGrid").jqGrid("exportToExcel", includeLabels : true, includeGroupHeader : true, includeFooter: true, fileName : "jqGridExport.xlsx", onBeforeExport : function( xlsx ) var mystyle_id = $.jgrid.newExcelStyle(xlsx, font : name : 'DejaVu Sans', options : ''>, color : fgColor : 'ff1a1a'> >); var sheet = xlsx.xl.worksheets['sheet1.xml']; // Loop over the cells in column 'D' $('row c[r^="D"]', sheet).each( function () //Get the value and strip the non numeric characters if ( $(this).text()* 1 >= 50 ) // apply style definition 20 for the cell // we have 50 predefined styles $(this).attr( 's', mystyle_id + "" ); > >); >, maxlength : 40 // maxlength for visible string data >); >);
The returned id mystyle_id is the new style id which then can be used for cell formatting
(Portable Document Format)
Portable Document Format (PDF) is a file format used to present and exchange documents reliably, independent of software, hardware, or operating system. Invented by Adobe, PDF is now an open standard maintained by the International Organization for Standardization (ISO). PDFs can contain links and buttons, form fields, audio, video, and business logic.
Today PDF is the most used exchange document format.
jqGrid support export of its data to PDF.
In order to use the method additional module is needed to be loaded. The name of the module is pdfmake. The module is included into the package. More about the installation of this module can be seen here. The pdfmake and vfs_fonts can be loaded via cdn.
Since the pdf creation is a heavy work, please use the method in relative small data set. Using the script on data set with more than 300 rows can cause problems and memory leaks.
To export to PDF it is needed to load the following javascripts
script type="text/javascript" language="javascript" src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.26/build/pdfmake.min.js"> script> script type="text/javascript" language="javascript" src="//cdn.rawgit.com/bpampuch/pdfmake/0.1.26/build/vfs_fonts.js">script>
and call the method
$("#grid_id").jqGrid('exportToPdf', options);
where the options is a object with the following properties and default values:
options = title: null, orientation: 'portrait', pageSize: 'A4', description: null, onBeforeExport: null, download: 'download', includeLabels : true, includeGroupHeader : true, includeFooter: true, includeHeader: true, fileName : "jqGridExport.pdf", mimetype : "application/pdf", loadIndicator : true, treeindent : "-" >
The default settings for the document before PDF export are follow:
var documentDefinition = pageSize: options.pageSize, pageOrientation: ooptions.orientation, content: [ style : 'tableExample', widths : jqgrid.widths, table: headerRows: 0, //or 1 body: jqgrid.rows > > ], styles: tableHeader: bold: true, fontSize: 11, color: '#2e6e9e', fillColor: '#dfeffc', alignment: 'center' >, tableBody: fontSize: 10 >, tableFooter: bold: true, fontSize: 11, color: '#2e6e9e', fillColor: '#dfeffc' >, title: alignment: 'center', fontSize: 15 >, description: <> >, defaultStyle: fontSize: 10 > >;
This definition is passed to the onBeforeExport event and it can be modified for custom purposes.
Below example shows how to make the body font of the table body smaller using the onBeforeExport event.
$("#export").on("click", function() $("#jqGrid").jqGrid("exportToPdf", . onBeforeExport : function( doc ) doc.styles.tableBody.fontSize = 8; >, . >); >);
HTML is the standard markup language for creating web pages and web applications. With Cascading Style Sheets (CSS) and JavaScript it forms a triad of cornerstone technologies for the World Wide Web. Web browsers receive HTML documents from a web server or from local storage and render them into multimedia web pages. HTML describes the structure of a web page semantically and originally included cues for the appearance of the document.
This method does not require additional plugin or external programs. When called the method either shows a new html page (with option to print) or it can return the exported data as HTML string depending on configuration (see below).
To export to HTML format simple call the method:
$("#grid_id").jqGrid('exportToHtml', options);
Where options is a object with a following default properties:
options = title: '', onBeforeExport: null, includeLabels : true, includeGroupHeader : true, includeFooter: true, includeHeader: true, tableClass : 'jqgridprint', autoPrint : false, topText : '', bottomText : '', returnAsString : false, treeindent : ' ' >
Copyright © TriRand Ltd, 2017 by Tony Tomov. All Rights Reserved.
Text is available under the Creative Commons Attribution-Non-Commercial 3.0 License