export

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 7 Imported by: 0

Documentation

Overview

Package export provides table export functionality.

Package export provides table export functionality.

Package export provides table export functionality for various formats.

Supported formats:

  • CSV (Comma-Separated Values)
  • JSON (JavaScript Object Notation)
  • Excel (.xlsx)

Example:

tables := doc.ExtractTables()
export.ToCSV(tables[0], "output.csv")
export.ToJSON(tables[0], "output.json")

Package export provides table export functionality.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CSVExporter

type CSVExporter struct {
	// contains filtered or unexported fields
}

CSVExporter exports tables to CSV format.

CSV (Comma-Separated Values) is a simple text format for tabular data.

Features:

  • Configurable delimiter (comma, semicolon, tab, etc.)
  • Proper quoting and escaping
  • Handles multi-line cells
  • Standard RFC 4180 compliant

Limitations:

  • Does not support merged cells (spans)
  • No cell formatting (alignment, colors, etc.)

Example usage:

exporter := export.NewCSVExporter()
err := exporter.Export(table, file)

func NewCSVExporter

func NewCSVExporter() *CSVExporter

NewCSVExporter creates a new CSV exporter with default options.

func NewCSVExporterWithOptions

func NewCSVExporterWithOptions(options *ExportOptions) *CSVExporter

NewCSVExporterWithOptions creates a new CSV exporter with custom options.

func (*CSVExporter) ContentType

func (e *CSVExporter) ContentType() string

ContentType returns the MIME content type for CSV.

func (*CSVExporter) Export

func (e *CSVExporter) Export(tbl *table.Table, w io.Writer) error

Export writes the table to the writer in CSV format.

func (*CSVExporter) ExportToString

func (e *CSVExporter) ExportToString(tbl *table.Table) (string, error)

ExportToString exports the table to a CSV string.

func (*CSVExporter) FileExtension

func (e *CSVExporter) FileExtension() string

FileExtension returns the file extension for CSV.

func (*CSVExporter) WithDelimiter

func (e *CSVExporter) WithDelimiter(delimiter string) *CSVExporter

WithDelimiter returns a new CSVExporter with a custom delimiter.

Common delimiters:

  • "," - Comma (default)
  • ";" - Semicolon (European standard)
  • "\t" - Tab (TSV format)

type ExcelExporter

type ExcelExporter struct {
	// contains filtered or unexported fields
}

ExcelExporter exports tables to Excel format (XLSX).

Excel export provides rich formatting and layout capabilities.

Features:

  • Full Excel XLSX format support
  • Merged cells (row and column spans)
  • Cell alignment (left, center, right)
  • Multiple sheets (if needed)
  • Professional formatting

Limitations:

  • Binary format (larger than CSV/JSON)
  • Requires excelize library

Example usage:

exporter := export.NewExcelExporter()
err := exporter.Export(table, file)

func NewExcelExporter

func NewExcelExporter() *ExcelExporter

NewExcelExporter creates a new Excel exporter with default options.

func NewExcelExporterWithOptions

func NewExcelExporterWithOptions(options *ExportOptions) *ExcelExporter

NewExcelExporterWithOptions creates a new Excel exporter with custom options.

func (*ExcelExporter) ContentType

func (e *ExcelExporter) ContentType() string

ContentType returns the MIME content type for Excel.

func (*ExcelExporter) Export

func (e *ExcelExporter) Export(tbl *table.Table, w io.Writer) error

Export writes the table to the writer in Excel format.

func (*ExcelExporter) ExportToBytes

func (e *ExcelExporter) ExportToBytes(tbl *table.Table) ([]byte, error)

ExportToBytes exports the table to Excel format as bytes.

This is a convenience method for getting Excel content as a byte slice.

func (*ExcelExporter) ExportToString

func (e *ExcelExporter) ExportToString(tbl *table.Table) (string, error)

ExportToString is not applicable for Excel (binary format).

Returns an error indicating binary formats should use Export() with a buffer.

func (*ExcelExporter) FileExtension

func (e *ExcelExporter) FileExtension() string

FileExtension returns the file extension for Excel.

func (*ExcelExporter) WithMergedCells

func (e *ExcelExporter) WithMergedCells(preserve bool) *ExcelExporter

WithMergedCells returns a new ExcelExporter with merged cells enabled/disabled.

func (*ExcelExporter) WithSheetName

