[ Nexacro.Grid -  excel exort ]

 


참고 : 아래 객체중 [gExportObj]는 UX Studio에서 GlobalVariables에서 Objects 에 [ExportObject]로 추가한 객체명임

//-------------------------------------------------------------------------------------------------
// 기능 : 엑셀 다운로드 버튼 클릭 시
//-------------------------------------------------------------------------------------------------

function fn_Exceldn(obj:Button,  e:ClickEventInfo)
{
    //1. header 용 grid, dataset 생성
    var sSearchName;
    sSearchName = "[계획구분 : 1:경영계획] "
                + "[브랜드 : "   + divSearch.cboBrandCd.text + "] "
                + "[계획년도 : " + divSearch.divPlanOrdr.fn_GetPlanYy()   + ":年] "
                + "[계획차수 : " + divSearch.divPlanOrdr.fn_GetPlanOrdr() + ":차] ";

    gfn_bpCreateExcelGridDs("grdTitle0" , "dsTitle0" , 1, new Array("타이틀명",  '', sSearchName));
    gfn_bpCreateExcelGridDs("grdTitle1" , "dsTitle1" , 8, new Array("sub타이틀명1") );
    gfn_bpCreateExcelGridDs("grdTitle21", "dsTitle21", 8, new Array("sub타이틀명2"));
    gfn_bpCreateExcelGridDs("grdTitle22", "dsTitle22", 8, new Array("sub타이틀명3"));
    gfn_bpCreateExcelGridDs("grdTitle3" , "dsTitle3" , 6, new Array("sub타이틀명4") );

    //2. 생성할 grid 목록 지정
    //형식 : gridName:line 출력여부:헤더라인수:현grid exort 후 빈 라인
    var arrGridList = new Array("grdTitle0:noLine:0:1"
                              , "grdTitle1:line:0:0" , "divList.grdMainOne:line:2:1"
                              , "grdTitle21:line:0:0", "divList.grdMainTwo1:line:2:1"
                              , "grdTitle22:line:0:0", "divList.grdMainTwo2:line:2:1"
                              , "grdTitle3:line:0:0" , "divList.grdMainThree:line:1:1");

    //3. excel export
    gfn_bpMultiExcelDown(arrGridList, this.titletext, this.titletext);
}
 

/******************************************************************************************
 * 기      능 : 엑셀Export용 Grid와 Dataset 생성하기
 * 인      자 : psObjGrid   - grid name
                psObjDs      - dataset name
                pnCellCnt    - grid생성 시 cell 갯수 (해당 cell만큼 merge가 처리 된다.)
                paData       - dataset에 추가할 자료 (array 형)
 * 반      환 :
 *****************************************************************************************/

function gfn_bpCreateExcelGridDs(psObjGrid, psObjDs, pnCellCnt, paData){
    var objDs = new Dataset;
    
    //1. Dataset 생성
    if(!isValidObject(psObjDs) ){
        objDs = new Dataset;
        objDs.name = psObjDs;
        this.addChild(psObjDs, objDs);
        objDs.addColumn("text0", "STRING", 256);
    }else{
        objDs = eval(psObjDs);
    }
    
    //2. Grid 생성
    if(!isValidObject(psObjGrid) ){
        var poGrid = new Grid(psObjGrid, 0, 0, 1, 1);
        this.addChild(psObjGrid, poGrid);
        poGrid.show();

        poGrid.binddataset = psObjDs ;
        poGrid.insertContentsRow("body", 0, true);
        for(var i=0; i<pnCellCnt; i++) {poGrid.appendContentsCol(true)};
        
        poGrid.mergeContentsCell('body',0,0,0,pnCellCnt,0,false);
        
        poGrid.setCellProperty( "body", 0, "text" , "bind:text0");
        poGrid.setCellProperty( "body", 0, "align", "left");
    }
    
    //3. 자료 추가
    var nInstRow;
    if (objDs.rowcount == 0){
        for(var idx=0; idx<paData.length; idx++){
                nInstRow = objDs.addRow();
                objDs.setColumn(nInstRow, "text0", paData[idx]);
        }
    }
}
 


