Jump to content

[Solved] Excel to Text File with delimiter added


Recommended Posts

Hi Experts,

I just want to ask if this is possible to do with AutoIt.

Q: Can Excel file be converted into a text file and add with "|" as delimiter in every entry of a row?

Excel.PNG

And the text file would be:

Jhon Jhonson|Operation|50-A|Currently have pending items for production|jJhonson

Is this possible? I was not able create a code that do this yet, I just want to know if this can be done or any link that put me to start.:sweating:

 

Thanks in advance, Experts.

KS15

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Excel has no native function to save to a text file and specify the delimiter (it only supports CSV).
But you could read the whole Excel worksheet into an array and then save the array to a text file by adding the needed delimiter.

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

Right, good point Water. I'll check it with array and then convert it to text file, might do the trick.^_^ I'll just ask help after I've coded it and if it works or not. Thanks!

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@water,

I tried using the sample in help file and I found something like this:

#include <Array.au3>
#include <File.au3>
#include <Excel.au3>
#include <FileConstants.au3>
#include <WinAPIFiles.au3>
#include <MsgBoxConstants.au3>

Local $oExcel = _Excel_Open()
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error!", "Error creating the Excel application object." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
Local $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsx")
If @error Then
    MsgBox($MB_SYSTEMMODAL, "Error!", "Error opening workbook '" & @ScriptDir & "\Test.xlsx" & @CRLF & "@error = " & @error & ", @extended = " & @extended)
    _Excel_Close($oExcel)
    Exit
EndIf

Local $aResult = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("A:N"), 1)
_Excel_Close($oExcel)
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "Error!", "Error reading from workbook." & @CRLF & "@error = " & @error & ", @extended = " & @extended)
   $file = _ArrayDisplay($aResult, "Displaying Excel Data")
Local $Path = @ScriptDir & "\Excel.txt"
Local $afile = FileOpen($Path, 1)
   FileWrite($afile, $aResult)
   FileClose($afile)
ShellExecute($Path)

I can display the excel data using "_ArrayDisplay" and trying to combine it with FileWrite() function to extract the array being displayed. However, I've got nothing in return. I also tried using "_ArrayExtract($file, 1, 2, 2, 3)" and save it to text file but I've got -1 in return.

Now all I want is how can I save to text file this "_ArrayDisplay", have it tried using "$aResult[0]".

Edited by KickStarter15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

Something for you to play with:

#include <Excel.au3>
Global $sLine = ""
Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\test.xlsx")
Global $aContent = _Excel_RangeRead($oWorkbook)
_Arraydisplay($aContent)
For $i = 0 To UBound($aContent, 1) - 1
    $sLine = ""
    For $j = 0 To UBound($aContent, 2) - 1
        If $j = 0 Then
            $sLine = $aContent[$i][$j]
        Else
            $sLine = $sLine & "|" & $aContent[$i][$j]
        EndIf
    Next
    ConsoleWrite($sLine & @CRLF)
Next

Replace ConsoleWrite with FileWriteLine and you should be set.

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

@water,

Thanks, that's to simple as I thought ^_^.  However, when the cell entry has multiple data like below screengrab, it will generate to another line in text file.

Cell entry.PNG

And the output in text file would be:

Jhon Jhonson|Operation|50-A|Currently have pending items for production|jJhonson
David Morse|Staff|10-C|
- items has been released
- Continuous Improvement|jJhonson

Which, should be like this:

Jhon Jhonson|Operation|50-A|Currently have pending items for production|jJhonson
David Morse|Staff|10-C|- items has been released - Continuous Improvement|jJhonson

I tried using StringReplace() but could not perfectly done how.

In addition to that, how can I control the row count to be displayed in an array. Like, i only want to generate [290] for row and [20] for column.:sweating:

Edited by KickStarter15
Edited for additional:

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

How did you use StringReplace? That's the approach I would use as well. Could you please post the line of code?

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

@water,

Apology, I was off the office the time you responded yesterday. Anyway, I just followed how it was done in help file.

Local $Path = @ScriptDir & "\Excel.txt"
   Local $file = FileOpen($Path, 1)
   FileWriteLine($file, $sLine&@CRLF)
   StringReplace($file, @CRLF, "") ; can replace the space with no space and close-up but can't run-in as one line in text output.
   FileClose($file)

