Home | Forums | Contact | Search | Syndication  
 
 [login] [create account]   Thursday, August 28, 2025 
 
slxdeveloper.com Community Forums  
   
The Forums on slxdeveloper.com are now retired. The forum archive will remain available for the time being. Thank you for your participation on slxdeveloper.com!
 Architect Forums - SalesLogix Scripting & Customization
Forum to discuss writing script in Architect plugins for SalesLogix & general SalesLogix customization topics (for Windows client only). View the code of conduct for posting guidelines.
Forums RSS Feed


 Back to Forum List | Back to SalesLogix Scripting & Customization | New ThreadView:  Search:  
 Author  Thread: Faster export to excel Script
RJ Samp
Posts: 973
Top 10 forum poster: 973 posts
 
Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 22 Jul 08 1:07 PM
This replaces the SLX EXCEL dumper......6500 rows in 26 seconds instead of over an HOUR!

'*********************************************************************************************************
' Description: Export Grid or RS To Excel
' Purpose : Export data from a grid or recordset into an excel spreedsheet
' If grid object data will be displayed in excel exactly as what appears in the grid, any
' columns that are hidden will remain hidden
' If RS object data will be desplayed in excel exactly as what appears in RS, using a RS,
' however, allows the ability to customize the data prior to calling ExportRSToExcel
' Inputs : Grid object or Recordset object
' Written : 06/24/03
' Updates : November 2007, July 2008 RJ Samp played with the Grid Output.
'*********************************************************************************************************

option explicit
Dim objExcel 'Excel object
Dim blnCurrencySymbol 'Whether or not we display the currency symbol for appropriate data types