/******************************************************************************************
 * 정의 : 여러개 Grid를 Excel Export
 * 인자 : objGrid - 그리드
 *****************************************************************************************/

function gfn_bpMultiExcelDown(paObjList, psSheetName, psExcelName){
    var fileName = psExcelName + ".xlsx";
    var nStartRow = 1;
    
    gExportObj.clear();
    gExportObj.exporttype     = ExportTypes.EXCEL;
    gExportObj.activepagename = psSheetName;    
    gExportObj.exportfilename = fileName;
    gExportObj.exportopenmode = "overwrite";
    
    for(var nIdx=0; nIdx<paObjList.length; nIdx++){
    
        var arrProperty = paObjList[nIdx].split(':');
        var objGrid     = eval(arrProperty[0]);
        
        if (arrProperty[1].toUpperCase() == "LINE"){
            gExportObj.addExportItem(ExportItemTypes.GRID, objGrid , psSheetName+"!A" + nStartRow, "allband");
        }else{
            gExportObj.addExportItem(ExportItemTypes.GRID, objGrid , psSheetName+"!A" + nStartRow, "nohead,nosumm","allrecord","nosuppress","onlyvalue","none","color","both","cellline");
        }
         
        nStartRow = nStartRow + objGrid.rowcount + toNumber(arrProperty[2]) + toNumber(arrProperty[3]);
    }

    gExportObj.exportmessageprocess = "%d 항목을 출력중입니다. %d/%d";
    gExportObj.export();
}


/*********************************************************************************************
 ★ 설명
     Grid Excel Export하는 함수
 ★ parameter
    1. obj:Grid : Grid Object ( 예 : Grid00 )
    2. ExpObj:ExportObject : Export Object (옵션 : Default는 생성해서 처리됨)
    3. sSheetName : Sheet 명 ( 옵션 : Default="Sheet1" )
 ★ return
    - 성공 = true
    - 실패 = false
*********************************************************************************************/

function GridExportExcel(obj:Grid, ExpObj:ExportObject, sSheetName)

{

 if( fn_IsNull(obj) ) return false;

 

 if( fn_IsNull(ExpObj) == true )

  var ExpObj = new ExportObject;

 if( fn_IsNull(sSheetName) == true )

  sSheetName = "Sheet1";

  

 ExpObj.exportfilename = Grid.name+".xls";  // 꼭 줘야됨

 ExpObj.exporttype     = ExportTypes.EXCEL; // 현재 Excel만 가능합니다.

 ExpObj.activepagename = sSheetName;

 

 ExpObj.addExportItem(ExportItemTypes.GRID, obj, sSheetName+"!A1");

 ExpObj.export(); // Excel로 Export실행

 

 return true;

}

/*********************************************************************************************
  ★ 설명
      Grid Excel Export할 때 그리드에 보이는데로 열기. 단. "SEQ, No, 상태, 삭제"  컬럼은 제거
  ★ parameter
    1. obj:Grid : Grid Object ( 예 : Grid00 )
     2. sSheetName : Sheet 명 ( 옵션 : Default="Sheet1" )
     3. sActivemode : ExportObject의 exportactivemode를 지정 ( Default='active' )
     3. sFilePath : Export할 파일 풀 경로.
  ★ return
    - 성공 = true
    - 실패 = false
 *********************************************************************************************/

 function gf_gridViewExportExcel(obj:Grid, sSheetName, sActivemode, sFilePath)

