Monday, June 2, 2008

Creating MS EXCEL Using CreateObject in PeopleSoft

Application Engine is great for batch processing, but it is not a reporting tool. Recently, our users requires every single AE to have a formatted output report, so my teammate came up with the solution that writes .txt file using writeline function. Its very simple, you just need to code it manually. Getting bored of the text file that comes out on my report, I look for some other ways to create a more decent output. Of course, XML Publisher would have been nice but I dont want to go thru that hassle creating a report definition, data source, etc. I browse thru PeopleBooks and found this PeopleCode function CreateObject! It has an example using Excel.Application, a few trial and error, and voala! I created an Excel Report! I only tested it on the NT Server though.

/*How to read data from one cell and writes to another*/
&oWorkApp = CreateObject("COM", "Excel.Application");
ObjectSetProperty(&oWorkApp, "Visible", True);
&oWorkBook = ObjectGetProperty(&oWorkApp, "Workbooks");
&oWorkBook.Open("C:\temp\TEST1.xls");
&oWorkSheet = &oWorkApp.Worksheets("Sheet1");
&oData = &oWorkSheet.Range("A1").Value;
&oWorkSheet.Range("A2").Value = &oData;
&oWorkApp.ActiveWorkbook.SaveAs("C:\temp\TestXLS.xls");
/*How to read data from one cell and writes to different sheet*/
&oWorkApp = CreateObject("COM", "Excel.Application");
ObjectSetProperty(&oWorkApp, "Visible", True);
&oWorkBook = ObjectGetProperty(&oWorkApp, "Workbooks");
&oWorkBook.Open("C:\temp\TEST1.xls");
&oWorkSheet = &oWorkApp.Worksheets("Sheet1");
&oData = &oWorkSheet.Range("A1").Value;
&oWorkSheet2 = &oWorkApp.Worksheets(2);
&oWorkSheet2.Range("A1").Value = &oData;
&oWorkApp.ActiveWorkbook.SaveAs("C:\temp\TestXLS.xls");

/*Add data to cells of the first worksheet in the new workbook*/
&oWorkApp = CreateObject("COM", "Excel.Application");
ObjectSetProperty(&oWorkApp, "Visible", True);
&oWorkBook = ObjectGetProperty(&oWorkApp, "Workbooks");
&oWorkBook.Open("C:\temp\TEST1.xls");
&oWorkSheet = &oWorkApp.Worksheets("Sheet1");
&oWorkSheet.Range("A1").Value = "Last Name";
&oWorkSheet.Range("B1").Value = "First Name";
&oWorkSheet.Range("A1:B1").Font.Bold = True;
&oWorkSheet.Range("A2").Value = "Doe";
&oWorkSheet.Range("B2").Value = "John";
&oWorkApp.ActiveWorkbook.SaveAs("C:\temp\TestXLS.xls");

/*Transfer the data to Excel from Rowset*/
&oWorkApp = CreateObject("COM", "Excel.Application");
ObjectSetProperty(&oWorkApp, "Visible", True);
&oWorkBook = ObjectGetProperty(&oWorkApp, "Workbooks");
&oWorkBook.Open("C:\temp\TEST1.xls");
&oWorkBook = &oWorkApp.Workbooks.Add();
&rs_Awards = CreateRowset(Record.PERTBL);
&rs_Awards.Fill("WHERE FILL.YEAR = '2008' AND FILL.STATUS = 'C'");
&oWorkSheet = &oWorkApp.Worksheets("Sheet1");
For &ie = 1 To &rs_Awards.activerowcount
&oWorkSheet.Cells(&ie, 1).Value = &rs_Awards.getrow(&ie).PERTBL.ID.Value;
&oWorkSheet.Cells(&ie, 2).Value = &rs_Awards.getrow(&ie).PERTBL.COMP.Value;
End-For;
&oWorkApp.ActiveWorkbook.SaveAs("C:\temp\TestXLS.xls");

/*Save an xls file as a CSV*/
&oWorkApp = CreateObject("COM", "Excel.Application");
ObjectSetProperty(&oWorkApp, "Visible", True);
&oWorkBook = ObjectGetProperty(&oWorkApp, "Workbooks");
&oWorkBook.Open("C:\temp\TEST1.xls");
&oWorkApp.ActiveWorkbook.SaveAs("C:\temp\TestXLS.csv", 6);

6 comments:

Unknown said...

astig!

Unknown said...

Hello ,