'''' TxDataGridColumnType
CONST ctStandard = 0
CONST ctMask = 1
CONST ctButton = 2
CONST ctDate = 3
CONST ctCheck = 4
CONST ctImage = 5
CONST ctSpin = 6
CONST ctCombo = 8
CONST ctCalculator = 9
CONST ctPictureBlob = 10
CONST ctHyperlink = 11
CONST ctTime = 12
CONST crCurrency = 13
CONST ctMemo = 14
CONST ctPickList = 15
''' TxAlignment
CONST taLeftJustify = 0
CONST taRightJustify = 1
CONST taCenter = 2
''' TxTextAlignment
CONST tlTop = 0
CONST tlCenter = 1
CONST tlBottom = 2

CONST ftNone = 0
CONST ftFixed = 1
CONST ftInteger = 2
CONST ftDateTime = 3
CONST ftPerCent = 4
CONST ftCurrency = 5
CONST ftUser = 6
CONST ftPhone = 7
CONST ftOwner = 8
CONST ftBoolean = 9

''RJ Samp July 2008
CONST BigReportSize = 25
CONST MaxExcelRows = 65000
DIm TimeStart
DIM TimeEnd
DIm TimeUsed
DIM ExportRows
DIM nCols
DIM nRows
DIM MTEXT, RowText

Sub Main
'Initialize
blnCurrencySymbol = True
End Sub

Sub ExportGridToExcel(objGrid, SheetName)
Dim intRow, intCol, intRecordCount
Dim aRows, aColCaptions
Dim objRS

'Get the RS from the Grid
Set objRS = objGrid.Recordset

'Make sure there are records
If objRS.RecordCount < 1 Then Exit Sub

'Move to the first record
objRS.MoveFirst

'Get an array of visible FieldNames in the grid, then get only those fields from the RS
aRows = objRS.GetRows(,, GetArrayVisibleColumnNames(objGrid))

'Get the number of records
intRecordCount = UBound(aRows, 2) + 1
ExportRows = intRecordCount
'Create reference variable for the spreadsheet. If Excel isn't found exit sub
If Not CreateExcelObject Then Exit Sub
if intRecordCount > BigReportSize THEN MSGBOX "Wait one, creating spreadsheet for " & intRecordCount & " rows."
TIMESTART = Timer
objExcel.Visible = FALSE
objExcel.Workbooks.Add

'Ensure that Excel remains visible if we switch to the Active Sheet
SetExcelActiveSheet(objExcel)
'Set the SpreedSheet name, if specified
If SheetName <> "" Then
'Sheet name can be no more than 31 characters
objExcel.Sheets(1).Name = LEFT(TRIM("" & SheetName),31)
End If
'''''
'Format the Column header
'Get array of visible captions in the grid as will be displayed in Excel
aColCaptions = GetArrayVisibleColumnCaptions(objGrid)
intRow = 1
intCol = 1
For intCol = 1 To UBound(aRows, 1) + 1
'''' RJ Samp
objExcel.Columns(intCol).NumberFormat = "General" ''' GetFormatType(objGrid.Columns.Item(intCol).Format) '''Not Exposed in 6.1??
objExcel.Columns(intCol).VerticalAlignment = -4160 'Align Top
With objExcel.Cells(intRow, intCol)
.Value = aColCaptions(intCol - 1)
IF instr(1,.VALUE, "Grade",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "@"
IF .Value = "PID" or .VALUE = "Postalcode" or .VALUE = "Postal code" THEN
objExcel.Columns(intCol).NumberFormat = "General"
END IF
IF instr(1,.VALUE, "Potential $",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "$##,###.00;[Red]($##,###.00)"
IF instr(1,.VALUE, "Discount",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "##,##0.00% "
IF instr(1,.VALUE, "%",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "##,##0.00% "
IF instr(1,.VALUE, "Enroll",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "##,##0 "
IF instr(1,.VALUE, "Students",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "##,##0 "
IF instr(1,.VALUE, "Schools",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "##,##0 "
IF instr(1,.VALUE, "Price",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "$##,###.00;[Red]($##,###.00)"
IF instr(1,.VALUE, "$",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "$##,###.00;[Red]($##,###.00)"
IF instr(1,.VALUE, "Amount",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "$##,###.00;[Red]($##,###.00)"
IF instr(1,.VALUE, "Date",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "mm/dd/yyyy"
IF instr(1,.VALUE, "Start",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "mm/dd/yyyy"
IF instr(1,.VALUE, "End",1) > 0 THEN objExcel.Columns(intCol).NumberFormat = "mm/dd/yyyy"
'''''''__________________________________________________
With .Font
.Name = "Arial" 'DNL
.Bold = True
.Size = 9
End With
End With
Next
IF EXPORTROWS > MaxExcelROWS - 2 THEN EXPORTROWS = MaxExcelROWS - 2
if intRecordCount <= BigReportSize THEN

'Add records
With objExcel
For intRow = 2 To intRecordCount + 1
For intCol = 1 To UBound(aRows, 1) + 1
.Cells(intRow, intCol).Value = aRows(intCol - 1, intRow - 2)
Next
Next
End With
ELSE
'Build up a massive string of Data + TAB + DATA + TAB + EOL for all data rows.
MText = ""
For intRow = 2 To EXPORTROWS + 1
RowText = ""
For intCol = 1 To UBound(aRows, 1) + 1
RowText = RowText & aRows(intCol - 1, intRow - 2) & CHR(9)
NEXT
MText = MText & RowText & CHR(13)
NEXT

''''put the string into the windows clipboard.
DIM oSHELL, objHTM
set oShell = CreateObject("WScript.Shell")
Set objHTM = CreateObject("htmlfile")

objHTM.ParentWindow.ClipboardData.SetData "text", MText
'paste it into Excel.
objExcel.Range("A2").SELECT
objExcel.ActiveSheet.Paste
END IF
objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
objExcel.Visible = True
TIMEEND = Timer
Erase aColCaptions
Set objExcel = Nothing
TimeUsed = (TimeEnd - TimeStart)
MSGBOX "Grid exported to Excel, formatted, in: " & TIMEUSED & " seconds. "
End Sub

Sub ExportRSToExcel(objRS, SheetName)
Dim intRow, intCol, intRecordCount
Dim aRows

On Error Resume Next
'Move to the first record
objRS.MoveFirst

'Read all of the records into an array
aRows = objRS.GetRows()

'Get the number of fields and records
intRecordCount = UBound(aRows, 2) + 1
ExportRows = intRecordCount - 1
'Create reference variable for the spreadsheet. If Excel isn't found exit sub
If Not CreateExcelObject Then Exit Sub
if intRecordCount > BigReportSize THEN MSGBOX "Wait one, creating spreadsheet for " & intRecordCount & " rows."
TIMESTART = Timer
objExcel.Visible = FALSE
objExcel.Workbooks.Add

'Ensure that Excel remains visible if we switch to the Active Sheet
SetExcelActiveSheet(objExcel)
'Set the SpreedSheet name, if specified
If SheetName <> "" Then
'Sheet name can be no more than 31 characters
objExcel.Sheets(1).Name = LEFT(TRIM("" & SheetName),31)
End If