func (e *ExcelExporter) WithSheetName(name string) *ExcelExporter

WithSheetName returns a new ExcelExporter with a custom sheet name.

type ExportOptions

type ExportOptions struct {
	// IncludeEmpty indicates whether to include empty cells in export.
	// Default: true
	IncludeEmpty bool

	// PreserveSpans indicates whether to preserve merged cells (row/col spans).
	// Not all formats support this (e.g., CSV doesn't).
	// Default: false
	PreserveSpans bool

	// Delimiter is the field delimiter for CSV export (e.g., ",", ";", "\t").
	// Default: ","
	Delimiter string

	// IncludeMetadata indicates whether to include table metadata in export.
	// Applicable to JSON and other metadata-aware formats.
	// Default: false
	IncludeMetadata bool

	// PrettyPrint indicates whether to format output for readability.
	// Applicable to JSON export.
	// Default: false
	PrettyPrint bool
}

ExportOptions contains options for table export.

func DefaultExportOptions

func DefaultExportOptions() *ExportOptions

DefaultExportOptions returns default export options.

type JSONExporter

type JSONExporter struct {
	// contains filtered or unexported fields
}

JSONExporter exports tables to JSON format.

JSON export provides a rich, structured representation of tables.

Features:

  • Includes cell metadata (position, spans, alignment)
  • Includes table metadata (page, bounds, method)
  • Supports merged cells
  • Pretty-printing option

Output format:

{
  "rows": 3,
  "columns": 4,
  "data": [
    [{"text": "A1", "row": 0, "col": 0}, ...],
    ...
  ],
  "metadata": {
    "page": 0,
    "method": "Lattice",
    "bounds": {...}
  }
}

Example usage:

exporter := export.NewJSONExporter().WithPrettyPrint(true)
err := exporter.Export(table, file)

func NewJSONExporter

func NewJSONExporter() *JSONExporter

NewJSONExporter creates a new JSON exporter with default options.

func NewJSONExporterWithOptions

func NewJSONExporterWithOptions(options *ExportOptions) *JSONExporter

NewJSONExporterWithOptions creates a new JSON exporter with custom options.

func (*JSONExporter) ContentType

func (e *JSONExporter) ContentType() string

ContentType returns the MIME content type for JSON.

func (*JSONExporter) Export

func (e *JSONExporter) Export(tbl *table.Table, w io.Writer) error

Export writes the table to the writer in JSON format.

func (*JSONExporter) ExportToString

func (e *JSONExporter) ExportToString(tbl *table.Table) (string, error)

ExportToString exports the table to a JSON string.

func (*JSONExporter) FileExtension

func (e *JSONExporter) FileExtension() string

FileExtension returns the file extension for JSON.

func (*JSONExporter) WithMetadata

func (e *JSONExporter) WithMetadata(include bool) *JSONExporter

WithMetadata returns a new JSONExporter with metadata inclusion enabled/disabled.

func (*JSONExporter) WithPrettyPrint

func (e *JSONExporter) WithPrettyPrint(pretty bool) *JSONExporter

WithPrettyPrint returns a new JSONExporter with pretty printing enabled/disabled.

type TableExporter

type TableExporter interface {
	// Export writes the table to the writer in the format implemented by the exporter.
	//
	// Parameters:
	//   - tbl: The table to export
	//   - w: The writer to write to (file, buffer, network, etc.)
	//
	// Returns an error if export fails.
	Export(tbl *table.Table, w io.Writer) error

	// ExportToString exports the table to a string.
	//
	// This is a convenience method for formats that produce text output.
	// Returns the exported string, or error.
	ExportToString(tbl *table.Table) (string, error)

	// ContentType returns the MIME content type of the exported format.
	//
	// Examples:
	//   - CSV: "text/csv"
	//   - JSON: "application/json"
	//   - Excel: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
	ContentType() string

	// FileExtension returns the recommended file extension for the format.
	//
	// Examples:
	//   - CSV: ".csv"
	//   - JSON: ".json"
	//   - Excel: ".xlsx"
	FileExtension() string
}

TableExporter is the interface for exporting tables to different formats.

This interface enables:

  • Multiple export formats (CSV, JSON, Excel, etc.)
  • Custom exporter implementations
  • Easy testing with mocks
  • Dependency injection

Example usage:

exporter := export.NewCSVExporter()
err := exporter.Export(table, writer)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL