示例#1
0
文件: excel.go 项目: gitdlam/excel
func (mso *MSO) SetOption(args ...interface{}) (err error) {
	defer Except("SetOption", &err)
	ops, curs, isinit := mso.Option, Option{}, false
	leng := len(args)
	if leng == 1 {
		switch args[0].(type) {
		case int:
			if args[0].(int) > 0 {
				curs, isinit = ops, true
			}
		case Option:
			curs = args[0].(Option)
		}
	} else if leng == 2 {
		if key, ok := args[0].(string); ok {
			curs[key] = args[1]
		}
	}
	for key, val := range curs {
		if isinit {
			_, err = oleutil.PutProperty(mso.IdExcel, key, val)
		} else if one, ok := ops[key]; !ok || val != one {
			_, err = oleutil.PutProperty(mso.IdExcel, key, val)
			ops[key] = val
		}
	}
	return
}
示例#2
0
文件: adodb.go 项目: mattn/go-adodb
func (c *AdodbConn) prepare(ctx context.Context, query string) (driver.Stmt, error) {
	unknown, err := oleutil.CreateObject("ADODB.Command")
	if err != nil {
		return nil, err
	}
	s, err := unknown.QueryInterface(ole.IID_IDispatch)
	if err != nil {
		return nil, err
	}
	_, err = oleutil.PutProperty(s, "ActiveConnection", c.db)
	if err != nil {
		return nil, err
	}
	_, err = oleutil.PutProperty(s, "CommandText", query)
	if err != nil {
		return nil, err
	}
	_, err = oleutil.PutProperty(s, "CommandType", 1)
	if err != nil {
		return nil, err
	}
	_, err = oleutil.PutProperty(s, "Prepared", true)
	if err != nil {
		return nil, err
	}
	val, err := oleutil.GetProperty(s, "Parameters")
	if err != nil {
		return nil, err
	}
	return &AdodbStmt{c, s, val.ToIDispatch(), nil}, nil
}
示例#3
0
文件: excel.go 项目: dlintw/go-ole
func writeExample(excel, workbooks *ole.IDispatch, filepath string) {

	// ref: https://msdn.microsoft.com/zh-tw/library/office/ff198017.aspx
	// http://stackoverflow.com/questions/12159513/what-is-the-correct-xlfileformat-enumeration-for-excel-97-2003
	const xlExcel8 = 56
	workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch()
	defer workbook.Release()
	worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
	defer worksheet.Release()
	cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch()
	oleutil.PutProperty(cell, "Value", 12345)
	cell.Release()
	activeWorkBook := oleutil.MustGetProperty(excel, "ActiveWorkBook").ToIDispatch()
	defer activeWorkBook.Release()

	os.Remove(filepath)
	// ref: https://msdn.microsoft.com/zh-tw/library/microsoft.office.tools.excel.workbook.saveas.aspx
	oleutil.MustCallMethod(activeWorkBook, "SaveAs", filepath, xlExcel8, nil, nil).ToIDispatch()

	//time.Sleep(2 * time.Second)

	// let excel could close without asking
	// oleutil.PutProperty(workbook, "Saved", true)
	// oleutil.CallMethod(workbook, "Close", false)
}
示例#4
0
文件: adodb.go 项目: mattn/go-adodb
func (s *AdodbStmt) bind(args []namedValue) error {
	if s.b != nil {
		for i, v := range args {
			var b string = "?"
			if len(s.b) < i {
				b = s.b[i]
			}
			unknown, err := oleutil.CallMethod(s.s, "CreateParameter", b, 12, 1)
			if err != nil {
				return err
			}
			param := unknown.ToIDispatch()
			defer param.Release()
			_, err = oleutil.PutProperty(param, "Value", v.Value)
			if err != nil {
				return err
			}
			_, err = oleutil.CallMethod(s.ps, "Append", param)
			if err != nil {
				return err
			}
		}
	} else {
		for i, v := range args {
			var err error
			var val *ole.VARIANT
			if v.Name != "" {
				val, err = oleutil.CallMethod(s.ps, "Item", v.Name)
			} else {
				val, err = oleutil.CallMethod(s.ps, "Item", int32(i))
			}
			if err != nil {
				return err
			}
			item := val.ToIDispatch()
			defer item.Release()
			_, err = oleutil.PutProperty(item, "Value", v.Value)
			if err != nil {
				return err
			}
		}
	}
	return nil
}
示例#5
0
文件: excel.go 项目: ChongFeng/beats
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Excel.Application")
	excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(excel, "Visible", true)
	workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
	workbook := oleutil.MustCallMethod(workbooks, "Add", nil).ToIDispatch()
	worksheet := oleutil.MustGetProperty(workbook, "Worksheets", 1).ToIDispatch()
	cell := oleutil.MustGetProperty(worksheet, "Cells", 1, 1).ToIDispatch()
	oleutil.PutProperty(cell, "Value", 12345)

	time.Sleep(2000000000)

	oleutil.PutProperty(workbook, "Saved", true)
	oleutil.CallMethod(workbook, "Close", false)
	oleutil.CallMethod(excel, "Quit")
	excel.Release()

	ole.CoUninitialize()
}
示例#6
0
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Agent.Control.1")
	agent, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(agent, "Connected", true)
	characters := oleutil.MustGetProperty(agent, "Characters").ToIDispatch()
	oleutil.CallMethod(characters, "Load", "Merlin", "c:\\windows\\msagent\\chars\\Merlin.acs")
	character := oleutil.MustCallMethod(characters, "Character", "Merlin").ToIDispatch()
	oleutil.CallMethod(character, "Show")
	oleutil.CallMethod(character, "Speak", "こんにちわ世界")

	time.Sleep(4000000000)
}
示例#7
0
文件: excel.go 项目: dlintw/go-ole
func main() {
	log.SetFlags(log.Flags() | log.Lshortfile)
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("Excel.Application")
	excel, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.PutProperty(excel, "Visible", true)

	workbooks := oleutil.MustGetProperty(excel, "Workbooks").ToIDispatch()
	cwd, _ := os.Getwd()
	writeExample(excel, workbooks, cwd+"\\write.xls")
	readExample(cwd+"\\excel97-2003.xls", excel, workbooks)
	showMethodsAndProperties(workbooks)
	workbooks.Release()
	// oleutil.CallMethod(excel, "Quit")
	excel.Release()
	ole.CoUninitialize()
}
示例#8
0
文件: ie.go 项目: ChongFeng/beats
func main() {
	ole.CoInitialize(0)
	unknown, _ := oleutil.CreateObject("InternetExplorer.Application")
	ie, _ := unknown.QueryInterface(ole.IID_IDispatch)
	oleutil.CallMethod(ie, "Navigate", "http://www.google.com")
	oleutil.PutProperty(ie, "Visible", true)
	for {
		if oleutil.MustGetProperty(ie, "Busy").Val == 0 {
			break
		}
	}

	time.Sleep(1e9)

	document := oleutil.MustGetProperty(ie, "document").ToIDispatch()
	window := oleutil.MustGetProperty(document, "parentWindow").ToIDispatch()
	// set 'golang' to text box.
	oleutil.MustCallMethod(window, "eval", "document.getElementsByName('q')[0].value = 'golang'")
	// click btnG.
	btnG := oleutil.MustCallMethod(window, "eval", "document.getElementsByName('btnG')[0]").ToIDispatch()
	oleutil.MustCallMethod(btnG, "click")
}