KickStarter15

[Solved] Excel to Text File with delimiter added

15 posts in this topic

#1 ·  Posted (edited)

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

Share this post


Link to post
Share on other sites



#2 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#3 ·  Posted

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!

Share this post


Link to post
Share on other sites

#4 ·  Posted (edited)

@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

Share this post


Link to post
Share on other sites

#5 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

@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:

Share this post


Link to post
Share on other sites

#7 ·  Posted

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 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#8 ·  Posted

@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

Share this post


Link to post
Share on other sites

#9 ·  Posted

@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

Share this post


Link to post
Share on other sites

#10 ·  Posted

Opsss sorry for that.... got log in my connection...:sweating:

Share this post


Link to post
Share on other sites

#11 ·  Posted

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

 

1 person likes this

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#12 ·  Posted

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.

1 person likes this

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)

Share this post


Link to post
Share on other sites

#13 ·  Posted

@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, " ")

 

Share this post


Link to post
Share on other sites

#14 ·  Posted

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

Share this post


Link to post
Share on other sites

#15 ·  Posted

:)


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
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