Open In App

Working with Excel Files in Julia

Julia is a high-level open-source programming language meaning that its source is freely available. It is a language that is used to perform operations in scientific computing. Julia is used for statistical computations and data analysis. Julia provides its users with some pre-defined functions and built-in packages with the help of which Julia makes it possible to work with Excel Files, that too with quite an easiness.

Reading Excel Files

With the help of Packages, Julia makes is easier to Read an Excel File. First, the package that is needed to be added to get its functionality for reading the Excel file is



XLSX.jl package

All the packages can be added with passing it as an argument in the add(” “) function which is present in the Pkg object

Pkg.add("")

Reading one row at a time

To read one row at a time from an Excel File, Julia provides a function eachrow() to iterate over each row of the file and store the same in a variable.



Approach:




using Pkg
Pkg.add("XLSX")
XLSX.openxlsx("sample1.xlsx", enable_cache=false) do f
  sheet = f["Sheet1"]
  for r in XLSX.eachrow(sheet)
  
    # r is a `SheetRow`, values are read 
    # using column references
    rn = XLSX.row_number(r) # `SheetRow` row number
    v1 = r[1]    # will read value at column 1
    v2 = r[2]# will read value at column 2
    v3 = r["B"]
    v4 = r[3]
    println("v1=$v1, v2=$v2, v3=$v3, v4=$v4")
  end
end

                                                       

Reading whole file at once

Julia provides a function readxlsx() to read all the contents of a file in a single attempt.

Approach: 




using Pkg
Pkg.add("XLSX")
import XLSX
xf = XLSX.readxlsx("sample3.xlsx")




sh = xf["Sheet1"]
sh[:]

Modifying contents of a File

Modification of contents of an Excel File can be done by opening the file in the ‘rw’ i.e. Read-write mode and then further updating the values with the help of iterator.

Approach:




# Modifying contents of a file
using Pkg
Pkg.add("XLSX")
XLSX.openxlsx("sample3.xlsx", mode="rw") do xf
    sheet = xf[1]
    sheet["B2"] = "March" #row number = B2
end

Creating new columns

The addition of columns in an Excel File is done by opening the file in ‘rw’ mode and then using the collect() function.

Approach:




# Only 10 columns are present
df2 = DataFrame(XLSX.readtable("sample2.xlsx", "Sheet1")...)
  
# add a new column to an existing file 
# and makes it 11 columns
XLSX.openxlsx("sample2.xlsx", mode="rw") do xf
    sheet = xf[1]
      
    # add a column from "K1" to "K3"
    sheet["K1", dim=1] = collect(1:3)
end

Deleting contents of a file

The deletion of contents from an Excel File can be done by using the setdiff() function. This function compares all the rows and removes the row that is passed as an argument.

Approach:




# deleting from an existing column
row = 2
df = df[setdiff(1:end, row), :]

Original File:

Updated File:

Appending to files

Julia provides a function append() to perform the append operation to files. This function takes both the dataframes as an argument and returns the appended dataframe.

Approach:




# Only 10 columns are present
df2 = DataFrame(XLSX.readtable("sample2.xlsx", "Sheet1")...)
  
# Add a new column to an existing file 
# and makes it 11 columns
XLSX.openxlsx("sample2.xlsx", mode="rw") do xf
    sheet = xf[1]
      
    # will add a column from "K1" to "K3"
    sheet["K1", dim=1] = collect(1:3)
end
  
# Updated columns and stored in new dataframe df3
df3 = DataFrame(XLSX.readtable("sample2.xlsx", "Sheet1")...)
  
# Appended df2 rows to the end of df3 
# with same column names
df3 = append!(df2,df3)

                                                   

Writing a new excel file

To write contents to a new Excel file, open the file in the ‘w’ i.e. write mode and then use the collect() function to add columns to the files and further assign the values to be added to the variables.

Approach:




# Writing a new xlsx file and 
# the mode is w means(write)
# and created a new one.
XLSX.openxlsx("sample5.xlsx", mode="w") do xf
    sheet = xf[1]
      
    # add a row from "A5" to "E5"
    # equivalent to `sheet["A5", dim=2] = collect(1:4)`
    sheet["A5"] = collect(1:5)
  
    # will add a column from "B1" to "B4"
    sheet["B1", dim=1] = collect(1:3)
  
    # will add a matrix from "A7" to "C9"
    sheet["A7:C9"] = [ 1 2 3 ; 4 5 6 ; 7 8 9 ]
end


Article Tags :