更新README.md,新增表格功能和页面设置功能的详细描述;在document.go中添加文档主体元素接口及其实现,支持段落和表格元素类型。

This commit is contained in:
zero
2025-05-29 14:02:16 +08:00
parent 9957fc7d57
commit 114dee00e0
6 changed files with 3382 additions and 3 deletions

233
README.md
View File

@@ -56,10 +56,102 @@ WordZero 是一个使用 Golang 实现的 Word 文档操作库,提供基础的
### 🚧 规划中功能 ### 🚧 规划中功能
#### 表格功能 #### 表格功能
- [ ] 表格创建和管理
- [ ] 单元格合并 ##### 表格基础操作
- [ ] 表格样式设置 - [x] 表格创建和初始化
- [x] 创建指定行列数的表格
- [x] 设置表格初始数据
- [x] 表格插入到文档指定位置
- [x] 表格结构管理
- [x] 插入行(指定位置、末尾、开头)
- [x] 删除行(单行、多行、指定范围)
- [x] 表格复制和剪切
- [x] 表格删除和清空
##### 单元格操作
- [x] 单元格内容管理
- [x] 单元格文本设置和获取
- [x] 单元格富文本格式支持
- [x] 单元格内容清空和重置
- [x] 单元格合并功能
- [x] 横向合并(合并列)
- [x] 纵向合并(合并行)
- [x] 区域合并(多行多列)
- [x] 取消合并操作
- [x] 合并状态查询
- [x] 单元格格式设置
- [x] 单元格文字格式(字体、大小、颜色)
- [x] 单元格对齐方式(水平、垂直对齐)
- [x] 单元格文字方向和旋转
- [x] 单元格内边距设置
##### 表格样式和外观
- [ ] 表格整体样式
- [ ] 预定义表格样式模板
- [ ] 自定义表格样式创建
- [ ] 表格主题色彩应用
- [ ] 表格样式继承和覆盖
- [ ] 表格边框设置 - [ ] 表格边框设置
- [ ] 外边框样式(线型、颜色、粗细)
- [ ] 内边框样式(网格线设置)
- [ ] 单元格边框独立设置
- [ ] 边框部分应用(顶部、底部、左右)
- [ ] 无边框表格支持
- [ ] 表格背景和填充
- [ ] 表格背景色设置
- [ ] 单元格背景色设置
- [ ] 奇偶行颜色交替
- [ ] 渐变背景支持(基础渐变)
- [ ] 图案填充支持
##### 表格布局和尺寸
- [x] 表格尺寸控制
- [x] 表格总宽度设置(固定宽度、相对宽度、自动宽度)
- [x] 列宽设置(固定宽度、相对宽度、自动调整)
- [ ] 行高设置(固定高度、最小高度、自动调整)
- [x] 单元格尺寸精确控制
- [ ] 表格对齐和定位
- [x] 表格页面对齐(左对齐、居中、右对齐)
- [ ] 表格文字环绕设置
- [ ] 表格相对定位
- [ ] 表格分页控制
- [ ] 表格跨页处理
- [ ] 标题行重复显示
- [ ] 表格分页符控制
- [ ] 避免分页的行设置
##### 表格数据处理
- [x] 数据导入导出
- [x] 二维数组数据绑定
- [x] 表格数据提取为数组
- [x] 批量数据填充
- [ ] 表格排序功能Word内置排序
- [ ] 单列排序(升序、降序)
- [ ] 多列排序
- [ ] 保持标题行不参与排序
##### 高级表格功能
- [ ] 表格标题和说明
- [ ] 表格标题设置(表格上方、下方)
- [ ] 表格标题编号自动生成
- [ ] 表格描述和备注
- [ ] 嵌套表格支持
- [ ] 单元格内嵌套表格
- [ ] 嵌套表格独立样式
- [ ] 表格模板功能
- [ ] 常用表格模板库
- [ ] 自定义模板保存
- [ ] 模板快速应用
##### 表格访问和查询
- [x] 表格查找和定位
- [x] 按索引获取表格
- [ ] 按标题查找表格
- [x] 表格位置信息获取
- [x] 单元格访问接口
- [x] 按行列索引访问
- [ ] 按范围批量访问
- [ ] 单元格遍历迭代器
#### 图片功能 #### 图片功能
- [ ] 图片插入 - [ ] 图片插入
@@ -67,6 +159,13 @@ WordZero 是一个使用 Golang 实现的 Word 文档操作库,提供基础的
- [ ] 图片位置设置 - [ ] 图片位置设置
- [ ] 多种图片格式支持JPG、PNG、GIF - [ ] 多种图片格式支持JPG、PNG、GIF
#### 页面设置功能
- [ ] 页面大小设置A4、Letter、Legal等标准尺寸
- [ ] 自定义页面尺寸
- [ ] 页面方向设置(纵向/横向)
- [ ] 页面边距设置(上下左右边距)
- [ ] 页面分节和分页控制
#### 高级功能 #### 高级功能
- [ ] 页眉页脚 - [ ] 页眉页脚
- [ ] 目录生成 - [ ] 目录生成
@@ -217,6 +316,121 @@ if err == nil {
doc.Save("styled.docx") doc.Save("styled.docx")
``` ```
### 高级表格功能
```go
package main
import (
"log"
"github.com/ZeroHawkeye/wordZero/pkg/document"
)
func main() {
doc := document.New()
// 1. 创建基础表格
config := &document.TableConfig{
Rows: 4,
Cols: 4,
Width: 8000,
Data: [][]string{
{"学号", "姓名", "语文", "数学"},
{"001", "张三", "85", "92"},
{"002", "李四", "78", "88"},
{"003", "王五", "90", "85"},
},
}
table := doc.AddTable(config)
// 2. 设置表头格式
headerFormat := &document.CellFormat{
TextFormat: &document.TextFormat{
Bold: true,
FontSize: 14,
FontColor: "FFFFFF", // 白色文字
FontName: "微软雅黑",
},
HorizontalAlign: document.CellAlignCenter,
VerticalAlign: document.CellVAlignCenter,
}
// 为第一行设置表头格式
for col := 0; col < 4; col++ {
table.SetCellFormat(0, col, headerFormat)
}
// 3. 单元格富文本
table.SetCellFormattedText(1, 1, "张三", &document.TextFormat{
Bold: true,
FontColor: "FF0000",
})
// 在同一单元格添加不同格式的文本
table.AddCellFormattedText(1, 1, " (优秀)", &document.TextFormat{
Italic: true,
FontColor: "00FF00",
FontSize: 10,
})
// 4. 单元格合并
// 水平合并
table.MergeCellsHorizontal(0, 2, 3) // 合并表头的"语文"和"数学"列
table.SetCellText(0, 2, "成绩")
// 垂直合并
table.MergeCellsVertical(1, 3, 0) // 合并学号列
table.SetCellText(1, 0, "2024级")
// 区域合并2x2区域
mergeTable := doc.AddTable(&document.TableConfig{Rows: 4, Cols: 4, Width: 6000})
mergeTable.MergeCellsRange(1, 2, 1, 2) // 合并中间2x2区域
mergeTable.SetCellText(1, 1, "合并区域")
// 5. 检查和取消合并
isMerged, _ := table.IsCellMerged(0, 2)
if isMerged {
// 获取合并信息
mergeInfo, _ := table.GetMergedCellInfo(0, 2)
log.Printf("合并信息: %+v", mergeInfo)
// 可以选择取消合并
// table.UnmergeCells(0, 2)
}
// 6. 内容和格式操作
// 清空内容但保留格式
table.ClearCellContent(1, 2)
table.SetCellText(1, 2, "90")
// 清空格式但保留内容
table.ClearCellFormat(1, 3)
// 7. 设置单元格内边距
table.SetCellPadding(0, 0, 10) // 10磅内边距
// 8. 设置单元格文字方向
// 设置垂直文字(从上到下)
table.SetCellTextDirection(1, 0, document.TextDirectionTB)
// 通过CellFormat设置完整格式包括文字方向
verticalFormat := &document.CellFormat{
TextFormat: &document.TextFormat{
Bold: true,
FontSize: 14,
},
HorizontalAlign: document.CellAlignCenter,
VerticalAlign: document.CellVAlignCenter,
TextDirection: document.TextDirectionTB, // 从上到下
}
table.SetCellFormat(1, 1, verticalFormat)
table.SetCellText(1, 1, "竖排文字")
doc.Save("advanced_table.docx")
}
```
## 项目结构 ## 项目结构
``` ```
@@ -391,6 +605,19 @@ go tool cover -html=coverage.out
## 更新日志 ## 更新日志
### 2025-05-29 单元格文字方向功能实现
- ✅ 实现单元格文字方向设置功能支持6种方向
- `TextDirectionLR`:从左到右(默认)
- `TextDirectionTB`:从上到下
- `TextDirectionBT`:从下到上
- `TextDirectionRL`:从右到左
- `TextDirectionTBV`:从上到下,垂直显示
- `TextDirectionBTV`:从下到上,垂直显示
- ✅ 添加 `SetCellTextDirection()``GetCellTextDirection()` 方法
- ✅ 扩展 `CellFormat` 结构支持文字方向属性
- ✅ 添加完整的测试用例和演示程序
- ✅ 更新README文档和使用示例
### 2025-05-29 测试修复 ### 2025-05-29 测试修复
- ✅ 修复 `TestComplexDocument` 测试调整期望段落数量从7改为6与实际创建的段落数量一致 - ✅ 修复 `TestComplexDocument` 测试调整期望段落数量从7改为6与实际创建的段落数量一致
- ✅ 修复 `TestErrorHandling` 测试:改进无效路径测试策略,确保在不同操作系统下都能正确测试错误处理 - ✅ 修复 `TestErrorHandling` 测试:改进无效路径测试策略,确保在不同操作系统下都能正确测试错误处理

View File

@@ -0,0 +1,373 @@
package main
import (
"fmt"
"log"
"github.com/ZeroHawkeye/wordZero/pkg/document"
)
func main() {
// 创建新文档
doc := document.New()
fmt.Println("=== 演示高级单元格操作功能 ===")
// 1. 创建表格
fmt.Println("\n1. 创建基础表格...")
config := &document.TableConfig{
Rows: 4,
Cols: 4,
Width: 8000,
Data: [][]string{
{"学号", "姓名", "语文", "数学"},
{"001", "张三", "85", "92"},
{"002", "李四", "78", "88"},
{"003", "王五", "90", "85"},
},
}
table := doc.AddTable(config)
if table == nil {
log.Fatal("创建表格失败")
}
// 2. 设置表头格式
fmt.Println("2. 设置表头格式...")
headerFormat := &document.CellFormat{
TextFormat: &document.TextFormat{
Bold: true,
FontSize: 14,
FontColor: "FFFFFF", // 白色文字
FontName: "微软雅黑",
},
HorizontalAlign: document.CellAlignCenter,
VerticalAlign: document.CellVAlignCenter,
}
// 为第一行的每个单元格设置表头格式
for col := 0; col < 4; col++ {
err := table.SetCellFormat(0, col, headerFormat)
if err != nil {
log.Printf("设置表头格式失败: %v", err)
}
}
// 3. 设置数据行格式
fmt.Println("3. 设置数据行格式...")
dataFormat := &document.CellFormat{
TextFormat: &document.TextFormat{
FontSize: 12,
FontName: "宋体",
},
HorizontalAlign: document.CellAlignCenter,
VerticalAlign: document.CellVAlignCenter,
}
// 为数据行设置格式
for row := 1; row < 4; row++ {
for col := 0; col < 4; col++ {
err := table.SetCellFormat(row, col, dataFormat)
if err != nil {
log.Printf("设置数据格式失败: %v", err)
}
}
}
// 4. 演示富文本单元格
fmt.Println("4. 添加富文本单元格...")
// 在表格下方添加一个新表格用于演示富文本
richTextConfig := &document.TableConfig{
Rows: 2,
Cols: 3,
Width: 8000,
}
richTable := doc.AddTable(richTextConfig)
// 在同一个单元格中添加不同格式的文本
err := richTable.SetCellFormattedText(0, 0, "标题:", &document.TextFormat{
Bold: true,
FontSize: 14,
})
if err != nil {
log.Printf("设置富文本失败: %v", err)
}
err = richTable.AddCellFormattedText(0, 0, "重要信息", &document.TextFormat{
Bold: true,
FontColor: "FF0000", // 红色
FontSize: 12,
})
if err != nil {
log.Printf("添加富文本失败: %v", err)
}
err = richTable.AddCellFormattedText(0, 0, "(普通文本)", &document.TextFormat{
FontSize: 10,
})
if err != nil {
log.Printf("添加富文本失败: %v", err)
}
// 5. 演示单元格合并
fmt.Println("5. 演示单元格合并...")
// 创建合并演示表格
mergeConfig := &document.TableConfig{
Rows: 5,
Cols: 5,
Width: 8000,
}
mergeTable := doc.AddTable(mergeConfig)
// 设置初始数据
for row := 0; row < 5; row++ {
for col := 0; col < 5; col++ {
text := fmt.Sprintf("R%dC%d", row+1, col+1)
err := mergeTable.SetCellText(row, col, text)
if err != nil {
log.Printf("设置单元格文本失败: %v", err)
}
}
}
// 水平合并合并第一行的第2-4列
err = mergeTable.MergeCellsHorizontal(0, 1, 3)
if err != nil {
log.Printf("水平合并失败: %v", err)
}
// 设置合并单元格的内容
err = mergeTable.SetCellFormattedText(0, 1, "水平合并单元格", &document.TextFormat{
Bold: true,
FontSize: 14,
FontColor: "0000FF",
})
if err != nil {
log.Printf("设置合并单元格内容失败: %v", err)
}
// 垂直合并合并第一列的第2-4行
err = mergeTable.MergeCellsVertical(1, 3, 0)
if err != nil {
log.Printf("垂直合并失败: %v", err)
}
// 设置垂直合并单元格的内容
err = mergeTable.SetCellFormattedText(1, 0, "垂直合并", &document.TextFormat{
Bold: true,
FontSize: 14,
FontColor: "00FF00",
})
if err != nil {
log.Printf("设置垂直合并单元格内容失败: %v", err)
}
// 区域合并合并右下角2x2区域
err = mergeTable.MergeCellsRange(3, 4, 3, 4)
if err != nil {
log.Printf("区域合并失败: %v", err)
}
// 设置区域合并单元格的内容
err = mergeTable.SetCellFormattedText(3, 3, "区域合并", &document.TextFormat{
Bold: true,
FontSize: 14,
FontColor: "FF00FF",
})
if err != nil {
log.Printf("设置区域合并单元格内容失败: %v", err)
}
// 6. 检查合并状态
fmt.Println("6. 检查合并状态...")
mergeInfo, err := mergeTable.GetMergedCellInfo(0, 1)
if err != nil {
log.Printf("获取合并信息失败: %v", err)
} else {
fmt.Printf("单元格(0,1)合并信息: %+v\n", mergeInfo)
}
// 7. 演示单元格内容和格式操作
fmt.Println("7. 演示内容和格式操作...")
// 创建操作演示表格
opConfig := &document.TableConfig{
Rows: 3,
Cols: 3,
Width: 6000,
}
opTable := doc.AddTable(opConfig)
// 设置带格式的内容
err = opTable.SetCellFormattedText(0, 0, "原始内容", &document.TextFormat{
Bold: true,
FontSize: 14,
FontColor: "FF0000",
})
if err != nil {
log.Printf("设置格式化内容失败: %v", err)
}
// 清空内容但保留格式
err = opTable.ClearCellContent(0, 1)
if err != nil {
log.Printf("清空内容失败: %v", err)
}
// 设置新内容
err = opTable.SetCellText(0, 1, "新内容")
if err != nil {
log.Printf("设置新内容失败: %v", err)
}
// 清空格式但保留内容
err = opTable.ClearCellFormat(0, 2)
if err != nil {
log.Printf("清空格式失败: %v", err)
}
// 8. 保存文档
fmt.Println("8. 保存文档...")
// 添加说明段落
doc.AddParagraph("本文档演示了wordZero库的高级单元格操作功能")
doc.AddParagraph("1. 第一个表格展示了基础表格创建和格式设置")
doc.AddParagraph("2. 第二个表格展示了富文本单元格功能")
doc.AddParagraph("3. 第三个表格展示了单元格合并功能")
doc.AddParagraph("4. 第四个表格展示了内容和格式操作功能")
// 9. 演示文字方向功能
fmt.Println("9. 演示文字方向功能...")
// 添加文字方向演示说明
doc.AddParagraph("")
doc.AddParagraph("=== 单元格文字方向演示 ===")
// 创建文字方向演示表格
directionConfig := &document.TableConfig{
Rows: 3,
Cols: 3,
Width: 8000,
}
directionTable := doc.AddTable(directionConfig)
// 演示不同的文字方向
directions := []struct {
name string
direction document.CellTextDirection
row int
col int
}{
{"水平从左到右", document.TextDirectionLR, 0, 0},
{"垂直从上到下", document.TextDirectionTB, 0, 1},
{"垂直从下到上", document.TextDirectionBT, 0, 2},
{"水平从右到左", document.TextDirectionRL, 1, 0},
{"垂直显示上下", document.TextDirectionTBV, 1, 1},
{"垂直显示下上", document.TextDirectionBTV, 1, 2},
{"混合格式文字", document.TextDirectionTB, 2, 0},
{"居中垂直文字", document.TextDirectionTB, 2, 1},
{"默认方向", document.TextDirectionLR, 2, 2},
}
for _, dir := range directions {
// 设置单元格文字
err := directionTable.SetCellText(dir.row, dir.col, dir.name)
if err != nil {
log.Printf("设置单元格文字失败: %v", err)
}
// 设置文字方向
err = directionTable.SetCellTextDirection(dir.row, dir.col, dir.direction)
if err != nil {
log.Printf("设置文字方向失败: %v", err)
}
// 为某些特殊单元格添加格式
if dir.row == 2 && dir.col == 0 {
// 混合格式演示
err = directionTable.SetCellFormattedText(dir.row, dir.col, "混合", &document.TextFormat{
Bold: true,
FontColor: "FF0000",
FontSize: 14,
})
if err != nil {
log.Printf("设置富文本失败: %v", err)
}
err = directionTable.AddCellFormattedText(dir.row, dir.col, "格式", &document.TextFormat{
Italic: true,
FontColor: "0000FF",
FontSize: 12,
})
if err != nil {
log.Printf("添加富文本失败: %v", err)
}
err = directionTable.AddCellFormattedText(dir.row, dir.col, "文字", &document.TextFormat{
FontColor: "00FF00",
FontSize: 10,
})
if err != nil {
log.Printf("添加富文本失败: %v", err)
}
}
if dir.row == 2 && dir.col == 1 {
// 居中垂直文字
format := &document.CellFormat{
TextFormat: &document.TextFormat{
Bold: true,
FontSize: 16,
},
HorizontalAlign: document.CellAlignCenter,
VerticalAlign: document.CellVAlignCenter,
TextDirection: document.TextDirectionTB,
}
err = directionTable.SetCellFormat(dir.row, dir.col, format)
if err != nil {
log.Printf("设置单元格格式失败: %v", err)
}
}
}
// 验证文字方向设置
fmt.Println("10. 验证文字方向设置...")
for _, dir := range directions {
actualDirection, err := directionTable.GetCellTextDirection(dir.row, dir.col)
if err != nil {
log.Printf("获取文字方向失败: %v", err)
continue
}
if actualDirection == dir.direction {
fmt.Printf("✓ 单元格(%d,%d) 文字方向设置正确: %s\n", dir.row, dir.col, dir.direction)
} else {
fmt.Printf("✗ 单元格(%d,%d) 文字方向不匹配,期望: %s实际: %s\n",
dir.row, dir.col, dir.direction, actualDirection)
}
}
// 11. 保存文档
fmt.Println("11. 保存文档...")
// 更新说明
doc.AddParagraph("• 单元格文字方向设置支持6种方向")
doc.AddParagraph("• 文字方向与其他格式的组合使用")
filename := "../output/cell_advanced_demo.docx"
err = doc.Save(filename)
if err != nil {
log.Fatalf("保存文档失败: %v", err)
}
fmt.Printf("文档已保存为: %s\n", filename)
fmt.Println("=== 演示完成 ===")
}

View File

@@ -0,0 +1,270 @@
// Package main 演示WordZero表格功能
package main
import (
"fmt"
"log"
"github.com/ZeroHawkeye/wordZero/pkg/document"
)
func main() {
fmt.Println("=== WordZero 表格功能演示 ===")
// 创建新文档
doc := document.New()
// 添加文档标题
title := doc.AddParagraph("WordZero 表格功能演示")
title.SetAlignment(document.AlignCenter)
// 演示1创建基础表格
fmt.Println("1. 创建基础表格...")
demonstrateBasicTable(doc)
// 演示2表格数据操作
fmt.Println("2. 演示表格数据操作...")
demonstrateTableDataOperations(doc)
// 演示3表格结构操作
fmt.Println("3. 演示表格结构操作...")
demonstrateTableStructureOperations(doc)
// 演示4表格复制和清空
fmt.Println("4. 演示表格复制和清空...")
demonstrateTableCopyAndClear(doc)
// 演示5表格删除操作
fmt.Println("5. 演示表格删除操作...")
demonstrateTableDeletion(doc)
// 保存文档
outputFile := "../output/table_demo.docx"
err := doc.Save(outputFile)
if err != nil {
log.Fatalf("保存文档失败: %v", err)
}
fmt.Printf("表格演示文档已保存到: %s\n", outputFile)
fmt.Println("=== 演示完成 ===")
}
// demonstrateBasicTable 演示基础表格创建
func demonstrateBasicTable(doc *document.Document) {
doc.AddParagraph("1. 基础表格创建")
// 创建一个3x4的表格包含初始数据
tableData := [][]string{
{"姓名", "年龄", "职位", "部门"},
{"张三", "28", "工程师", "技术部"},
{"李四", "32", "经理", "销售部"},
}
config := &document.TableConfig{
Rows: 3,
Cols: 4,
Width: 8000, // 8000磅宽度
Data: tableData,
}
table := doc.AddTable(config)
if table != nil {
fmt.Printf(" 创建表格成功:%dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
doc.AddParagraph("") // 空行分隔
}
// demonstrateTableDataOperations 演示表格数据操作
func demonstrateTableDataOperations(doc *document.Document) {
doc.AddParagraph("2. 表格数据操作")
// 创建一个简单的2x3表格
config := &document.TableConfig{
Rows: 2,
Cols: 3,
Width: 6000,
Data: [][]string{
{"产品", "价格", "库存"},
{"笔记本", "5000", "50"},
},
}
table := doc.AddTable(config)
if table == nil {
fmt.Println(" 创建表格失败")
return
}
// 设置单元格内容
err := table.SetCellText(0, 0, "商品名称")
if err != nil {
fmt.Printf(" 设置单元格失败: %v\n", err)
} else {
fmt.Println(" 设置单元格内容成功")
}
// 获取单元格内容
cellText, err := table.GetCellText(1, 1)
if err != nil {
fmt.Printf(" 获取单元格失败: %v\n", err)
} else {
fmt.Printf(" 单元格(1,1)内容: %s\n", cellText)
}
doc.AddParagraph("") // 空行分隔
}
// demonstrateTableStructureOperations 演示表格结构操作
func demonstrateTableStructureOperations(doc *document.Document) {
doc.AddParagraph("3. 表格结构操作")
// 创建基础表格
config := &document.TableConfig{
Rows: 2,
Cols: 2,
Width: 4000,
Data: [][]string{
{"A1", "B1"},
{"A2", "B2"},
},
}
table := doc.AddTable(config)
if table == nil {
fmt.Println(" 创建表格失败")
return
}
fmt.Printf(" 初始表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
// 插入行
err := table.InsertRow(1, []string{"A1.5", "B1.5"})
if err != nil {
fmt.Printf(" 插入行失败: %v\n", err)
} else {
fmt.Printf(" 插入行后表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
// 添加行到末尾
err = table.AppendRow([]string{"A末", "B末"})
if err != nil {
fmt.Printf(" 添加行失败: %v\n", err)
} else {
fmt.Printf(" 添加行后表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
// 插入列
err = table.InsertColumn(1, []string{"C1", "C1.5", "C2", "C末"}, 1000)
if err != nil {
fmt.Printf(" 插入列失败: %v\n", err)
} else {
fmt.Printf(" 插入列后表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
// 添加列到末尾
err = table.AppendColumn([]string{"D1", "D1.5", "D2", "D末"}, 1000)
if err != nil {
fmt.Printf(" 添加列失败: %v\n", err)
} else {
fmt.Printf(" 添加列后表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
doc.AddParagraph("") // 空行分隔
}
// demonstrateTableCopyAndClear 演示表格复制和清空
func demonstrateTableCopyAndClear(doc *document.Document) {
doc.AddParagraph("4. 表格复制和清空")
// 创建源表格
config := &document.TableConfig{
Rows: 2,
Cols: 2,
Width: 4000,
Data: [][]string{
{"原始1", "原始2"},
{"原始3", "原始4"},
},
}
sourceTable := doc.AddTable(config)
if sourceTable == nil {
fmt.Println(" 创建源表格失败")
return
}
// 复制表格
copiedTable := sourceTable.CopyTable()
if copiedTable != nil {
fmt.Println(" 表格复制成功")
// 修改复制的表格内容以区分
copiedTable.SetCellText(0, 0, "复制1")
copiedTable.SetCellText(0, 1, "复制2")
copiedTable.SetCellText(1, 0, "复制3")
copiedTable.SetCellText(1, 1, "复制4")
// 将复制的表格添加到文档
doc.Body.Tables = append(doc.Body.Tables, *copiedTable)
fmt.Println(" 复制的表格已添加到文档")
}
// 清空原表格内容
sourceTable.ClearTable()
fmt.Println(" 原表格内容已清空")
doc.AddParagraph("") // 空行分隔
}
// demonstrateTableDeletion 演示表格删除操作
func demonstrateTableDeletion(doc *document.Document) {
doc.AddParagraph("5. 表格删除操作")
// 创建测试表格
config := &document.TableConfig{
Rows: 4,
Cols: 4,
Width: 6000,
Data: [][]string{
{"1", "2", "3", "4"},
{"5", "6", "7", "8"},
{"9", "10", "11", "12"},
{"13", "14", "15", "16"},
},
}
table := doc.AddTable(config)
if table == nil {
fmt.Println(" 创建测试表格失败")
return
}
fmt.Printf(" 初始表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
// 删除第2行索引1
err := table.DeleteRow(1)
if err != nil {
fmt.Printf(" 删除行失败: %v\n", err)
} else {
fmt.Printf(" 删除行后表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
// 删除第2列索引1
err = table.DeleteColumn(1)
if err != nil {
fmt.Printf(" 删除列失败: %v\n", err)
} else {
fmt.Printf(" 删除列后表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
// 删除多行索引1到2
err = table.DeleteRows(1, 2)
if err != nil {
fmt.Printf(" 删除多行失败: %v\n", err)
} else {
fmt.Printf(" 删除多行后表格大小: %dx%d\n", table.GetRowCount(), table.GetColumnCount())
}
doc.AddParagraph("") // 空行分隔
}

View File

@@ -33,6 +33,22 @@ type Document struct {
type Body struct { type Body struct {
XMLName xml.Name `xml:"w:body"` XMLName xml.Name `xml:"w:body"`
Paragraphs []Paragraph `xml:"w:p"` Paragraphs []Paragraph `xml:"w:p"`
Tables []Table `xml:"w:tbl"`
}
// BodyElement 文档主体元素接口
type BodyElement interface {
ElementType() string
}
// ElementType 返回段落元素类型
func (p *Paragraph) ElementType() string {
return "paragraph"
}
// ElementType 返回表格元素类型
func (t *Table) ElementType() string {
return "table"
} }
// Paragraph 表示一个段落 // Paragraph 表示一个段落

1270
pkg/document/table.go Normal file

File diff suppressed because it is too large Load Diff

1223
pkg/document/table_test.go Normal file

File diff suppressed because it is too large Load Diff