问题
Any sample to write and read from excel sheet (i need to write values in excel sheet file and read other values after excel execute some its functions) using Powerbuilder "OLE"
回答1:
Here is a sample of accessing to an Excel file through OLE :
int li_rtn
string ls_range
oleobject lole_excel, lole_workbook, lole_worksheet, lole_range
lole_excel = create oleobject
li_rtn = lole_excel.ConnectToNewObject("excel.application")
if li_rtn <> 0 then
MessageBox( "Error", 'Error running MS Excel api.')
destroy lole_Excel
else
lole_excel.WorkBooks.Open("C:\some_path_to\sample.xls")
lole_workbook = lole_excel.application.workbooks(1)
lole_worksheet = lole_workbook.worksheets(1)
// Set the cell value
lole_worksheet.cells(1,11).value = "Some value" //it is cells(line, column)
//example to work on a range of cells
ls_range = "A1:F"+string(ll_excel_rows)
lole_range = lole_worksheet.Range(ls_range)
lole_range.Select
lole_range.Locked = True
// Save
lole_workbook.save()
// Quit
lole_excel.application.quit()
lole_excel.DisconnectObject()
destroy lole_Excel
end if
Reading and writing values can be easily done through the worksheet.cells
property.
The only problem is that driving Excel on a large amount of cells through OLE is slow.
来源:https://stackoverflow.com/questions/11223315/write-and-read-from-excel-using-powerbuilder