Neil_Willby Absent Member.
Absent Member.
701 views

C# to Cobol

Jump to solution

Im having a problem with one line below, can anyone advise ????

the problem line is 

set worksheet = workbook::ActiveSheet

from the c#

workbook.ActiveSheet

 

 

* // Creating a Excel object.
* Microsoft.Office.Interop.Excel._Application excel = new Microsoft.Office.Interop.Excel.Application();
* Microsoft.Office.Interop.Excel._Workbook workbook = excel.Workbooks.Add(Type.Missing);
* Microsoft.Office.Interop.Excel._Worksheet worksheet = null;

declare excel as type Microsoft.Office.Interop.Excel._Application = new type Microsoft.Office.Interop.Excel.Application
declare workbook as type Microsoft.Office.Interop.Excel._Workbook = excel::Workbooks::Add(type System.Type::Missing)
declare worksheet as type Microsoft.Office.Interop.Excel._Worksheet = null

try
* worksheet = workbook.ActiveSheet
set worksheet = workbook::ActiveSheet

* worksheet.Name = "ExportedFromDatGrid"
set worksheet::Name = "ExportedFromDataGrid"

* int cellRowIndex = 1;
declare cellRowIndex as binary-short = 1

* int cellColumnIndex = 1;
declare cellColumnIndex as binary-short = 1

declare i as binary-short = 0
declare j as binary-short = 0

* //Loop through each row and read value from each column.
* for (int i = 0; i < dgvCityDetails.Rows.Count - 1; i++)
perform varying i from 0 by 1 until i < dgvMain::Rows::Count - 1
* {
* for (int j = 0; j < dgvCityDetails.Columns.Count; j++)
perform varying j from 0 by 1 until j < dgvMain::Columns::Count
* {
* // Excel index starts from 1,1. As first Row would have the Column headers, adding a condition check.
* if (cellRowIndex == 1)
if cellRowIndex = 1
* {
* worksheet.Cells[cellRowIndex, cellColumnIndex] = dgvCityDetails.Columns.HeaderText;
set worksheet::Cells[cellRowIndex, cellColumnIndex] = dgvMain::Columns::HeaderText
* }
else
* {
* worksheet.Cells[cellRowIndex, cellColumnIndex] = dgvCityDetails.Rows.Cells.Value.ToString();
set worksheet::Cells[cellRowIndex, cellColumnIndex] = dgvMain::Rows::Cells::Value::ToString()
* }
end-if
* cellColumnIndex++;
set cellColumnIndex = cellColumnIndex + 1
* }
end-perform
* cellColumnIndex = 1;
set cellColumnIndex = 1
* cellRowIndex++;
set cellRowIndex = cellRowIndex + 1
* }
end-perform


catch
continue
end-try

0 Likes
1 Solution

Accepted Solutions
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: C# to Cobol

Jump to solution

If ActiveSheet returns an object, you may need to explicitly cast it.

set worksheet = workbook::ActiveSheet as type Microsoft.Office.Interop.Excel._Worksheet

0 Likes
4 Replies
Neil_Willby Absent Member.
Absent Member.

RE: C# to Cobol

Jump to solution
nope thats not it you can substitute 'to' with '=' in .NET Cobol
0 Likes
Highlighted
Micro Focus Expert
Micro Focus Expert

RE: C# to Cobol

Jump to solution

If ActiveSheet returns an object, you may need to explicitly cast it.

set worksheet = workbook::ActiveSheet as type Microsoft.Office.Interop.Excel._Worksheet

0 Likes
Neil_Willby Absent Member.
Absent Member.

RE: C# to Cobol

Jump to solution
Thanks Ted thats cracked it, but how did you know that ??? is C# different to Visual Cobol on this??
0 Likes
Micro Focus Expert
Micro Focus Expert

RE: C# to Cobol

Jump to solution

The error was that it could not implicitly convert object to _Worksheet, you therefore must do it explicitly using the `AS` or `AS IF` operator.

I think the reason this works in C# is that it automatically uses duck typing for COM objects. So it is effectively doing the following statement.

dynamic worksheet = workbook.ActiveSheet;

There is no support for duck typing in Visual COBOL though, so the casting is still necessary. More information can be found here:

https://jinishbhardwaj.wordpress.com/2016/06/21/com-interop-without-referencing-com-assemblies-using-dynamic-c/

0 Likes
The opinions expressed above are the personal opinions of the authors, not of Micro Focus. By using this site, you accept the Terms of Use and Rules of Participation. Certain versions of content ("Material") accessible here may contain branding from Hewlett-Packard Company (now HP Inc.) and Hewlett Packard Enterprise Company. As of September 1, 2017, the Material is now offered by Micro Focus, a separately owned and operated company. Any reference to the HP and Hewlett Packard Enterprise/HPE marks is historical in nature, and the HP and Hewlett Packard Enterprise/HPE marks are the property of their respective owners.