Documentation
¶
Overview ¶
`csvxl` package provides functions for converting between CSV and Excel formats.
Key Features: - Convert multiple CSV files to a single Excel workbook with separate sheets - Split Excel workbooks into multiple CSV files - Automatic encoding detection for CSV files - Support for UTF-8, Big5, and other common text encodings - Batch processing of files in directories
Encoding Support: The package now supports automatic encoding detection by default. When no encoding is explicitly specified, the package will automatically detect the encoding of CSV files using charset detection algorithms. Supported encodings include: - UTF-8 (most common; used as fallback for certain detected encodings) - Big5 (Traditional Chinese) - ASCII - Various other encodings. Unknown or unrecognized encodings will be reported as errors by `DetectEncoding`.
You can still explicitly specify an encoding using the constants: - csvxl.UTF8 for UTF-8 encoding - csvxl.Big5 for Big5 encoding - csvxl.Auto for automatic detection (default)
Example usage:
// Convert CSV files to Excel with auto-detection (default)
if err := csvxl.CsvToExcel([]string{"file1.csv", "file2.csv"}, nil, "output.xlsx"); err != nil {
panic(err)
}
// Convert with explicit encoding
if err := csvxl.CsvToExcel([]string{"file1.csv"}, nil, "output.xlsx", csvxl.UTF8); err != nil {
panic(err)
}
// Read CSV file to string with auto-detection
csvString, err := csvxl.ReadCsvToString("myfile.csv")
if err != nil {
// Handle error
}
// Detect encoding of a specific file
encoding, err := insyra.DetectEncoding("myfile.csv")
if err != nil {
// Unknown encoding or detection error; handle appropriately
}
Index ¶
- Constants
- func AppendCsvToExcel(csvFiles []string, sheetNames []string, existingFile string, ...) error
- func CsvToExcel(csvFiles []string, sheetNames []string, output string, csvEncoding ...string) error
- func EachCsvToOneExcel(dir string, output string, encoding ...string) error
- func EachExcelToCsv(dir string, outputDir string) error
- func ExcelToCsv(excelFile string, outputDir string, csvNames []string, ...) error
- func ReadCsvToString(filePath string, encoding ...string) (string, error)
Constants ¶
const ( UTF8 = "utf-8" Big5 = "big5" Auto = "auto" )
CsvEncoding Options
Variables ¶
This section is empty.
Functions ¶
func AppendCsvToExcel ¶
func AppendCsvToExcel(csvFiles []string, sheetNames []string, existingFile string, csvEncoding ...string) error
Append CSV files to an existing Excel file, supporting custom sheet names. If the sheet name is not specified, the file name of the CSV file will be used. If the sheet is exists, it will be overwritten. If csvEncoding is not specified, auto-detection will be used.
func CsvToExcel ¶
Convert multiple CSV files to an Excel file, supporting custom sheet names. If the sheet name is not specified, the file name of the CSV file will be used. If csvEncoding is not specified, auto-detection will be used.
func EachCsvToOneExcel ¶ added in v0.1.0
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. If encoding is not specified, auto-detection will be used.
func EachExcelToCsv ¶ added in v0.1.0
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".
Types ¶
This section is empty.