Jump to content

Writing array into excel cells with line breaks automatically


Recommended Posts

I'm trying to write data from array into .text file with line break and i did it successfully.

but now I want to write from the same array into already empty excel with with line breaks means each value of array should be written in next cell automatically as it's happening in text file using "Filewrite"

Looking for guidance.

Here is my code;

$file = fileopen(@scriptdir & "\source.txt", 10)
$IE = _IECreate("http://www.example.com")
$source = _IEDocReadHTML($IE)
FileWrite($file, $source)
$target_source1 = _StringBetween($source, '<span>', '</span>')
If Not @error Then
For $i=0 To UBound($target_source1) -1
      FileWrite (@scriptdir & "\save.txt", $target_source2[$i] & @crlf)
; I want to write above array onto excel file
Next
EndIf

 

Link to comment
Share on other sites

  • Moderators

@jonson1986,  The help file is your friend. Instead of fileopen, look at opening an Excel spreadsheet:

Local $oExcel = _Excel_Open()
Local $oWoorkbook = _Excel_BookOpen($oExcel, <Path to your Excel File>)

Then you can write the entire array into the spreadsheet. Look at the second or third example under _Excel_RangeWrite.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

Thanks JLogan3o13,

I tried the example to write array but in my codes it's not able to work for me;

here is my code;

#RequireAdmin
#include <IE.au3>
#include <String.au3>
#include <Array.au3>
#include <Excel.au3>
Local $oExcel = _Excel_Open()
Local $oWorkbook1 = _Excel_BookOpen($oExcel, @ScriptDir & "\1337x.xlsx", True)
$file = fileopen(@scriptdir & "\source.txt", 10)
$IE = _IECreate("http://www.example.com")
$source = _IEDocReadHTML($IE)
FileWrite($file, $source)
$target_source1 = _StringBetween($source, '<span>', '</span>')
If Not @error Then
For $i=0 To UBound($target_source1) -1
      _Excel_RangeWrite($oWorkbook1, Default, $target_source2[$i], "A1")
Next
MsgBox ('', '', 'Done')
EndIf

 

Link to comment
Share on other sites

You refer to another array as a variable $target_source2, but that hasn't been defined in the code you show.  Working with example.com (an example, I know) I put this together; see if it works the way you want.  There are 2 code sections with the <p> tag in example.com.

#RequireAdmin
#include <IE.au3>
#include <String.au3>
#include <Array.au3>
#include <Excel.au3>
Local $oExcel = _Excel_Open()
Local $oWorkbook1 = _Excel_BookOpen($oExcel, "1337x.xlsx", True)
$file = fileopen(@scriptdir & "\source.txt", 10)
$IE = _IECreate("http://www.example.com")
$source = _IEDocReadHTML($IE)
FileWrite($file, $source)
$target_source1 = _StringBetween($source, '<p>', '</p>')
_ArrayDisplay($target_source1)
If Not @error Then
For $i=0 To UBound($target_source1) -1
      _Excel_RangeWrite($oWorkbook1, Default, $target_source1[$i], "A"&$i+1) ; if you want each item in the array in its own cell
Next
MsgBox (0, '', 'Done')
EndIf

 

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

×
×
  • Create New...