Author Topic: Question about exportExcel function  (Read 3902 times)

noctrona

  • Pro Enterprise
  • Jr. Member
  • *
  • Posts: 82
    • View Profile
Question about exportExcel function
« on: May 19, 2014, 08:20:38 am »
Hi,

I check the "exportExcel" method in our pro API. And also check the export demo. Base on the demo and API, and want to know how the grid pass the data to background.
In the demo I saw this code (ASP.NET):
Code: [Select]
$("#grid_export").pqGrid("exportExcel", { url: "/pro/demos/excel", sheetName: "pqGrid sheet" });

And the background
Code: [Select]
[HttpPost, ValidateInput(false)]       
public String excel(String extension, String excel)
{
    if (extension != "csv" && extension != "xml")
    {               
        throw new Exception("Unsupported extension");
    }
    String filename = "pqGrid." + extension;           
    System.IO.File.WriteAllText(Server.MapPath("~") + "\\" + filename, excel);
    return filename;
}
 
[HttpGet]
public FileContentResult excel(String filename)
{
    String path = Server.MapPath("~") + "\\" + filename;
    String contents = System.IO.File.ReadAllText(path);
 
    if(filename.EndsWith(".csv")){
        return File(new System.Text.UTF8Encoding().GetBytes(contents), "text/csv", filename);
    }
    else if(filename.EndsWith(".xml")){
        return File(new System.Text.UTF8Encoding().GetBytes(contents), "text/xml", filename);
    }
    else{
        throw new Exception("unknown extension");
    }



And my platform is Salesforce. So I want to know how these grid data pass to background, then I can achieve it in Salesforce.

Thank you!

paramvir

  • Administrator
  • Hero Member
  • *****
  • Posts: 6263
    • View Profile
Re: Question about exportExcel function
« Reply #1 on: May 19, 2014, 08:43:50 am »
There are 2 requests to the server.

In first request, pqGrid sends (POST request) excel data and extension of the required file to the server.
You are supposed to save this data ( in a file or session ) and return the filename.
It corresponds to
Code: [Select]
[HttpPost, ValidateInput(false)]       
public String excel(String extension, String excel)
{
    if (extension != "csv" && extension != "xml")
    {               
        throw new Exception("Unsupported extension");
    }
    String filename = "pqGrid." + extension;           
    System.IO.File.WriteAllText(Server.MapPath("~") + "\\" + filename, excel);
    return filename;
}

Second request is send to fetch the file ( GET request) from server. you are supposed to return the data (saved in first request) as a file attachment in 2nd request.

Code: [Select]
[HttpGet]
public FileContentResult excel(String filename)
{
    String path = Server.MapPath("~") + "\\" + filename;
    String contents = System.IO.File.ReadAllText(path);
 
    if(filename.EndsWith(".csv")){
        return File(new System.Text.UTF8Encoding().GetBytes(contents), "text/csv", filename);
    }
    else if(filename.EndsWith(".xml")){
        return File(new System.Text.UTF8Encoding().GetBytes(contents), "text/xml", filename);
    }
    else{
        throw new Exception("unknown extension");
    }

« Last Edit: May 19, 2014, 08:50:54 am by paramquery »