Skip to content

Instantly share code, notes, and snippets.

@redrover9
Created August 28, 2021 01:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save redrover9/d11298474da8ef4789a9ac73ee92646c to your computer and use it in GitHub Desktop.
Save redrover9/d11298474da8ef4789a9ac73ee92646c to your computer and use it in GitHub Desktop.
package main
import (
"bufio"
"fmt"
"github.com/xuri/excelize/v2"
"log"
"os"
"strconv"
"strings"
)
func main() {
fmt.Print("Enter the name of the file to be copied: ")
reader := bufio.NewReader(os.Stdin)
fileInput, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
fmt.Print("Enter the name of the Sheet you would like to copy: ")
newReader := bufio.NewReader(os.Stdin)
sheetInput, err := newReader.ReadString('\n')
fileInput = strings.TrimSpace(fileInput)
sheetInput = strings.TrimSpace(sheetInput)
originalFile, err := excelize.OpenFile(fileInput)
if err != nil {
fmt.Println(err)
return
}
newFile := excelize.NewFile()
rows, err := originalFile.GetRows(sheetInput)
i := 0
for _, row := range rows {
for _, colCell := range row {
i++
cell := strconv.Itoa(i)
cell = "A" + cell
newFile.SetCellValue("Sheet1", cell, colCell)
}
}
if err := newFile.SaveAs("Book2.xlsx"); err != nil {
fmt.Println(err)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment