Wednesday, May 15, 2019

Create Excel report of selected item Revision

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.ss.usermodel.WorkbookFactory;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;

import com.teamcenter.rac.aif.kernel.AIFComponentContext;
import com.teamcenter.rac.aifrcp.AIFUtility;
import com.teamcenter.rac.kernel.TCComponent;
import com.teamcenter.rac.kernel.TCException;
import com.teamcenter.rac.kernel.TCProperty;
import com.teamcenter.rac.kernel.TCSession;


public class SampleHandler extends AbstractHandler {

TCSession session=null;;
TCComponent tcc=null;
public void CreateExcelReport() throws TCException, InvalidFormatException, IOException {

tcc=(TCComponent) AIFUtility.getCurrentApplication().getTargetComponent();
session=tcc.getSession();
session.getUser();

FileInputStream input=new FileInputStream(new File("C:\\Temp\\ExcelReport.xlsx"));
Workbook workbook=WorkbookFactory.create(input);
FileOutputStream fos = new FileOutputStream("C:\\Temp\\ExcelReport.xlsx");
Sheet sheet=(Sheet) workbook.getSheetAt(0);

AIFComponentContext[] children=tcc.getChildren();
TCProperty Object_Name;
       TCProperty Type;
       TCProperty Owner;
       TCProperty GropupID;
       TCProperty DateModified;
 
       int rowincr=1;
               int colincr=0;
     
               for(int i=0;i<children.length;i++)
{
TCComponent taskcomp=(TCComponent) children[i].getComponent();
Object_Name=taskcomp.getTCProperty("object_name");
Type=taskcomp.getTCProperty("object_type");
Owner=taskcomp.getTCProperty("owning_user");
GropupID=taskcomp.getTCProperty("owning_group");
DateModified=taskcomp.getTCProperty("last_mod_date");


Cell cell1=sheet.getRow(rowincr).getCell(colincr);
cell1.setCellValue(Object_Name.toString());
colincr++;

Cell cell2=sheet.getRow(rowincr).getCell(colincr);
cell2.setCellValue(Type.toString());
colincr++;

Cell cell3=sheet.getRow(rowincr).getCell(colincr);
cell3.setCellValue(Owner.toString());
colincr++;

Cell cell4=sheet.getRow(rowincr).getCell(colincr);
cell4.setCellValue(GropupID.toString());
colincr++;

Cell cell5=sheet.getRow(rowincr).getCell(colincr);
cell5.setCellValue(DateModified.toString());
colincr++;

colincr=0;

rowincr++;

     
workbook.write(fos);
fos.close();
}

No comments:

Post a Comment