angularjs, spring boot, apache poi, can't generate excel document -


i'm trying generate excel document. i'm using apache poi version 3.14

my controller:

@restcontroller @requestmapping("/excel") public class excelrest {      @autowired     private profildao profildao;      @requestmapping(value = "/get",  method = requestmethod.get)     public modelandview getexcel(httpservletrequest request, httpservletresponse response) throws exception{           response.setheader("content-disposition", "attachment; filename=testexcel.xls");         response.setcontenttype("application/vnd.ms-excel");          // list of profil.         list<profil> profils = profildao.findall();          return new modelandview(new excelprofilsbuilder(), "profils", profils);      } } 

my modelview :

public class excelprofilsbuilder extends abstractxlsview {      @override     protected void buildexceldocument(         map<string, object> model,          workbook workbook,          httpservletrequest request,         httpservletresponse response) throws exception {          // on récupère les profiles provenant de la méthode rest          @suppresswarnings("unchecked")         list<profil> profiles = (list<profil>) model.get("profils");          // create new excel sheet         hssfsheet sheet = (hssfsheet) workbook.createsheet("profiles");         sheet.setdefaultcolumnwidth(30);          // create style header cells         cellstyle style = workbook.createcellstyle();         font font = workbook.createfont();         font.setfontname("arial");         style.setfillforegroundcolor(hssfcolor.blue.index);         style.setfillpattern(cellstyle.solid_foreground);         font.setboldweight(hssffont.boldweight_bold);         font.setcolor(hssfcolor.white.index);         style.setfont(font);          // create header row         hssfrow header = sheet.createrow(0);          header.createcell(0).setcellvalue("nom");         header.getcell(0).setcellstyle(style);          header.createcell(1).setcellvalue("prénom");         header.getcell(1).setcellstyle(style);          // create data rows         int rowcount = 1;          (profil profil : profiles) {             hssfrow arow = sheet.createrow(rowcount++);             arow.createcell(0).setcellvalue(profil.getname());             arow.createcell(0).setcellvalue(profil.getfirstname());         }      }  } 

to finish, angularjs :

$http.get('/excel/get').then(     function(response){          var blob = new blob([response], {type: "application/vnd.ms-excel"});         var objecturl = url.createobjecturl(blob);         window.open(objecturl);      },      function(response){          //error case      } ); 

i don't know why, when receive files, name is, exemple "8a4c6f51-3323-4f1b-bc4b-bd041801e66c.xls"

and have 1 cells : [object object]

edit : method doesn't works :

@requestmapping(value = "/get/excel", method = requestmethod.get) @responsebody public void getexcel(httpservletresponse response) throws exception{      response.setcontenttype("application/vnd.ms-excel");     response.setheader("content-disposition", "attachment; filename=monfichier.xls");      hssfworkbook workbook = new hssfworkbook();     hssfsheet sheet = workbook.createsheet("utilisateurs");     hssfrow user = sheet.createrow(0);     hssfcell name = user.createcell(0);     name.setcellvalue("test");      try {         workbook.write(response.getoutputstream());     } catch (ioexception e) {      }   } 

edit 2 : it's working if i'm using $http.post instead of $http.get :

    $http.post('/list/profil/get/excel',null, {responsetype:'arraybuffer'})       .success(function (response) {           console.log("plop");            var file = new blob([response], {type: 'application/vnd.ms-excel'});            var fileurl = url.createobjecturl(file);            window.open(fileurl);     }); 

but have bad filename.

instead of

return new modelandview(new excelprofilsbuilder(), "profils", profils);

return this...

return new modelandview("excelprofilsbuilder", "profils", profils); 

resource bundles:

create views.properties file under applications class-path. in eclipse located under /src/main/resources/views.properties. "excelprofilsbuilder" view name used in excel controller new modelandview("excelprofilsbuilder", "employees",employees);

in views.properties file, add entry...

excelprofilsbuilder.(class)=com.abc.def.excelprofilsbuilder  

Comments

Popular posts from this blog

sql - invalid in the select list because it is not contained in either an aggregate function -

Angularjs unit testing - ng-disabled not working when adding text to textarea -

How to start daemon on android by adb -