Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

xls_write.py 1.4KB

1 year ago
1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import datetime
  2. import adodbapi
  3. try:
  4. import adodbapi.is64bit as is64bit
  5. is64 = is64bit.Python()
  6. except ImportError:
  7. is64 = False # in case the user has an old version of adodbapi
  8. if is64:
  9. driver = "Microsoft.ACE.OLEDB.12.0"
  10. else:
  11. driver = "Microsoft.Jet.OLEDB.4.0"
  12. filename = "xx.xls" # file will be created if it does not exist
  13. extended = 'Extended Properties="Excel 8.0;Readonly=False;"'
  14. constr = "Provider=%s;Data Source=%s;%s" % (driver, filename, extended)
  15. conn = adodbapi.connect(constr)
  16. with conn: # will auto commit if no errors
  17. with conn.cursor() as crsr:
  18. try:
  19. crsr.execute("drop table SheetOne")
  20. except:
  21. pass # just is case there is one already there
  22. # create the sheet and the header row and set the types for the columns
  23. crsr.execute(
  24. "create table SheetOne (Name varchar, Rank varchar, SrvcNum integer, Weight float, Birth date)"
  25. )
  26. sql = "INSERT INTO SheetOne (name, rank , srvcnum, weight, birth) values (?,?,?,?,?)"
  27. data = ("Mike Murphy", "SSG", 123456789, 167.8, datetime.date(1922, 12, 27))
  28. crsr.execute(sql, data) # write the first row of data
  29. crsr.execute(
  30. sql, ["John Jones", "Pvt", 987654321, 140.0, datetime.date(1921, 7, 4)]
  31. ) # another row of data
  32. conn.close()
  33. print("Created spreadsheet=%s worksheet=%s" % (filename, "SheetOne"))