I am trying to read an excel file through peoplecode using the CreateObject function.
I am actually facing the error "Invalid parameter: CreateObject Excel.Application invalid class string for function ObjectGetProperty. The specified parameter is not correct for the function. It may be the wrong type of parameter (string, number, record, field etc) or an invalid value in this context."
when using the following code in my AE peoplecode:
Local object &oWorkApp,&oWorkBook;
&oWorkApp = CreateObject("COM", "Excel.Application");
&oWorkBook = ObjectGetProperty(&oWorkApp, "Workbooks");

Can you please help me in resolving this problem?

Thank you

Srini said...
This comment has been removed by the author.
Tom Williams Jr. said...

Has anybody seen any type of on-line documentation that covers standard Excel COM object references within PeopleCode?
I'm looking for something that provides me with a mapping on how to reference code created using VBA in to PeopleCode.

My current challenge is how to apply a theme to an Excel Spreadsheet using the following VBA.

Any tips would be greatly apprecaited.

ActiveSheet.ListObjects.Add(xlSrcRange, Range("$A$1:$H$30"), , xlYes).Name = "Table2"
Range("Table2[#All]").Select
ActiveSheet.ListObjects("Table2").TableStyle = "TableStyleMedium9"

Thanks…

Tom

Mayra said...

Partner.
Run your code, obviously change the folder paths, but Cando xecute sending to the process, it ends in error:
He says:


Error de automatización OLE en Excel.Application.DisplayAlerts: ObjectSetProperty: No se puede asignar la propiedad DisplayAlerts de la clase Application.. (180,161) REPRIES.MAIN.GBL.default.1900-01-01.Step01.OnExecute PCPC:340 Statement:4

Proceso 70238 interrumpido en paso REPRIES.MAIN.Step01 (PeopleCode); cd retorno = 8 (108,524)

I need someone please help me, that this error depends

K@rthik's said...

If we want to fetch for less rows using file layout ?? how we can do it?

Like, in our staging file we have 3-4 fields and in excel or .csv file have 10 columns ??

I am trying to achieve this with readrowset (Mismatching fields) and array (Pushing one row extra (like 2 fields are getting same value)).. both are not giving the desired result.


Local File &FILE1;
Local Record &REC;
Local SQL &SQL;
Local Rowset &FRS;


&FILE1 = GetFile("\\PSFINWEB\TEMP\Intfc_report.csv", "R", %FilePath_Absolute);
&REC_Main = CreateRecord(Record.INTF_PRJRES_STG);
&SQL = CreateSQL("%Insert(:1)");
rem &ARRAY = CreateArrayRept("", 0);


If &FILE1.IsOpen Then

If &FILE1.SetFileLayout(FileLayout.CCI_INTF_FL) Then
&FRS = &FILE1.CreateRowset();
&FRS = &FILE1.ReadRowset();
&FRS = &FILE1.ReadRowset();
While &FRS <> Null

&REC_Main.INTFC_ID.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.INTFC_ID.Value;
&REC_Main.INTFC_LINE_NUM.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.INTFC_LINE_NUM.Value;
&REC_Main.ACTIVITY_ID.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.ACTIVITY_ID.Value;
&REC_Main.BUSINESS_UNIT.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.BUSINESS_UNIT.Value;
&REC_Main.ACTIVITY_ID_2.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.PC_INTFC_STATUS.Value;
&REC_Main.PC_INTFC_STATUS.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.PC_INTFC_STATUS.Value;
&REC_Main.PC_LOAD_STATUS.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.PC_LOAD_STATUS.Value;
&REC_Main.ANALYSIS_TYPE.VALUE = &FRS.GetRow(1).INTF_PRJRES_STG.ANALYSIS_TYPE.Value;
&FRS = &FILE1.ReadRowset();
&REC_Main.INSERT();
End-While;

/*&str = &FILE1.ReadLine(&string);
While &FILE1.ReadLine(&string)
&check = "N";
&ARRAY = Split(&string, ",");
For &I = 1 To &REC1.FieldCount
If &check = "N" And
&I = 6 Then
&REC1.ACTIVITY_ID_2.Value = &ARRAY [&I];
&check = "S";
&ARRAY [&I] = &ARRAY [&I];
&I = &I - 1;
Else;
&REC1.GetField(&I).Value = &ARRAY [&I];

End-If;
End-For; */

End-If;

&FILE1.Close();
End-If;

Can anyone help me with this issue ?

Thanks in advance
Naveen