{

         

        if( gf_isNull(obj)      )       return false;

 

        //1. Grid 복제

         var objGrid ;  

        if(this.components["excelGrid"]!=null)

         {

                 objGrid = this.components["excelGrid"];  

        }

         else

         {

                 objGrid = new Grid();

                 objGrid.init("excelGrid", 10, 10, 800, 100);

                 this.addChild("excelGrid", objGrid);

                 objGrid.binddataset = obj.binddataset;

                 objGrid.tooltiptext = obj.tooltiptext;

                 objGrid.visible = false;

                 objGrid.show();

         }

 

        var strFormatContents = obj.getCurFormatString();

         strFormatContents = "<Formats>" + strFormatContents + "</Formats>";

         objGrid.formats  = strFormatContents;

 

        //2. Excel에서 제외할 Column 제거..................  

        objGrid.deleteContentsCol("left",0);    // 틀고정 있는 컬럼을 먼저 제거한다.

         

        objGrid.enableevent = false;

         

        //그리드 컬럼 삭제하기 excelExportDelCell userdata 확인

         try{

                 var delCell = obj.excelExportDelCell;

                 if(!gf_isNull(delCell)){

                         var arr = delCell.toString().split(",").reverse();

                         for(i in arr){

                                 objGrid.deleteContentsCol(arr[i].trim());

                         }

                 }

         }catch(e){

                 trace("js_utill::excelExportDelCell 입력값 확인하세요");

         }

         

        var cellCount = objGrid.getCellCount("Head");

         for(i=cellCount-1; i>-1; i--)

         {

                 var cellText = objGrid.getCellProperty("Head", i, "text" );

                 

                if(cellText=="삭제"

                        || cellText=="상태"

                        || cellText == "No"  

                        || cellText == "SEQ")

                 {

                         objGrid.deleteContentsCol(i);

                 }

         }

         

        //colSize = 0 컬럼 제외

         cellCount = objGrid.getFormatColCount();

         for(i=cellCount-1; i>-1; i--)

         {

                 if(objGrid.getFormatColSize(i) == 0){

                         objGrid.deleteContentsCol(i);

                 }

         }

         

        objGrid.enableevent = true;

         

        ////trace(eval(objGrid.binddataset).saveXML());

 

        //3. ExpObj:ExportObject : Export Object (옵션 : Default는 생성해서 처리됨)

         var ExpObj = new ExportObject;

         

        if( gf_isNull(sSheetName) == true )

                 sSheetName = "Sheet1";

 

        ExpObj.exportfilename   = obj.name+".xls";      // 꼭 줘야됨

         ExpObj.exporttype       = ExportTypes.EXCEL;    // 현재 Excel만 가능합니다.     

        ExpObj.activepagename   = sSheetName;

 

        var chk = ExpObj.addExportItem(ExportItemTypes.GRID, objGrid, sSheetName+"!A1");        

        ExpObj.export();        // Excel로 Export실행

 

        //trace("0:성공, -1:실패 = "+chk);

        return true;

 }

 

