使用powerbuilder从excel写入和读取

时间:2012-06-27 09:39:03

标签: powerbuilder

使用Powerbuilder“OLE”

从excel表中写入和读取的任何样本(我需要在excel表文件中写入值并在excel执行其某些功能后读取其他值)

1 个答案:

答案 0 :(得分: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很慢。