How to import data from Excel files?

I am trying to upload information into the system from excel files
. i can’t make out most of the things ive read online

Let’s start with what have you tried, what do you want to import, and an example Excel file?

1 Like

Hello Vincent,

I’ve used Xlsxir — Xlsxir v1.6.4 to parse excel files and store their rows as database records.

Here is a maybe oversimplified example of its usage, hth:

defmodule MyApp.Utils.ImportXlsx do
  def import_xlsx(input_xlsx) do
    with {:ok, table_id} <- Xlsxir.multi_extract(input_xlsx, 0),
         :ok <-
           Xlsxir.get_list(table_id)
           |> List.delete_at(0) # to remove header's row
           |> insert_records() do
      Xlsxir.close(table_id)
    else
      {:error, reason} -> {:error, reason}
    end
  end

  defp insert_records([]), do: :ok

  defp insert_records([r | rs]) do
    {:ok, _} =
      MyApp.MyContext.create_record(%{
        field1: Enum.at(r, 0),
        field2: Enum.at(r, 1),
        field3: Enum.at(r, 2)
      })

    insert_records(rs)
  end
end
5 Likes