/*********************************************************************************************
  ★ 설명
      Grid Excel Export할 때 옵션에 따라 데이터셋내용 전체를 열거나,
      "SEQ, No, 상태, 삭제"  컬럼은 제거하는 옵션.
      그리고 바로열기, 파일로 저장, 취소 3가지 옵션.
      
     현재 :그리드의 tooltiptext 속성명으로 파일명 생성되도록 하였음.
       
 ★ parameter
    1. obj:Grid : Grid Object ( 예 : Grid00 )
 ★ return
    - 성공 = true
    - 실패 = false
 *********************************************************************************************/

 function gf_gridExportExcel(obj:Grid,  e:ClickEventInfo)

 {

         var arg_url = "common_pop::pop_Confirm.xfdl";

         var returnObj = gf_showModal(arg_url, "", false);

         

        var excelInfo = returnObj[0];

         var rtn =  excelInfo["open_type"];                      //열기 종류

         var excelType = excelInfo["excel_type"];        //엑셀 종류

         

//      var rtn = 0;

 //      var excelType = "0001";

 

        if(excelType == '0002') //전체

         {

                 if(obj.getFormatString().indexOf("\"import\"") == -1){

                         gf_message("저장용 그리드 포맷이 존재하지 않습니다.\n관리자에게 문의해 주십시오.", "오류");

                         return;

                 }

         }

         

        var objVFile ;

 

        if(rtn==0)

         {       

                if(excelType == '0001') //보이는데로

                 {

                         gf_gridViewExportExcel(obj, "Sheet1", "active") ;

                 }

                 else    //전체다운

                 {

                         gf_gridAllExportExcel(obj, "Sheet1", "active") ;

                 }

         }

         else if(rtn==1)

         {       

        

                var FileDialog_excel = new FileDialog;

                 FileDialog_excel.defaultextention = true;

 

                var sFilter = "Excel 통합문서 (*.xlsx)|*.xlsx|Excel 97 - 2003 통합문서 (*.xls)|*.xls";

                 FileDialog_excel.filter = sFilter;

 

                // Excel저장을 위해 파일다이얼로그창를 띄운다.

                 objVFile = FileDialog_excel.open("파일저장", FileDialog.SAVE, "%MYDOCUMENT%", obj.tooltiptext );

 

                if(excelType == '0001') //보이는데로

                 {

                         //obj.formatid = "default";

                 }

                 else    //전체다운

                 {

                         //import용 그리드 포멧을 만들어놔야 한다.

                         obj.formatid = "import";                

                }

 

                if(excelType == '0001') //보이는데로

                 {

                 

                        //1. Grid 복제(복제 안하면 그리드 내용이 삭제됨.)

                         var objGrid ;  

                        if(this.components["excelGrid"]!=null)

                         {

                                 objGrid = this.components["excelGrid"];  

                        }

                         else

                         {

                                 objGrid = new Grid();  

                                objGrid.init("excelGrid", 10, 10, 800, 100);

                                 this.addChild("excelGrid", objGrid);

                                objGrid.binddataset = obj.binddataset;

                                 objGrid.tooltiptext = obj.tooltiptext;

                                 objGrid.visible = false;

                                 objGrid.show();

                        }

          

                        var strFormatContents = obj.getCurFormatString();

                         objGrid.formats  = obj.formats;

                         

                

                        //Excel에서 제외할 Column 제거..................  

                        objGrid.deleteContentsCol("left",0);    // 틀고정 있는 컬럼제거

                         

                        var cellCount = objGrid.getCellCount("Head");

                 

                        objGrid.enableevent = false;

 

                        for(i=cellCount-1; i>-1; i--)

                         {

                                 var cellText = objGrid.getCellProperty("Head", i, "text" );

 

                                try{

                                         if(cellText=="삭제" || cellText=="상태" || cellText.toLowerCase()=="seq" || cellText.toLowerCase()=="no")

                                         {

                                                 objGrid.deleteContentsCol(i);

                                         }

                                 }catch(e){}

                         }

                         

                        //colSize = 0 컬럼 제외

                         cellCount = objGrid.getFormatColCount();

                         for(i=cellCount-1; i>-1; i--)

                         {

                                 if(objGrid.getFormatColSize(i) == 0){

                                         objGrid.deleteContentsCol(i);

                                 }

                         }

                        

                        objGrid.enableevent = true;

                 }

 

                if( objVFile == undefined ) return;

                 

                var ExpObj = new ExportObject;

                 var sSheetName = "Sheet1";

 

                ExpObj.exportfilename   = objVFile.fullpath;    // 꼭 줘야됨

                 ExpObj.exporttype       = ExportTypes.EXCEL;    // 현재 Excel만 가능합니다.     

                ExpObj.activepagename   = sSheetName;

 

                ExpObj.exportopenmode   = "alwaysnew";

                 ExpObj.exportactivemode = "noactive";

                 ExpObj.exportsavemode   = "save";

 

                if(excelType == '0001') //보이는데로

                 {

                         var chk = ExpObj.addExportItem(ExportItemTypes.GRID, objGrid, sSheetName+"!A1");        

                }

                 else

                 {

                         var chk = ExpObj.addExportItem(ExportItemTypes.GRID, obj, sSheetName+"!A1");    

                }

                 

                ExpObj.export();        // Excel로 Export실행

 

                //trace("0:성공, -1:실패 = "+chk);

 

                obj.formatid = "default";

 

        }

         else if(rtn==2)

         {

                 // 취소

         }

 }

 

 
 

 

'Nexacro-Function > Grid' 카테고리의 다른 글

Nexacro.Grid - treeView2  (0) 2017.01.28
Nexacro.Grid - treeView  (0) 2017.01.28
Nexacro.Grid - copy & paste  (0) 2017.01.28
Nexacro.Grid - Conents Editor  (0) 2017.01.28
Nexacro.Grid - all Checked 처리  (0) 2017.01.28
Posted by 농부지기
,