Skip to content

Commit

Permalink
Merge pull request #35 from HazelnutParadise/csvxl
Browse files Browse the repository at this point in the history
Add `EachCsvToOneExcel` and `EachExcelToCsv` to `csvxl` package.
  • Loading branch information
TimLai666 authored Dec 2, 2024
2 parents 20f093d + 28b1c95 commit 04ec61d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Docs/csvxl.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ func ExcelToCsv(excelFile string, outputDir string, csvNames []string, onlyConta
- `csvNames`: Custom CSV file names. If not provided, the sheet name will be used as the default CSV file name.
- `onlyContainSheets`(optional): Only convert the specified sheets. If not provided, all sheets will be converted.

> [!NOTE]
> The CSV file names will be in the format of "ExcelFileName_SheetName.csv".

### 4. `EachExcelToCsv`

```go
func EachExcelToCsv(dir string, outputDir string)
```

**Description**: Splits each sheet in each Excel file in the given directory into individual CSV files.

- `dir`: The directory containing the input Excel files.
- `outputDir`: The directory where the split CSV files will be saved.

### 5. `EachCsvToOneExcel`

```go
func EachCsvToOneExcel(dir string, output string, encoding ...string)
```

**Description**: Converts each CSV file in the given directory to an Excel file.

- `dir`: The directory containing the input CSV files.
- `output`: The name of the output Excel file.
- `encoding`(optional): The encoding of the CSV files. If not provided, `UTF-8` encoding is used.

## Encoding Support

> [!NOTE]
Expand Down
75 changes: 75 additions & 0 deletions csvxl/convertDir.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package csvxl

import (
"os"
"path/filepath"
"strings"

"github.com/HazelnutParadise/insyra"
"github.com/xuri/excelize/v2"
)

// EachCsvToOneExcel converts each CSV file in the given directory to an Excel file.
// The output Excel file will be saved in the given output path.
func EachCsvToOneExcel(dir string, output string, encoding ...string) {
files, err := filepath.Glob(filepath.Join(dir, "*.csv"))
if err != nil {
insyra.LogWarning("csvxl.EachCsvToOneExcel: %s", err)
return
}

var csvFiles []string
csvFiles = append(csvFiles, files...)

CsvToExcel(csvFiles, nil, output, encoding...)
}

// EachExcelToCsv converts each Excel file in the given directory to CSV files.
// The output CSV files will be saved in the given output directory.
// The CSV files will be named as the Excel file name plus the sheet name plus ".csv",
// for example, "ExcelFileName_SheetName.csv".
func EachExcelToCsv(dir string, outputDir string) {
files, err := filepath.Glob(filepath.Join(dir, "*.xlsx"))
if err != nil {
insyra.LogWarning("csvxl.EachExcelToCsv: %s", err)
return
}

xlsx2Csv := func(excelFile string, outputDir string) {
f, err := excelize.OpenFile(excelFile)
if err != nil {
insyra.LogWarning("csvxl.ExcelToCsv: Failed to open Excel file %s: %v", excelFile, err)
return
}

excelFileName := filepath.Base(excelFile)
excelFileName = strings.TrimSuffix(excelFileName, ".xlsx")

sheets := f.GetSheetList()
for _, sheet := range sheets {

csvName := excelFileName + "_" + sheet + ".csv"

// Check if output directory exists, if not create it
if _, err := os.Stat(outputDir); os.IsNotExist(err) {
err := os.MkdirAll(outputDir, os.ModePerm)
if err != nil {
insyra.LogWarning("csvxl.ExcelToCsv: Failed to create directory %s: %v", outputDir, err)
return
}
}
outputCsv := filepath.Join(outputDir, csvName)
err := saveSheetAsCsv(f, sheet, outputCsv)
if err != nil {
insyra.LogWarning("csvxl.ExcelToCsv: Failed to save sheet %s as CSV: %v", sheet, err)
return
}
}

insyra.LogInfo("csvxl.ExcelToCsv: Successfully converted %d sheets to CSV files in %s.", len(sheets), outputDir)
}

for _, file := range files {
xlsx2Csv(file, outputDir)
}
}

0 comments on commit 04ec61d

Please sign in to comment.