Class: XlsxKit::Writer

Inherits:
Object
  • Object
show all
Defined in:
lib/xlsxkit/writer.rb

Overview

XLSX Workbook 写入 API(与 Workbook 读取器对应)。

将原生 Ruby 数据(二维数组 / Hash 数组 / 多 Sheet)按表格写入 .xlsx 文件。

用法:

1. 一次性写入单个 Sheet

XlsxKit::Writer.write('out.xlsx', [['Name', 'Age'], ['Alice', 30]])

# 2. 一次性写入多个 Sheet
XlsxKit::Writer.write('out.xlsx', { 'Sheet1' => rows1, 'Sheet2' => rows2 })

# 3. 构建器模式
XlsxKit::Writer.build('out.xlsx') do |wb|
wb.add_sheet('People') do |s|
  s << ['Name', 'Age']
  s << ['Alice', 30]
end
end

Defined Under Namespace

Classes: Sheet

Constant Summary collapse

NS_SPREADSHEET =

OOXML 命名空间

'http://schemas.openxmlformats.org/spreadsheetml/2006/main'
NS_REL =
'http://schemas.openxmlformats.org/officeDocument/2006/relationships'
NS_CONTENT =
'http://schemas.openxmlformats.org/package/2006/content-types'
NS_PKG_REL =
'http://schemas.openxmlformats.org/package/2006/relationships'

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path = nil, &block) ⇒ Writer

创建写入器。传 path 并给块时,块结束后自动 save。



53
54
55
56
57
58
59
60
# File 'lib/xlsxkit/writer.rb', line 53

def initialize(path = nil, &block)
  @path   = path
  @sheets = []
  if block
    yield self
    save(path) if path
  end
end

Instance Attribute Details

#sheets ⇒ Object (readonly)

Returns the value of attribute sheets.



49
50
51
# File 'lib/xlsxkit/writer.rb', line 49

def sheets
  @sheets
end

Class Method Details

.build(path = nil, &block) ⇒ Object

构建器模式:块内 add_sheet,块结束后写入文件。



64
65
66
# File 'lib/xlsxkit/writer.rb', line 64

def self.build(path = nil, &block)
  new(path, &block)
end

.write(path, data, sheet_name: 'Sheet1') ⇒ Object

一次性写入:

  • data 为二维数组时,写入单个 Sheet(默认名 "Sheet1")
  • data 为 { name => rows } 时,写入多个 Sheet
  • rows 可为「数组的数组」或「Hash 的数组」(首行 Hash 的 keys 作为表头)


73
74
75
76
77
78
79
80
81
82
# File 'lib/xlsxkit/writer.rb', line 73

def self.write(path, data, sheet_name: 'Sheet1')
  writer = new
  if data.is_a?(Hash)
    data.each { |name, rows| writer.add_sheet(name, rows) }
  else
    writer.add_sheet(sheet_name, data)
  end
  writer.save(path)
  writer
end

Instance Method Details

#add_sheet(name = nil, rows = nil) {|sheet| ... } ⇒ Object

添加一个 Sheet。可传入初始 rows,也可用块逐行 <<。

Yields:

  • (sheet)


86
87
88
89
90
91
92
# File 'lib/xlsxkit/writer.rb', line 86

def add_sheet(name = nil, rows = nil)
  sheet = Sheet.new(name || "Sheet#{@sheets.length + 1}")
  @sheets << sheet
  sheet.add_rows(rows) if rows
  yield sheet if block_given?
  sheet
end

#save(target = @path) ⇒ Object

写入文件。target 可为文件路径或可写 IO(如 StringIO)。

Raises:

  • (ArgumentError)


96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/xlsxkit/writer.rb', line 96

def save(target = @path)
  raise ArgumentError, 'output path required' unless target

  if target.respond_to?(:write)
    zip = ZipWriter.new(target)
    write_zip(zip)
    zip.close
  else
    ZipWriter.open(target) { |zip| write_zip(zip) }
  end
  target
end