Sign in to follow this  
Followers 0
jonson1986

Writing array into excel cells with line breaks automatically

4 posts in this topic

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

 

Share this post


Link to post
Share on other sites

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


√-1 2^3 ∑ π, and it was delicious!

Share this post


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

 

Share this post


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

 

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
Sign in to follow this  
Followers 0

  • Similar Content

    • ShawnW
      By ShawnW
      I have a script that takes a large excel file, pulls out and reorganizes certain information I need, and spits out a trimmed down csv file which I uses to upload the information on my website. Some of this information contains characters with accents or em dashes. By default it would create a csv file in ANSI which I then uploaded but had to tell my website import system it was windows-1252 in order for it to look correct.
      This was all working fine except now I need to add in a non-breaking space and non-breaking hyphen into parts of my output. At first I tried using ChrW(0xA0) and ChrW(0x2011) as replacements. A quick test in the console looked correct, however opening the csv output in notepad++ showed the space correctly but a ? for the hyphen and the file was still encoded as ANSI. I tried to view it as UTF-8 instead but this just made the space appear as xAO and also other characters appeared that way like my em dashes appeared as x97 and another symbol as xA7 etc.
      If I instead do a convert to UTF-8 from notepad++ then those problems go away except the hyphen still displays as ?. I then noticed on the page I linked for the non-breaking hyphen it lists the UTF-8 hex as 0xE2 0x80 0x91 (e28091). I was unsure how to enter this in autoit but several things i tried all failed to get the hyphen inserted.
      I need a way to get both the space and hyphen added correctly as either ANSI or UTF-8, but if it is UTF-8 then I need a way to convert all of the other data I extracted from the excel file.
      I've included a test excel file with a single line and test script to create a csv demonstrating the problem.
      test.xlsx
      test.au3
    • Ascer
      By Ascer
      1. Description.
      Udf working with MSDN System.Collections.ArrayList. Allow you to make fast operations on huge arrays, speed is even x10 better than basic _ArrayAdd.  Not prefered for small arrays < 600 items. 2. Requirements
      .NET Framework 1.1 - 4.5 (on this version Microsoft destroy old rules) System Windows 3. Possibilities.
      ;=============================================================================================================== ; UDF Name: List.au3 ; ; Date: 2018-02-17, 10:52 ; Description: Simple udf to create System Collections as ArrayList and make multiple actions on them. ; ; Function(s): _ListCreate -> Creates a new list ; _ListCapacity -> Gets a list size in bytes ; _ListCount -> Gets items count in list ; _ListIsFixedSize -> Get bool if list if fixed size ; _ListIsReadOnly -> Get bool if list is read only ; _ListIsSynchronized -> Get bool if list is synchronized ; _ListGetItem -> Get item on index ; _ListSetItem -> Set item on index ; ; _ListAdd -> Add item at end of list ; _ListClear -> Remove all list items ; _ListClone -> Duplicate list in new var ; _ListContains -> Get bool if item is in list ; _ListGetHashCode -> Get hash code for list ; _ListGetRange -> Get list with items between indexs ; _ListIndexOf -> Get index of item ; _ListInsert -> Insert a new item on index ; _ListInsertRange -> Insert list into list on index ; _ListLastIndexOf -> Get index last of item ; _ListRemove -> Remove first found item ; _ListRemoveAt -> Remove item in index ; _ListRemoveRange -> Remove items between indexs ; _ListReverse -> Reverse all items in list ; _ListSetRange -> Set new value for items in range ; _ListSort -> Sort items in list (speed of reading) ; _ListToString -> Get list object name ; _ListTrimToSize -> Remove unused space in list ; ; Author(s): Ascer ;=============================================================================================================== 4. Downloads
      List.au3 5. Examples
      SpeedTest _ArrayAdd vs ListAdd SpeedTest ArraySearch vs ListIndexOf Basic usage - crating guild with members  
    • ronmage
      By ronmage
      So I have a loop that keeps reading data from an array and searching it for the same value. If the value is no there it does work then adds the value to the array to prevent it from doing the same work.
      If _ArraySearch($ID,$filearray[$i]) = -1 Then Work.... _ArrayAdd($ID,$filearray[$i]) EndIf This is in a for loop hence $i
      So what is happening is the code works great for several hours. After a period of time _ArraySearch($ID,$filearray[$i]) will result in -1 even if $ID = $filearray. So it ready as if there is no data in the array. Anyone have this problem? 
       
      Also I am just running in using F5 not compiling it and running it if that makes a difference.
       
    • FMS
      By FMS
      Hello,
      I'm trying to read a binary file to an array but couln't get it to work.
      Also I coul not find any help in the forum around this subject whish was helpfull.
      Is there any way it could be done?
      I tried a lot of ways but maybe somebody know's the right way?
      #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <File.au3> #include <Array.au3> #include <AutoItConstants.au3> Local $in=FileOpen("TEST_labels.idx1-ubyte",16) ; 16+0=Read binary Local $data = FileRead($in) Local $FileArray = BinaryToString($data,4) ;~ $FileArray = StringSplit($BinarydData, @CRLF, 1+2) ;~ Local $FileArray = StringRegExp($BinarydData, "[^\r\n]+", 3) FileClose($in) _ArrayDisplay($FileArray,"$FileArray","",32) MsgBox($MB_SYSTEMMODAL, "", "$FileArray = " & $FileArray )  
      TEST_labels.idx1-ubyte
    • ur
      By ur
      I am reading a CSV file which is tab seperated as below.
      Local $aArray = FileReadToArray($file) And now, I am splitting this main array record wise so that Array contains internally another arrow to represent each row.
      For $i = 0 to (UBound($aArray) - 1) ;MsgBox(0,"",$aArray[$i]) $aArray[$i] = StringSplit(StringStripCR($aArray[$i]), Chr(9),2);Chr(9) for tab ;_ArrayDisplay($aArray[$i]) Next Afther that, _ArrayDIsplay is able to see the individual internal arrays.
      _ArrayDisplay($aArray[1]) But If I try to access the individual element of it as below.It is not showing any result.
      MsgBox(0,"",$aArray[1][1]) Any suggestion, below is the sample csv file.
      New Text Document.csv