Still having below output. Space was replace with no space but was not merged as one line.

David Morse|Staff|10-C|
- items has been released.
- Continuous Improvement|jJhonson

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

@water,

Apology, I was off the office the time you responded yesterday. Anyway, I just followed how it was done in help file.

Local $Path = @ScriptDir & "\Excel.txt"
   Local $file = FileOpen($Path, 1)
   FileWriteLine($file, $sLine&@CRLF)
   StringReplace($file, @CRLF, "") ; can replace the space with no space and close-up but can't run-in as one line in text output.
   FileClose($file)

Still having below output. Space was replace with no space but was not merged as one line.

David Morse|Staff|10-C|
- items has been released.
- Continuous Improvement|jJhonson

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

I would insert the StringReplace before creating the output file:

#include <Excel.au3>
Global $sLine = ""
Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\test.xlsx")
Global $aContent = _Excel_RangeRead($oWorkbook)
_Arraydisplay($aContent)
For $i = 0 To UBound($aContent, 1) - 1
    $sLine = ""
    For $j = 0 To UBound($aContent, 2) - 1
        $aContent[$i][$j] = StringReplace($aContent[$i][$j], @CRLF, "")
        If $j = 0 Then
            $sLine = $aContent[$i][$j]
        Else
            $sLine = $sLine & "|" & $aContent[$i][$j]
        EndIf
    Next
    ConsoleWrite($sLine & @CRLF)
Next

 

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

Almost: multiline cells use LF alone, not CRLF.

$aContent[$i][$j] = StringRegExpReplace($aContent[$i][$j], "(?<!\r)\n", "#")

I replaced LFs by # to display what was replaced, but should be a whitespace or empty string or anything else.

This wonderful site allows debugging and testing regular expressions (many flavors available). An absolute must have in your bookmarks.
Another excellent RegExp tutorial. Don't forget downloading your copy of up-to-date pcretest.exe and pcregrep.exe here
RegExp tutorial: enough to get started
PCRE v8.33 regexp documentation latest available release and currently implemented in AutoIt beta.

SQLitespeed is another feature-rich premier SQLite manager (includes import/export). Well worth a try.
SQLite Expert (freeware Personal Edition or payware Pro version) is a very useful SQLite database manager.
An excellent eBook covering almost every aspect of SQLite3: a must-read for anyone doing serious work.
SQL tutorial (covers "generic" SQL, but most of it applies to SQLite as well)
A work-in-progress SQLite3 tutorial. Don't miss other LxyzTHW pages!
SQLite official website with full documentation (may be newer than the SQLite library that comes standard with AutoIt)

Link to comment
Share on other sites

@water,

That wasn't so hard after all.:D Wow,  I was amazed with that small codes you've made. Will, I need to study more. Thanks Water, it works perfectly.

 

@jchd, Thanks, as well. I already replaced it with space to have the correct output.

$aContent[$i][$j] = StringReplace($aContent[$i][$j], @CRLF, " ")

 

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

If someone want it, here's the compiled code.:sweating:

#include <Array.au3>
#include <Excel.au3>
Global $sLine = ""
Global $oExcel = ObjCreate("Excel.Application") ; hide excel in opening
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Test.xlsx")
Global $aContent = _Excel_RangeRead($oWorkbook)
Local $aArray_2D[293][14] ; declaration of row and column
For $i = 0 To UBound($aArray_2D, 1) - 1
    $sLine = ""
    For $j = 0 To UBound($aArray_2D, 2) - 1
       $aContent[$i][$j] = StringReplace($aContent[$i][$j], @CRLF, " ")
        If $j = 0 Then
            $sLine = $aContent[$i][$j]
        Else
            $sLine = $sLine & "|" & $aContent[$i][$j]
         EndIf
     Next
   Local $Path = @ScriptDir & "\Excel.txt"
   Local $file = FileOpen($Path, 1)
   FileWriteLine($file, $sLine&@CRLF)
   FileClose($file)
   _Excel_Close($oExcel, Default, True)
Next

Thanks, to water...

 

KS15

Programming is "To make it so simple that there are obviously no deficiencies" or "To make it so complicated that there are no obvious deficiencies" by C.A.R. Hoare.

Link to comment
Share on other sites

:)

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...