Elixlsx

repository·master·Indexed 43 days ago

https://github.com/xou/elixlsx

An Elixir library for writing Microsoft Excel OpenXML format (.xlsx) files. It supports multiple named sheets, data types (strings, numbers, dates), cell formatting (fonts, colors, borders), merged cells, data validations, and pane freezing. It provides functions to write workbooks directly to disk via `write_to/2` or to memory via `write_to_memory/2`.

Tokens
3.1K
Snippets
21
Records
22
Agent score
55%

What's inside Elixlsx

  1. Handle formulas in Excel cells

    master
    Because the workbook is ultimately an XML file, formulas containing special characters like < or > must be escaped properly to avoid invalid XML. You can use :xmerl_lib.export_text/1 to ensure formulas are escaped correctly.
  2. Create an Excel file with Elixlsx

    master

    You can create an .xlsx file by composing Workbook and Sheet operations. Use Workbook.append_sheet/2 to add sheets to a workbook and Elixlsx.write_to/2 to save the file to disk. Sheets can be named using Sheet.with_name/1 and cells can be populated using Sheet.set_cell/4 with various formatting options like bold: true.

    (alias Elixlsx.Workbook, alias Elixlsx.Sheet)
    
    Workbook.append_sheet(%Workbook{}, Sheet.with_name("Sheet 1") |> Sheet.set_cell("A1", "Hello", bold: true)) 
    |> Elixlsx.write_to("hello.xlsx")
  3. Install Elixlsx

    master

    Add elixlsx to your mix.exs dependencies. Note that version 0.6 and above requires Elixir 1.12 or higher.

    # Via Hex
    defp deps do
      [{:elixlsx, "~> 0.6.0"}]
    end
    
    # Via GitHub
    defp deps do
      [{:elixlsx, github: "xou/elixlsx"}]
    end
  4. Add data validations with `add_data_validations/4`

    master

    Add data validation rules to a range of cells.

    Parameters:

    • start_cell: The starting Excel coordinate (e.g., "A1").
    • end_cell: The ending Excel coordinate (e.g., "B10").
    • values: A single String.t() or a list(String.t()) representing the allowed values.
    # Validates that cells in A1:A10 contain one of the specified values
    Elixlsx.Sheet.add_data_validations(sheet, "A1", "A10", ["Option1", "Option2"])
  5. Configure number and date formats with Elixlsx.Style.NumFmt

    master

    The Elixlsx.Style.NumFmt module is used to define how numbers and dates are formatted in an Excel spreadsheet. You can create format structs using from_props/1 by passing a map of properties, or by using the provided helper functions for common date formats.

    Supported Properties for from_props/1:

    • :yyyymmdd: Returns a yyyy-mm-dd format.
    • :yyyymm: Returns a yyyy-mm format.
    • :datetime: Returns a yyyy-mm-dd h:mm:ss format.
    • :num_format: Accepts a custom string representing a valid Excel format code. This will raise an ArgumentError if the string is not a valid XML string.
    # Using predefined date formats
    fmt_dd = Elixlsx.Style.NumFmt.from_props(%{yyyymmdd: true})
    fmt_mm = Elixlsx.Style.NumFmt.from_props(%{yyyym: true})
    
    # Using a custom format code
    custom_fmt = Elixlsx.Style.NumFmt.from_props(%%{num_format: "#,##0.00"})
  6. Generate border XML entries with get_border_entry/1

    master

    The Elixlsx.Style.Border.get_border_entry/1 function converts a %Elixlsx.Style.Border{} struct into its corresponding XML string representation used in XLSX files.

    Example

    # Input
    Elixlsx.Style.Border.get_border_entry %Elixlsx.Style.Border{type: :top, style: :dotted}
    
    # Output
    "<top style=\"dotted\"></top>"
    Elixlsx.Style.Border.get_border_entry %Elixlsx.Style.Border{type: :top, style: :dotted}
    # => "<top style=\"dotted\"></top>"
  7. Write a Workbook to a file with write_to/2

    master

    Use Elixlsx.write_to/2 to save an Elixlsx.Workbook object to a specific file on disk. This function handles the compilation of the workbook (including shared strings and styles) and packages it into the .xlsx (OpenXML) format using the :zip application.

    Elixlsx.write_to(workbook, "output.xlsx")
  8. Manage pane freezing with `set_pane_freeze/3`

    master

    Freeze panes at a specific row and column intersection. This keeps certain rows or columns visible while scrolling.

    • Rows and columns are indexed starting from 1.
    • Use 0 to indicate no freezing for that dimension (e.g., {1, 0} freezes the first row but no columns).
    • Use remove_pane_freeze/1 to clear any existing freeze settings.
    # Freeze the first row and first column
    Elixlsx.Sheet.set_pane_freeze(sheet, 1, 1)
    
    # Remove freezing
    Elixlsx.Sheet.remove_pane_freeze(sheet)
  9. Configure border styles with Elixlsx.Style.Border

    master

    You can define border properties for cells using the %Elixlsx.Style.Border{} struct. Borders are defined by their position, visual style, and color.

    Supported Properties

    • type: An atom specifying the border position. Supported values are :left, :right, :top, :bottom, and :diagonal.
    • style: An atom specifying the visual appearance. Supported values are:
      • :solid
      • :dotted
      • :dashed
      • :double
      • :dash_dot (renders as dashDot)
      • :dash_dot_dot (renders as dashDotDot)
      • :thin
      • :medium
      • :thick
    • color: A Hex-String representing the color.

    Creating a Border

    You can create a border struct using Elixlsx.Style.Border.from_props/2, which takes a properties map and the border type.

    # Using from_props to create a border
    border = Elixlsx.Style.Border.from_props(%{style: :dotted, color: "#FF0000"}, :top)
    
    # Or using the struct directly
    border = %Elixlsx.Style.Border{type: :top, style: :dotted}
  10. Write a Workbook to memory with write_to_memory/2

    master

    Use Elixlsx.write_to_memory/2 to generate the .xlsx binary in memory instead of writing directly to a file. This is useful for web applications where you want to serve the file as a download or pass it to another process without disk I/O.

    It returns a tuple containing the filename (as a charlist) and the actual binary data.

    {:ok, {filename, binary}} = Elixlsx.write_to_memory(workbook, "output.xlsx")
  11. Group columns with `group_cols/3`

    master

    Increase the outline level for a range of columns. Columns are indexed by name (e.g., "A", "C").

    Options:

    • collapsed: If true, the group will be collapsed by default.
    # Groups columns from A to C and collapses them
    Elixlsx.Sheet.group_cols(sheet, "A", "C", collapsed: true)