Golang Cli 工具-拆分xlsx文件
功能描述:拆分拥有多个 sheet 的xlsx文件,每一个sheet都保存为单独的文件。使用方法:
- 需要 Go 开发环境,Go 版本大于等于 1.21
- 下载代码后进入代码根目录,执行 go build
使用:目标文件为 examples.xlsx
xlsxsplit split -f examples.xlsx
核心代码:
- 位置:xlsxplit/cmd/version.go
展示:
```go
var splitCmd = &cobra.Command{
Use: "split",
Short: "Split xlsx file into multiple files",
Example: "splitxlsx -f examples.xlsx",
Run: func(cmd *cobra.Command, args []string) {
xlsxFile := file
// Open the xlsx file
f, err := excelize.OpenFile(xlsxFile)
if err != nil {
log.Fatalf("Error opening file: %v", err)
}
// Iterate through each sheet
for _, sheetName := range f.GetSheetList() {
log.Debugf("Splitting sheet: %s", sheetName)
// Create a new xlsx file
newFileName := sheetName + ".xlsx"
newFile := excelize.NewFile()
// 创建新sheet
if err := newFile.SetSheetName("Sheet1", sheetName); err != nil {
log.Fatalf("Error creating sheet: %v", err)
}
// Copy the content of the current sheet into the new file
rows, err := f.GetRows(sheetName)
if err != nil {
log.Fatalf("Error getting rows from sheet: %v", err)
}
newFile.Styles = f.Styles
// Retrieve all comments
comments, err := f.GetComments(sheetName)
if err != nil {
log.Fatalf("Error getting comments from sheet: %v", err)
}
commentMap := make(mapexcelize.Comment, len(comments))
for i := range comments {
commentMap.Cell] = comments
}
for rowIndex, row := range rows {
log.Debugf("Row %d", rowIndex)
h, err := f.GetRowHeight(sheetName, rowIndex+1)
if err != nil {
log.Fatalf("Error getting row height: %v", err)
}
err = newFile.SetRowHeight(sheetName, rowIndex+1, h)
if err != nil {
log.Fatalf("Error setting row height: %v", err)
}
for colIndex, cellValue := range row {
cellName, err := excelize.CoordinatesToCellName(colIndex+1, rowIndex+1)
if err != nil {
log.Fatalf("Error converting coordinates: %v", err)
}
colName, err := excelize.ColumnNumberToName(colIndex + 1)
if err != nil {
log.Fatalf("Error converting column number to name: %v", err)
}
// Set the dimensions of each cell
width, err := f.GetColWidth(sheetName, colName)
if err != nil {
log.Fatalf("Error getting column width: %v", err)
}
err = newFile.SetColWidth(sheetName, colName, colName, width)
if err != nil {
log.Fatalf("Error setting column width: %v", err)
}
// Set the value of a cell
if err := newFile.SetCellValue(sheetName, cellName, cellValue); err != nil {
log.Fatalf("Error setting cell value: %v", err)
}
// Copy formulas
formula, err := f.GetCellFormula(sheetName, cellName)
if err != nil {
log.Fatalf("Error getting cell formula: %v", err)
}
if err := newFile.SetCellFormula(sheetName, cellName, formula); err != nil {
log.Fatalf("Error setting cell formula: %v", err)
}
// Copy comments
if comment, ok := commentMap; ok {
if err := newFile.AddComment(sheetName, comment); err != nil {
log.Fatalf("Error setting cell comment: %v", err)
}
}
// Copy cell formatting
styleID, err := f.GetCellStyle(sheetName, cellName)
if err != nil {
log.Fatalf("Error getting cell style: %v", err)
}
// Apply background color to cells in the new file
fillColor, err := f.GetStyle(styleID)
if err != nil || fillColor == nil {
log.Fatalf("Error getting cell background color: %v", err)
}
if err := newFile.SetCellStyle(sheetName, cellName, cellName, styleID); err != nil {
log.Fatalf("Error setting cell style: %v", err)
}
}
}
if err := saveNewFile(newFile, newFileName); err != nil {
log.Fatalf("Error saving new file: %v", err)
}
log.Infof("Sheet '%s' has been saved as '%s'\n", sheetName, newFileName)
}
log.Info("Finished!'\n")
},
}
```
源代码,链接: https://pan.baidu.com/s/1Lc6ZXWrefUdhEv4Sl6dAZQ?pwd=d8ed 提取码: d8ed
页:
[1]