2025-03-27
Go
00

目录

第一部分 概述
第二部分 常用操作
2.1 基本示例
2.2 无边框/页脚/批量追加
2.3 从CSV读取数据
2.4 自定义分隔符
2.5 Markdown格式
2.6 相同单元格合并
2.7 相同单元格合并(指定要合并的列索引)
2.8 带颜色的表格
2.8 带颜色的表格单元格
2.9 设置表格标题
2.10 设置 NoWhiteSpace 和 TablePadding 选项

第一部分 概述

tablewriter是一个用于在终端中生成ASCII表格的Go语言库。它提供了丰富的功能,包括自动填充、支持多行内容、对齐设置、自定义分隔符、数字和百分比的自动对齐等。此外,它还能直接从CSV文件读取数据,支持自定义脚注,并允许合并相同的单元格内容。

主要功能:

  • 自动填充:根据内容自动调整列宽,确保表格对齐美观。
  • 多行支持:单元格内支持多行文本,适用于展示较长内容。
  • 对齐设置:可设置文本左对齐、右对齐或居中对齐,满足不同展示需求。
  • 自定义分隔符:允许自定义行、列和中心的分隔符,增强表格的可读性。
  • 数字与百分比自动对齐:针对数字和百分比内容,提供自动对齐功能,方便数据比较。
  • 直接读取 CSV 文件:可直接从 CSV 文件读取数据并生成表格,简化数据展示流程。
  • 自定义脚注:支持在表格底部添加自定义脚注,提供额外信息。
  • 合并相同单元格内容:对于连续相同的单元格内容,可选择合并,提升表格简洁性。

项目地址:https://github.com/olekukonko/tablewriter

第二部分 常用操作

2.1 基本示例

示例代码:

