1

使用Powerbuilder“OLE”从excel表写入和读取的任何示例(我需要在excel表文件中写入值并在excel执行某些功能后读取其他值)

4

1 回答 1

3

以下是通过 OLE 访问 Excel 文件的示例:

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

worksheet.cells通过该属性可以轻松地读取和写入值。

唯一的问题是通过 OLE 在大量单元格上驱动 Excel 速度很慢。

于 2012-06-27T10:45:32.343 回答