<commit_before>require 'spreadsheet'

class Array

  def to_xls(options = {}, &block)

    return '' if self.empty? && options[:prepend].blank?

    columns = []
    options.reverse_merge!(:header => true)

    xls_report = StringIO.new
    book = Spreadsheet::Workbook.new
    sheet = book.create_worksheet

    if options[:only]
      columns = Array(options[:only]).map(&:to_sym)
    elsif !self.empty?
      columns = self.first.class.column_names.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
    end

    return '' if columns.empty? && options[:prepend].blank?

    sheet_index = 0

    unless options[:prepend].blank?
      options[:prepend].each do |array|
        sheet.row(sheet_index).concat(array)
        sheet_index += 1
      end
    end

    if options[:header]
      sheet.row(sheet_index).concat(options[:header_columns].blank? ? columns.map(&:to_s).map(&:humanize) : options[:header_columns])
      sheet_index += 1
    end

    self.each_with_index do |obj, index|
      if block<commit_msg>added client_encoding options<commit_after>require 'spreadsheet'

class Array

  def to_xls(options = {}, &block)

    return '' if self.empty? && options[:prepend].blank?

    columns = []
    options.reverse_merge!(:header => true)

    xls_report = StringIO.new

    Spreadsheet.client_encoding = options[:client_encoding] || "UTF-8"

    book = Spreadsheet::Workbook.new
    sheet = book.create_worksheet

    if options[:only]
      columns = Array(options[:only]).map(&:to_sym)
    elsif !self.empty?
      columns = self.first.class.column_names.map(&:to_sym) - Array(options[:except]).map(&:to_sym)
    end

    return '' if columns.empty? && options[:prepend].blank?

    sheet_index = 0

    unless options[:prepend].blank?
      options[:prepend].each do |array|
        sheet.row(sheet_index).concat(array)
        sheet_index += 1
      end
    end

    if options[:header]
      sheet.row(sheet_index).concat(options[:header_columns].blank? ? columns.map(&:to_s).map(&:humanize) : options[:header_columns])
      sheet_index += 1
    end

    self.each_with_index do |obj, index|
      if block
{'fraction_non_alphanumeric': 0.14010343206393983, 'fraction_numerical': 0.003291020216267043, 'mean_word_length': 3.2903225806451615, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4696747', 'n_tokens_mistral': 701, 'n_tokens_neox': 668, 'n_words': 124}