go
data := [][]string{ []string{"A", "The Good", "500"}, []string{"B", "The Very very Bad Man", "288"}, []string{"C", "The Ugly", "120"}, []string{"D", "The Gopher", "800"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Name", "Sign", "Rating"}) for _, v := range data { table.Append(v) } table.Render() // Send output

显示效果:

+------+-----------------------+--------+ | NAME | SIGN | RATING | +------+-----------------------+--------+ | A | The Good | 500 | | B | The Very very Bad Man | 288 | | C | The Ugly | 120 | | D | The Gopher | 800 | +------+-----------------------+--------+

2.2 无边框/页脚/批量追加

示例代码:

go
data := [][]string{ []string{"1/1/2014", "Domain name", "2233", "$10.98"}, []string{"1/1/2014", "January Hosting", "2233", "$54.95"}, []string{"1/4/2014", "February Hosting", "2233", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer table.EnableBorder(false) // Set Border to false table.AppendBulk(data) // Add Bulk Data table.Render()

显示效果:

DATE | DESCRIPTION | CV2 | AMOUNT -----------+--------------------------+-------+---------- 1/1/2014 | Domain name | 2233 | $10.98 1/1/2014 | January Hosting | 2233 | $54.95 1/4/2014 | February Hosting | 2233 | $51.00 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 -----------+--------------------------+-------+---------- TOTAL | $146 93 --------+----------

2.3 从CSV读取数据

示例代码:

go
table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test_info.csv", true) table.SetAlignment(tablewriter.ALIGN_LEFT) // Set Alignment table.Render()

显示效果:

+----------+--------------+------+-----+---------+----------------+ | FIELD | TYPE | NULL | KEY | DEFAULT | EXTRA | +----------+--------------+------+-----+---------+----------------+ | user_id | smallint(5) | NO | PRI | NULL | auto_increment | | username | varchar(10) | NO | | NULL | | | password | varchar(100) | NO | | NULL | | +----------+--------------+------+-----+---------+----------------+

2.4 自定义分隔符

示例代码

go
table, _ := tablewriter.NewCSV(os.Stdout, "testdata/test.csv", true) table.SetRowLine(true) // Enable row line // Change table lines table.SetCenterSeparator("*") table.SetColumnSeparator("╪") table.SetRowSeparator("-") table.SetAlignment(tablewriter.ALIGN_LEFT) table.Render()

显示效果

*------------*-----------*---------* ╪ FIRST NAME ╪ LAST NAME ╪ SSN ╪ *------------*-----------*---------* ╪ John ╪ Barry ╪ 123456 ╪ *------------*-----------*---------* ╪ Kathy ╪ Smith ╪ 687987 ╪ *------------*-----------*---------* ╪ Bob ╪ McCornick ╪ 3979870 ╪ *------------*-----------*---------*

2.5 Markdown格式

示例代码

go
data := [][]string{ []string{"1/1/2014", "Domain name", "2233", "$10.98"}, []string{"1/1/2014", "January Hosting", "2233", "$54.95"}, []string{"1/4/2014", "February Hosting", "2233", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetBorders(tablewriter.Border{Left: true, Top: false, Right: true, Bottom: false}) table.SetCenterSeparator("|") table.AppendBulk(data) // Add Bulk Data table.Render()

显示效果:

| DATE | DESCRIPTION | CV2 | AMOUNT | |----------|--------------------------|------|--------| | 1/1/2014 | Domain name | 2233 | $10.98 | | 1/1/2014 | January Hosting | 2233 | $54.95 | | 1/4/2014 | February Hosting | 2233 | $51.00 | | 1/4/2014 | February Extra Bandwidth | 2233 | $30.00 |

2.6 相同单元格合并

示例代码:

go
data := [][]string{ []string{"1/1/2014", "Domain name", "1234", "$10.98"}, []string{"1/1/2014", "January Hosting", "2345", "$54.95"}, []string{"1/4/2014", "February Hosting", "3456", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetFooter([]string{"", "", "Total", "$146.93"}) table.SetAutoMergeCells(true) table.SetRowLine(true) table.AppendBulk(data) table.Render()

显示效果:

+----------+--------------------------+-------+---------+ | DATE | DESCRIPTION | CV2 | AMOUNT | +----------+--------------------------+-------+---------+ | 1/1/2014 | Domain name | 1234 | $10.98 | + +--------------------------+-------+---------+ | | January Hosting | 2345 | $54.95 | +----------+--------------------------+-------+---------+ | 1/4/2014 | February Hosting | 3456 | $51.00 | + +--------------------------+-------+---------+ | | February Extra Bandwidth | 4567 | $30.00 | +----------+--------------------------+-------+---------+ | TOTAL | $146 93 | +----------+--------------------------+-------+---------+

2.7 相同单元格合并(指定要合并的列索引)

示例代码:

go
data := [][]string{ []string{"1/1/2014", "Domain name", "1234", "$10.98"}, []string{"1/1/2014", "January Hosting", "1234", "$10.98"}, []string{"1/4/2014", "February Hosting", "3456", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "4567", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetFooter([]string{"", "", "Total", "$146.93"}) table.SetAutoMergeCellsByColumnIndex([]int{2, 3}) table.SetRowLine(true) table.AppendBulk(data) table.Render()

显示效果:

+----------+--------------------------+-------+---------+ | DATE | DESCRIPTION | CV2 | AMOUNT | +----------+--------------------------+-------+---------+ | 1/1/2014 | Domain name | 1234 | $10.98 | +----------+--------------------------+ + + | 1/1/2014 | January Hosting | | | +----------+--------------------------+-------+---------+ | 1/4/2014 | February Hosting | 3456 | $51.00 | +----------+--------------------------+-------+---------+ | 1/4/2014 | February Extra Bandwidth | 4567 | $30.00 | +----------+--------------------------+-------+---------+ | TOTAL | $146.93 | +----------+--------------------------+-------+---------+

2.8 带颜色的表格

示例代码:

go
data := [][]string{ []string{"1/1/2014", "Domain name", "2233", "$10.98"}, []string{"1/1/2014", "January Hosting", "2233", "$54.95"}, []string{"1/4/2014", "February Hosting", "2233", "$51.00"}, []string{"1/4/2014", "February Extra Bandwidth", "2233", "$30.00"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Date", "Description", "CV2", "Amount"}) table.SetFooter([]string{"", "", "Total", "$146.93"}) // Add Footer table.EnableBorder(false) // Set Border to false table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor}, tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor}, tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor}, tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor}) table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor}) table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{}, tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.FgHiRedColor}) table.AppendBulk(data) table.Render()

运行效果:

Table with Color

2.8 带颜色的表格单元格

提示

func Rich设置的单元格颜色优先于列颜色。

示例代码:

go
data := [][]string{ []string{"Test1Merge", "HelloCol2 - 1", "HelloCol3 - 1", "HelloCol4 - 1"}, []string{"Test1Merge", "HelloCol2 - 2", "HelloCol3 - 2", "HelloCol4 - 2"}, []string{"Test1Merge", "HelloCol2 - 3", "HelloCol3 - 3", "HelloCol4 - 3"}, []string{"Test2Merge", "HelloCol2 - 4", "HelloCol3 - 4", "HelloCol4 - 4"}, []string{"Test2Merge", "HelloCol2 - 5", "HelloCol3 - 5", "HelloCol4 - 5"}, []string{"Test2Merge", "HelloCol2 - 6", "HelloCol3 - 6", "HelloCol4 - 6"}, []string{"Test2Merge", "HelloCol2 - 7", "HelloCol3 - 7", "HelloCol4 - 7"}, []string{"Test3Merge", "HelloCol2 - 8", "HelloCol3 - 8", "HelloCol4 - 8"}, []string{"Test3Merge", "HelloCol2 - 9", "HelloCol3 - 9", "HelloCol4 - 9"}, []string{"Test3Merge", "HelloCol2 - 10", "HelloCol3 -10", "HelloCol4 - 10"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Col1", "Col2", "Col3", "Col4"}) table.SetFooter([]string{"", "", "Footer3", "Footer4"}) table.EnableBorder(false) table.SetHeaderColor(tablewriter.Colors{tablewriter.Bold, tablewriter.BgGreenColor}, tablewriter.Colors{tablewriter.FgHiRedColor, tablewriter.Bold, tablewriter.BgBlackColor}, tablewriter.Colors{tablewriter.BgRedColor, tablewriter.FgWhiteColor}, tablewriter.Colors{tablewriter.BgCyanColor, tablewriter.FgWhiteColor}) table.SetColumnColor(tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiRedColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgHiBlackColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgBlackColor}) table.SetFooterColor(tablewriter.Colors{}, tablewriter.Colors{}, tablewriter.Colors{tablewriter.Bold}, tablewriter.Colors{tablewriter.FgHiRedColor}) colorData1 := []string{"TestCOLOR1Merge", "HelloCol2 - COLOR1", "HelloCol3 - COLOR1", "HelloCol4 - COLOR1"} colorData2 := []string{"TestCOLOR2Merge", "HelloCol2 - COLOR2", "HelloCol3 - COLOR2", "HelloCol4 - COLOR2"} for i, row := range data { if i == 4 { table.Rich(colorData1, []tablewriter.Colors{tablewriter.Colors{}, tablewriter.Colors{tablewriter.Normal, tablewriter.FgCyanColor}, tablewriter.Colors{tablewriter.Bold, tablewriter.FgWhiteColor}, tablewriter.Colors{}}) table.Rich(colorData2, []tablewriter.Colors{tablewriter.Colors{tablewriter.Normal, tablewriter.FgMagentaColor}, tablewriter.Colors{}, tablewriter.Colors{tablewriter.Bold, tablewriter.BgRedColor}, tablewriter.Colors{tablewriter.FgHiGreenColor, tablewriter.Italic, tablewriter.BgHiCyanColor}}) } table.Append(row) } table.SetAutoMergeCells(true) table.Render()

运行效果:

Table cells with Color

2.9 设置表格标题

示例代码:

go
data := [][]string{ []string{"A", "The Good", "500"}, []string{"B", "The Very very Bad Man", "288"}, []string{"C", "The Ugly", "120"}, []string{"D", "The Gopher", "800"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Name", "Sign", "Rating"}) table.SetCaption(true, "Movie ratings.") for _, v := range data { table.Append(v) } table.Render() // Send output

标题文本会根据渲染表格的总宽度自动换行。

运行效果:

+------+-----------------------+--------+ | NAME | SIGN | RATING | +------+-----------------------+--------+ | A | The Good | 500 | | B | The Very very Bad Man | 288 | | C | The Ugly | 120 | | D | The Gopher | 800 | +------+-----------------------+--------+ Movie ratings.

2.10 设置 NoWhiteSpace 和 TablePadding 选项

示例代码:

go
data := [][]string{ {"node1.example.com", "Ready", "compute", "1.11"}, {"node2.example.com", "Ready", "compute", "1.11"}, {"node3.example.com", "Ready", "compute", "1.11"}, {"node4.example.com", "NotReady", "compute", "1.11"}, } table := tablewriter.NewWriter(os.Stdout) table.SetHeader([]string{"Name", "Status", "Role", "Version"}) table.SetAutoWrapText(false) table.SetAutoFormatHeaders(true) table.SetHeaderAlignment(tablewriter.ALIGN_LEFT) table.SetAlignment(tablewriter.ALIGN_LEFT) table.SetCenterSeparator("") table.SetColumnSeparator("") table.SetRowSeparator("") table.SetHeaderLine(false) table.EnableBorder(false) table.SetTablePadding("\t") // pad with tabs table.SetNoWhiteSpace(true) table.AppendBulk(data) // Add Bulk Data table.Render()

运行效果:

NAME STATUS ROLE VERSION node1.example.com Ready compute 1.11 node2.example.com Ready compute 1.11 node3.example.com Ready compute 1.11 node4.example.com NotReady compute 1.11
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:蒋固金

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!