'Place the names of the fields in the column headers
'''' RJ Samp
dim strName
DIM J, strALIAS
For intCol = 1 To UBound(aRows, 1) + 1
objExcel.Columns(intCol).VerticalAlignment = -4160 'Align Top
objExcel.Columns(intCol).NumberFormat = GetDBFormatType(objRS.Fields.Item(intCol - 1).Type)
With objExcel.Cells(1, intCol)
strName = Application.Translator.Localize(objRS.Fields(intCol - 1).Name)
FOR J = 2 TO 20
strALIAS = "A" & cSTR(J) & "_"
strName = Replace(strName,strALIAS, "")
NEXT
strName = Replace(strName,"_", " ")
.Value = objExcel.PROPER(strName)
With .Font
.Name = "Arial" 'DNL
.Bold = True
.Size = 9
End With
End With
Next

'Add data
IF EXPORTROWS > MaxExcelROWS - 2 THEN EXPORTROWS = MaxExcelROWS - 2
if intRecordCount <= BigReportSize THEN

'Add records
With objExcel
For intRow = 2 To intRecordCount + 1
For intCol = 1 To UBound(aRows, 1) + 1
.Cells(intRow, intCol).Value = aRows(intCol - 1, intRow - 2)
Next
Next
End With
ELSE
'Build up a massive string of Data + TAB + DATA + TAB + EOL for all data rows.
MText = ""
For intRow = 2 To EXPORTROWS + 1
RowText = ""
For intCol = 1 To UBound(aRows, 1) + 1
RowText = RowText & aRows(intCol - 1, intRow - 2) & CHR(9)
NEXT
MText = MText & RowText & CHR(13)
NEXT

''''put the string into the windows clipboard.
DIM oSHELL, objHTM
set oShell = CreateObject("WScript.Shell")
Set objHTM = CreateObject("htmlfile")

objHTM.ParentWindow.ClipboardData.SetData "text", MText
'paste it into Excel.
objExcel.Range("A2").SELECT
objExcel.ActiveSheet.Paste
END IF

objExcel.Cells(1, 1).CurrentRegion.EntireColumn.AutoFit
objExcel.Visible = True
TIMEEND = Timer
Set objExcel = Nothing
On Error Goto 0
TimeUsed = (TimeEnd - TimeStart)
MSGBOX "RecordSet exported to Excel in: " & TIMEUSED & " seconds. "
End Sub

'Create reference variable for the spreadsheet if Excel is found, if not display message.
Function CreateExcelObject
Dim blnExcel

blnExcel = True
On Error Resume Next
'Create reference variable for the spreadsheet
Set objExcel = CreateObject("Excel.Application") 'DNL
If Not IsObject(objExcel) Then
MsgBox Application.Translator.Localize("Microsoft Excel cannot be found. ") & vbCrLf & vbCrLf & _
Application.Translator.Localize("Please insure Excel is installed prior to using this feature."), vbExclamation, "SalesLogix"
blnExcel = False
End If

CreateExcelObject = blnExcel
End Function

Sub SetExcelActiveSheet(objExcel)
Dim objTemp

Set objTemp = objExcel
If objExcel.Application.Version >= 8 Then
Set objExcel = objExcel.ActiveSheet
End If

Set objTemp = Nothing
End Sub

Function GetArrayVisibleColumnNames(objGrid)
Dim aColumns()
Dim i, intColumn

intColumn = 0
'Get the number of visible columns on the grid
ReDim aColumns(GetVisibleColumnsCount(objGrid))
For i = 0 To objGrid.Columns.Count - 1
'For each visible column add to array
If objGrid.Columns.Item(i).Visible Then
aColumns(intColumn) = objGrid.Columns.Item(i).FieldName
intColumn = intColumn + 1
End If
Next

GetArrayVisibleColumnNames = aColumns

Erase aColumns
End Function

Function GetArrayVisibleColumnCaptions(objGrid)
Dim aColumns()
Dim intColumn
Dim i

intColumn = 0
'Get the number of visible columns on the grid
ReDim aColumns(GetVisibleColumnsCount(objGrid))
For i = 0 To objGrid.Columns.Count - 1
'For each visible column add to array
If objGrid.Columns.Item(i).Visible Then
aColumns(intColumn) = objGrid.Columns.Item(i).Caption
intColumn = intColumn + 1
End If
Next
GetArrayVisibleColumnCaptions = aColumns
End Function

Function GetVisibleColumnsCount(objGrid)
Dim i, intCount

'Loop through the Columns collection of the grid to get count of visible columns
intCount = 0
For i = 0 To objGrid.Columns.Count - 1
If objGrid.Columns.Item(i).Visible Then
intCount = intCount + 1
End If
Next

GetVisibleColumnsCount = intCount - 1
End Function

Function GetFormatType(strFormatType)
Select Case strFormatType
Case 2 'Integer
GetFormatType = "#,##0" 'DNL
Case 5 'Currency'
GetFormatType = FormatCurrencyString
Case 10 ' Pos Integer
GetFormatType = "#,##0" 'DNL
Case Else
GetFormatType = ""
End select
End Function

Function GetDBFormatType(strFormatType)
Select Case strFormatType
Case 3 'adInteger
GetDBFormatType = "#,##0" 'DNL
Case 4 'adSingle
GetDBFormatType = FormatCurrencyString
Case 5 'adDouble
GetDBFormatType = FormatCurrencyString
Case 6 'adCurrency'
GetDBFormatType = FormatCurrencyString
Case 11 'adBoolean
GetDBFormatType = "#,##0" 'DNL
Case 131 'adNumeric
GetDBFormatType = "#,##0" 'DNL
Case Else
GetDBFormatType = ""
End select
End Function

Function FormatCurrencyString
If Not blnCurrencySymbol Then
FormatCurrencyString = "#,##0.00" 'DNL
Else
Select Case Application.Translator.CurrencyFormat
Case 0
FormatCurrencyString = Application.Translator.CurrencyString & "#,##0.00" 'DNL
Case 3
FormatCurrencyString = "#,##0.00 " + Application.Translator.CurrencyString 'DNL
Case Else
FormatCurrencyString = Application.Translator.CurrencyString + " #,##0.00" 'DNL
End Select
End If
End Function

'Looks for duplicate records based on specified Fields, if found enters an empty string as the new value
'Paramaters: objRS - Recordset object
' aFieldNames - Array of Fields to be searched
Function RemoveDuplicateValue(objRS, aFieldNames)
Dim strValue, Field
Dim i

With objRS
.MoveFirst
For Each Field In aFieldNames
For i = 0 To .RecordCount - 1
If .Fields.Item(Field).Value = strValue Then
.Fields.Item(Field).Value = ""
End If
strValue = .Fields.Item(Field).OriginalValue
.MoveNext
Next
.MoveFirst
Next
End With
End Function

'Loops through RS for the specified value, if found replaces old value with the specified new value
'Paramaters: objRS - Recordset object
' aFieldNames - Array of Fields to be searched
' strValue - Value to be searched and replaced if found
' strNewValue - New value of strValue if found
Function RecordsetReplaceValues(objRS, aFieldNames, strValue, strNewValue)
Dim Field
Dim i

With objRS
.MoveFirst
For i = 0 To .RecordCount - 1
For Each Field In aFieldNames
If Not IsNull(.Fields.Item(Field).Value) Then
.Fields.Item(Field).Value = Replace(.Fields.Item(Field).Value, strValue, strNewValue)
End If
Next
.MoveNext
Next
End With

Set RecordsetReplaceValues = objRS
End Function

Sub DisplayCurrencySymbol(blnValue)
blnCurrencySymbol = blnValue
End Sub
[Reply][Quote]
Steve Robertson
Posts: 146
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 22 Jul 08 5:32 PM
Thanks RJ!

Cheers,

Steve
[Reply][Quote]
Phil Parkin
Posts: 819
Top 10 forum poster: 819 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 22 Jul 08 8:18 PM
This kicks arse - excellent.

The worst thing - I'm assuming that your original code does not look like this. We need something like <script> </script> tags to put around stuff like this that will preserve indenting. My pasted code is almost unreadable.

Phil
[Reply][Quote]
Bob (RJ)Ledger
Posts: 1103
Top 10 forum poster: 1103 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 23 Jul 08 8:01 AM
the "pre" and "/pre" tags should work.
ex:

Function GetFormatType(strFormatType)
Select Case strFormatType
Case 2 'Integer
GetFormatType = "#,##0" 'DNL
Case 5 'Currency'
GetFormatType = FormatCurrencyString
Case 10 ' Pos Integer
GetFormatType = "#,##0" 'DNL
Case Else
GetFormatType = ""
End select
End Function


