Jump to content

FileReadLine & FileWrite To _FileWriteFromArray (Slow To Fast)


Go to solution Solved by Nine,

Recommended Posts

Hello
I Get 5 text files From Excel file
Then I will have More then 36000 Lines in 5 Files TXT
I need specific range 

I use this code :

#include <Excel.au3>
#include <File.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir&"\Price.xlsx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening")
     _FileWriteFromArray(@ScriptDir&"\Tmp_Price1.txt",_Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("B")), 0)
         _FileWriteFromArray(@ScriptDir&"\Tmp_Price2.txt",_Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("D")), 0)
             _FileWriteFromArray(@ScriptDir&"\Tmp_CODE.txt",_Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("F")), 0)
                 _FileWriteFromArray(@ScriptDir&"\Tmp_PriceDate.txt",_Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("J")), 0)
                     _FileWriteFromArray(@ScriptDir&"\Tmp_Price3.txt",_Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("L")), 0)

$Price1 = @ScriptDir&"\Tmp_Price1.txt"
$Price2 = @ScriptDir&"\Tmp_Price2.txt"
$CODE = @ScriptDir&"\Tmp_CODE.txt"
$PriceDate = @ScriptDir&"\Tmp_PriceDate.txt"
$Price3 = @ScriptDir&"\Tmp_Price3.txt"
            For $i = 1 to _FileCountLines($PriceDate)
                If FileReadLine($PriceDate, $i) = 0 Then
                    If FileReadLine($CODE, $i) = 2 Then
                        FileWrite(@ScriptDir&"\Revdr1.txt",FileReadLine($Price1, $i)&@CRLF)
                        FileWrite(@ScriptDir&"\Revdr2.txt",FileReadLine($Price2, $i)&@CRLF)
                        FileWrite(@ScriptDir&"\Revdr3.txt",FileReadLine($Price3, $i)&@CRLF)
                    EndIf
                    If FileReadLine($CODE, $i) = 3 Then
                        FileWrite(@ScriptDir&"\Cons1.txt",FileReadLine($Price1, $i)&@CRLF)
                        FileWrite(@ScriptDir&"\Cons2.txt",FileReadLine($Price2, $i)&@CRLF)
                        FileWrite(@ScriptDir&"\Cons3.txt",FileReadLine($Price3, $i)&@CRLF)
                    EndIf
                EndIf
            Next

This take a long time
Please any fast other way
thanx

 

Price.xlsx

Link to comment
Share on other sites

  • Solution

Maybe this :

#include <Excel.au3>
#include <File.au3>

Global $oExcel = _Excel_Open()
Global $oWorkbook = _Excel_BookOpen($oExcel, @ScriptDir & "\Price.xlsx")
If @error Then Exit MsgBox($MB_SYSTEMMODAL, "", "Error opening")

Local $aExcel = _Excel_RangeRead($oWorkbook, Default, $oWorkbook.ActiveSheet.Usedrange.Columns("B:L"))

$hRevdr1 = FileOpen(@ScriptDir & "\Revdr1.txt", $FO_OVERWRITE)
$hRevdr2 = FileOpen(@ScriptDir & "\Revdr2.txt", $FO_OVERWRITE)
$hRevdr3 = FileOpen(@ScriptDir & "\Revdr3.txt", $FO_OVERWRITE)

$hCons1 = FileOpen(@ScriptDir & "\Cons1.txt", $FO_OVERWRITE)
$hCons2 = FileOpen(@ScriptDir & "\Cons2.txt", $FO_OVERWRITE)
$hCons3 = FileOpen(@ScriptDir & "\Cons3.txt", $FO_OVERWRITE)

For $i = 0 To UBound($aExcel) - 1
  If $aExcel[$i][8] = 0 Then
    If $aExcel[$i][4] = 2 Then
      FileWrite($hRevdr1, $aExcel[$i][0] & @CRLF)
      FileWrite($hRevdr2, $aExcel[$i][2] & @CRLF)
      FileWrite($hRevdr3, $aExcel[$i][10] & @CRLF)
    EndIf
    If $aExcel[$i][4] = 3 Then
      FileWrite($hCons1, $aExcel[$i][0] & @CRLF)
      FileWrite($hCons2, $aExcel[$i][2] & @CRLF)
      FileWrite($hCons3, $aExcel[$i][10] & @CRLF)
    EndIf
  EndIf
Next

FileClose($hRevdr1)
FileClose($hRevdr2)
FileClose($hRevdr3)
FileClose($hCons1)
FileClose($hCons2)
FileClose($hCons3)

It is taking less than 8 secs...But I didn't made much verification.

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