x1
2<div id="grid_export" style="margin: auto;">
3</div>
4
951
2$(function () {
3var colM = [
4{ title: "ShipCountry", width: 120, dataIndx: "ShipCountry" },
5{ title: "Customer Name", width: 130, dataIndx: "ContactName" },
6{
7title: "Freight", width: 120, format: '$##,###.00',
8summary: {
9type: "sum"
10},
11dataType: "float", dataIndx: "Freight"
12},
13{ title: "Shipping Via", width: 130, dataIndx: "ShipVia" },
14{ title: "Shipped Date", width: 100, dataIndx: "ShippedDate", dataType: "date", format: 'yy-mm-dd' },
15//{ title: "Shipping Address", width: 220, dataIndx: "ShipAddress" },
16{ title: "Shipping City", width: 130, dataIndx: "ShipCity" }
17];
18var dataModel = {
19location: "remote",
20dataType: "JSON",
21method: "GET",
22url: "/Content/orders.json"
23//url: "/pro/orders/get",//for ASP.NET
24//url: "orders.php",//for PHP
25};
26var groupModel = {
27on: true,
28dataIndx: ['ShipCountry'],
29collapsed: [false],
30title: [
31"{0} ({1})",
32"{0} - {1}"
33]
34};
35var obj = {
36height: 500,
37toolbar: {
38items: [
39{
40type: 'select',
41label: 'Format: ',
42cls: 'export-format',
43options: [{ xlsx: 'Excel', csv: 'Csv', htm: 'Html', pdf: 'PDF' }]
44},
45{
46type: 'button',
47label: "Export",
48icon: 'ui-icon-arrowthickstop-1-s',
49listener: function () {
50
51var format = this.toolbar().find('.export-format').val(),
52data = this.exportData({
53format: format,
54render: true
55});
56if (format == 'pdf') {
57var dd = {
58footer: function (currentPage, pageCount) { return currentPage.toString() + ' of ' + pageCount; },
59pageOrientation: 'landscape',
60content: [
61{
62//include document definition of grid.
63table: data
64}
65]
66};
67pq.getScripts(['/pdfmake027/pdfmake.min.js', '/pdfmake027/vfs_fonts.min.js'], function () {
68//finally open the pdf file.
69pdfMake.createPdf(dd).open();
70});
71}
72else {
73pq.saveAs(data, "pqGrid." + format);
74}
75}
76}]
77},
78dataModel: dataModel,
79scrollModel: { autoFit: true },
80colModel: colM,
81numberCell: { show: false },
82menuIcon: true,
83selectionModel: { type: 'cell' },
84groupModel: groupModel,
85title: 'Group Rows',
86showTitle: false,
87resizable: true,
88hwrap: false,
89wrap: false
90};
91var grid = pq.grid("#grid_export", obj);
92
93});
94
95
351
2[HttpPost, ValidateInput(false)]
3public String exportData(String pq_ext, String pq_data, bool pq_decode, String pq_filename)
4{
5String[] arr = new String[] { "csv", "htm", "json", "xlsx", "zip" };
6if (arr.Contains(pq_ext))
7{
8String filename = pq_filename + "." + pq_ext;
9Session["pq_data"] = pq_data;
10Session["pq_decode"] = pq_decode;
11Session["pq_filename"] = filename;
12return filename;
13}
14else
15{
16throw new Exception("unsupported format!");
17}
18}
19
20[HttpGet]
21public FileContentResult exportData(String pq_filename)
22{
23if ( Session["pq_filename"].ToString() == pq_filename )
24{
25String contents = Session["pq_data"].ToString();
26byte[] bytes = ((bool)Session["pq_decode"])? Convert.FromBase64String(contents):
27new System.Text.UTF8Encoding().GetBytes(contents);
28return File(bytes, "application/octet-stream", pq_filename);
29}
30else
31{
32throw new Exception("unknown file.");
33}
34}
35
391
2if (isset($_POST["pq_data"]) && isset($_POST["pq_ext"]))
3{
4$pq_ext = $_POST["pq_ext"];
5$ext = array("csv", "htm", "json", "xlsx", "zip");
6if (in_array($pq_ext, $ext))
7{
8session_start();
9$data = $_POST['pq_data'];
10$pq_decode = $_POST["pq_decode"];
11if( $pq_decode === "true" ){
12$data = base64_decode($data);
13}
14$_SESSION['data'] = $data;
15$filename = $_POST['pq_filename']. "." . $pq_ext;
16$_SESSION['filename'] = $filename;
17echo $filename;
18}
19}
20else if(isset($_GET["pq_filename"]))
21{
22$filename = $_GET["pq_filename"];
23session_start();
24if ($filename == $_SESSION["filename"] )
25{
26if (isset($_SESSION['data'])) {
27$data = $_SESSION['data'];
28$_SESSION['data']=null;
29
30header('Content-Disposition: attachment; filename="'.$filename.'"');
31header("Content-Type: application/octet-stream");
32header('Content-Length: ' . strlen($data));
33header('Connection: close');
34echo $data;
35exit;
36}
37}
38}
39
431
2@Controller
3public class ExportDataController {
4@RequestMapping(value = "/exportData", method = RequestMethod.POST)
5public @ResponseBody
6String exportData(String pq_data, String pq_ext, Boolean pq_decode, String pq_filename, HttpServletRequest request) throws IOException {
7
8String[] arr = new String[] {"csv", "xlsx", "htm", "zip", "json"};
9String filename = pq_filename + "." + pq_ext;
10
11if(Arrays.asList(arr).contains(pq_ext)){
12HttpSession ses = request.getSession(true);
13ses.setAttribute("pq_data", pq_data);
14ses.setAttribute("pq_decode", pq_decode);
15ses.setAttribute("pq_filename", filename);
16}
17return filename;
18}
19
20@RequestMapping(value = "/exportData", method = RequestMethod.GET)
21public void exportData(String pq_filename, HttpServletRequest request, HttpServletResponse response) throws IOException, Base64DecodingException {
22HttpSession ses = request.getSession(true);
23if ( ((String)ses.getAttribute("pq_filename")).equals(pq_filename) ) {
24
25String contents = (String) ses.getAttribute("pq_data");
26Boolean pq_decode = (Boolean) ses.getAttribute("pq_decode");
27
28byte[] bytes = pq_decode? Base64.decode(contents): contents.getBytes(Charset.forName("UTF-8"));
29
30response.setContentType("application/octet-stream");
31
32response.setHeader("Content-Disposition",
33"attachment;filename=" + pq_filename);
34response.setContentLength(bytes.length);
35ServletOutputStream out = response.getOutputStream();
36out.write(bytes);
37
38out.flush();
39out.close();
40}
41}
42}
43