--
RJLedger - rjlSystems
"...Customer First..."
[Reply][Quote]
Ryan Farley
Posts: 2265
slxdeveloper.com Site Administrator
Top 10 forum poster: 2265 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 23 Jul 08 1:07 PM
Quote:
Originally posted by Phil Parkin

The worst thing - I'm assuming that your original code does not look like this. We need something like <script> </script> tags to put around stuff like this that will preserve indenting. My pasted code is almost unreadable.


I've edited RJ's post to include the pre tags so it doesn't loose it's formatting.
[Reply][Quote]
RJ Samp
Posts: 973
Top 10 forum poster: 973 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 23 Jul 08 2:03 PM
You'll note that I use a bunch of IF INSTR on each Caption Value to help determine the Excel Format for each column....you can mess around with the values and add / subtract statements as necessary....

But it would be better if somehow I could get the DataBase Type, FormatType, FormatString from each Grid Column.....or Database Field and Database Table from a SQL statement (to use in the RecordSet dumper to go out and get the DisplayName from SECTABLEDEFS for each field!).

Is there a VB Script parsing program for SQL statements???? Can I get the FormatType, FormatString for each column in a grid (I know I can get FieldName....comes back in the ALIAS format, i.e.: A1.ACCOUNTID = ACCOUNTID, A2.ACCOUNTID = A2_ACCOUNTID).
[Reply][Quote]
Phil Parkin
Posts: 819
Top 10 forum poster: 819 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 23 Jul 08 4:29 PM
Nice work, thanks Ryan!

Phil
[Reply][Quote]
alexUA
Posts: 23
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 06 May 09 9:31 AM
Hi! How to open xls file in slx, C:\test.xls?
[Reply][Quote]
Raul A. Chavez
Posts: 1300
Top 10 forum poster: 1300 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 06 May 09 10:15 AM
Although I don't have the code handy, I had use a method of excel to copy the Recordset onto the sheet: CopyFromRecordset.
The problem with this approach is that the data was "flipped", so Columns come up as Rows and viceversa.

Never the less, it was quite fast for me to setup the Headers, setup the Formatting, use the CopyFromRecordset to quickly move data into Excel, and then just Transposed the table around.

This approach did export large Groups and Grids quite fast, but since it has been at least 2 years since I implemented, I don't have any numbers to compare performance.
[Reply][Quote]
Phil Parkin
Posts: 819
Top 10 forum poster: 819 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 07 May 09 3:47 AM
What exactly do you mean by 'open'? Do you want to import the data? Or just fire up Excel and open that file?
[Reply][Quote]
alexUA
Posts: 23
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 07 May 09 3:56 AM
No I dont need import any data. I just want to open existing file from local drive: "C:\1!.xls"

Dim sPath
sPath = "C:\1!.xls"
Set objExcel = CreateObject("Excel.Application")
Err.Clear
objExcel.Open sPath

doesnt work
[Reply][Quote]
Phil Parkin
Posts: 819
Top 10 forum poster: 819 posts
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 07 May 09 4:16 AM
Try this. Here's an 'open file' sub:

Sub OpenFile(strFile)
Dim objShell

Set objShell = CreateObject("Shell.Application")
objShell.ShellExecute strFile, "", "", "open", 1
Set objShell = Nothing
End Sub


Then, to open your Excel doc, just use this:

OpenFile "C:\1!.xls"
[Reply][Quote]
alexUA
Posts: 23
 
Re: Faster export to excel ScriptYour last visit to this thread was on 1/1/1970 12:00:00 AM
Posted: 07 May 09 6:31 AM
Thank you very match!
[Reply][Quote]
 Page 1 of 1 
  You can subscribe to receive a daily forum digest in your user profile. View the site code of conduct for posting guidelines.

   Forum RSS Feed - Subscribe to the forum RSS feed to keep on top of the latest forum activity!
 

 
 slxdeveloper.com is brought to you courtesy of Ryan Farley & Customer FX Corporation.
 This site, and all contents herein, are Copyright © 2025 Customer FX Corporation. The information and opinions expressed here are not endorsed by Sage Software.

code of conduct | Subscribe to the slxdeveloper.com Latest Article RSS feed
   
 
page cache (param): 8/28/2025 5:58:04 PM