а

Setting the filename for export toExcel Solution


#1

I was playing with the export functionality today and was amazed that there was not a method that allowed you to set the actual excel filename… all exported files were named grid.xslx

To resolve this oversight just modify a few lines of code as such:

  1. call function and pass through desired variables
MyGrid.toExcel('./codebase/generate.php?title='+printViewTitle+'&filename='+title);

In this case I am passing both the tab title and filename through.

  1. Modify the file ‘gridExcelWrapper.php’ starting around line 230 you will see the ‘outXLS’ public function. add the parameter fileName.
public function outXLS($title, $type = 'Excel2007', $fileName) {
  1. also change the following 3 lines of code for each file type just below this to the following.
    XLS: header('Content-Disposition: attachment;filename="'.$fileName.'.xls"');
    CSV: header("Content-Disposition: attachment; filename=".$fileName.".csv");
    XLSX: header('Content-Disposition: attachment;filename="'.$fileName.'.xlsx"');

  2. Modify the file ‘gridExcelGenerator.php’ and around line 227 you will see

$this->wrapper->outXLS($this->title, $this->outputType)

change to this

$this->wrapper->outXLS($this->title, $this->outputType, $this->fileName)
  1. up near the top of this same file between lines 28 & 36 add the following line
public $fileName = 'grid';

and thats it… I’m must say that I’m not the worlds best coder so if there is a better solution please feel free to correct me… but this does work and does not corrupt the exported file format.

I hope this can help someone and maybe in the next version they will actually implement this or a better solution…


#2

Whoops sorry forgot one last thing… you need to set the value in the ‘generate.php’ file like this

$excel->fileName = strtolower(str_replace(' ','_',$_GET['filename']))."_".date(dmY);

or what ever format you choose…


#3

Hi,
Good work! Thanks for your efforts!
We will add method to do it in easy way in future.


#4

Please remember to add it to java version too!!! I do not install php. …nor jquery, nor knockout, nor requirejs, nor anglar–as dhtmltx does it all :slight_smile:


#5

ok, thank you for your feedback! :wink:


#6

Whether filename change option is included in Java version.

Is there any alternate option available to give custom filename (export excel and csv) in java version


#7

I am in the same position now. Did you get the answer for this ? @paulpandi8