Jump to content

_Excel_BookNew($oExcel) DateTime issue - (Moved)


 Share

Recommended Posts

I wrote an application that reads a User's full name, their Employee number, where they work and a Date/Time, which is written to an SQL Database,
This works great, now I am working on another app that reads the SQL database into a 2DArray.  This part of it appears to be working.

Then I am using the _Excel_BookNew($oExcel) to send the data to the array and then to an Excel Spreadsheet.
This is working with the exception of the date/Time field.

Below are some screenshots of what I am seeing;

This is from the SQL Database -

 image.png.aa79fc2b0d018240d358d73b297e4a03.png

This is the 2D Array.  I think this is where the issue is. - 
image.png.0156e8505a8c5b14cb363f1237564865.png

And this is what I see in Excel - 

image.png.c966f74d8d96e52e0a27335bf5447b8a.png

I don't/ know if it is my database insertion of Date/Time, or it is the conversion from Array to Excel.

This is my code for insertion into the database with the line creating the variable for Date/Time;

Local $datetime = _DateTimeFormat(_NowCalc(), 0)
               local $RecAdd =_SQL_Execute(-1,"INSERT INTO dbo.Work_Location (Zone,FullName,EmployeeID,WorkLocation,Date) VALUES ('" & $Zone & "' ,'" & $FullName & "' ,'" & $EmpID & "' ,'Office','" & $datetime & "');")

image.png.ae36af1d4ada63f1c79acab81f7136b8.pngimage.png.ae36af1d4ada63f1c79acab81f7136b8.pngHere is my code to read the database into the 2DArray and then export to Excel.

Local $aData,$iRows,$iColumns;Variables to store the array data in to and the row count and the column count
    $GetDB = _SQL_GetTable2D(-1,"SELECT * FROM dbo.Work_Location;",$aData,$iRows,$iColumns)
      If $GetDB = $SQL_OK then _arrayDisplay($aData,"2D (" & $iRows & " Rows) (" & $iColumns & " Columns)"  )
      $oExcel = _Excel_Open(False)
      $oBook = _Excel_BookNew($oExcel)
      _Excel_RangeWrite($oBook,Default,$aData)
      _Excel_BookSaveAs($oBook,@ScriptDir & '\test5.xlsx',$xlOpenXMLWorkbook)
      _Excel_Close($oExcel)

Anyone have any thoughts on this?

Thank you in advance for your assistance.

Link to comment
Share on other sites

  • Moderators

Moved to the appropriate AutoIt General Help and Support forum, as the Developer General Discussion forum very clearly states:

Quote

General development and scripting discussions.


Do not create AutoIt-related topics here, use the AutoIt General Help and Support or AutoIt Technical Discussion forums.

Moderation Team

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

You'll need to add the formatting back before you write to Excel. The format you can see in your database works for excel as well. Right now, it looks to excel like you're entering a very large number because there's no date separation. You can either find a way that reading from the database returns the format to AutoIt or you can fix it in AutoIt with a series of StringSplits on each row of data

All my code provided is Public Domain... but it may not work. ;) Use it, change it, break it, whatever you want.

Spoiler

My Humble Contributions:
Personal Function Documentation - A personal HelpFile for your functions
Acro.au3 UDF - Automating Acrobat Pro
ToDo Finder - Find #ToDo: lines in your scripts
UI-SimpleWrappers UDF - Use UI Automation more Simply-er
KeePass UDF - Automate KeePass, a password manager
InputBoxes - Simple Input boxes for various variable types

Link to comment
Share on other sites

Maybe this :

#include <Excel.au3>

Local $aData = [[1, "Douglas", 12345, "Office", 20220311140853]]

 Local $oExcel = _Excel_Open()
 Local $oBook = _Excel_BookNew($oExcel)
 
 $aData[0][4] = StringRegExpReplace(String($aData[0][4]),"(.{4})(.{2})(.{2})(.{2})(.{2})(.{2})","$1-$2-$3 $4:$5:$6")
 $oBook.Activesheet.Columns("E").ColumnWidth = 20
 $oBook.Activesheet.Columns("E").NumberFormat = "yyyy-mm-dd hh:mm:ss"

 _Excel_RangeWrite($oBook,Default,$aData)

 

Edited by Nine
Link to comment
Share on other sites

More details about formatting an Excel Range can be found in the wiki.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2022-02-19 - Version 1.6.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2017-07-21 - Version 0.4.0.1) - Download - General Help & Support - Example Scripts
OutlookEX (2021-11-16 - Version 1.7.0.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX_GUI (2021-04-13 - Version 1.4.0.0) - Download
Outlook Tools (2019-07-22 - Version 0.6.0.0) - Download - General Help & Support - Wiki
PowerPoint (2021-08-31 - Version 1.5.0.0) - Download - General Help & Support - Example Scripts - Wiki
Task Scheduler (NEW 2022-07-28 - Version 1.6.0.1) - Download - General Help & Support - Wiki

Standard UDFs:
Excel - Example Scripts - Wiki
Word - Wiki

Tutorials:
ADO - Wiki
WebDriver - Wiki

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...