英文:
Generate a CSV/Excel having specified values in dropDown option at that column in Golang
问题
如何在Golang中生成一个带有下拉选项的csv/excel文件,以便在该csv/excel文件的某些列中,添加/更新值的人可以使用下拉列表中提供的值。
例如:下面的excel文件中,航空公司列中有指定值的列表。
注:下面的excel文件是使用poi-4.0.0.jar库在java中生成的。
编辑1:我采取了不同的方法来解决上述问题,这些方法在这里共享https://go.dev/play/p/WD-qpXh6JNg
英文:
How to generate a csv/excel in Golang, where in some of the columns in that csv/excel will have dropdown option, so that whoever try to add/update values on that column can use the values available in the dropdown list.
For example: below excel file where on Airline column has list of specified values in dropdown.
P.S: below excel was generated in java using poi-4.0.0.jar library
Edit 1: I have taken different approach to solve above problem, that are shared here https://go.dev/play/p/WD-qpXh6JNg
答案1
得分: 0
尝试:
package main
import (
"github.com/tealeg/xlsx"
)
func main() {
file := xlsx.NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "列 1"
cell = row.AddCell()
cell.Value = "列 2 (下拉菜单)"
dropdownValues := []string{"你好", "再见"}
for i := 0; i < 10; i++ {
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = fmt.Sprintf("测试 %d", i)
cell = row.AddCell()
cell.SetDataValidationList(dropdownValues)
}
file.Save("output.xlsx")
}
英文:
Try:
package main
import (
"github.com/tealeg/xlsx"
)
func main() {
file := xlsx.NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "Column 1"
cell = row.AddCell()
cell.Value = "Column 2 (Dropdown)"
dropdownValues := []string{"hi", "there"}
for i := 0; i < 10; i++ {
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = fmt.Sprintf("Test %d", i)
cell = row.AddCell()
cell.SetDataValidationList(dropdownValues)
}
file.Save("output.xlsx")
}
答案2
得分: 0
在Excel中,下拉列表是使用DataValidations实现的,你可以使用这个库:https://github.com/qax-os/excelize/
以下是一个简单的示例:
// 创建新文件
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
index, err := f.NewSheet("Sheet2")
if err != nil {
fmt.Println(err)
return
}
// 创建新的数据验证
dv := excelize.NewDataValidation(true)
// 添加你想要的下拉列表项
myKeys := []string{"test", "test2"}
// 将下拉列表项添加到数据验证中
dv.SetDropList(myKeys)
// 指定下拉列表的单元格范围
dv.Sqref = "A1:A1"
// 将数据验证添加到工作表中
f.AddDataValidation("Sheet2", dv)
f.SetActiveSheet(index)
f.SaveAs("bok1.xlsx")
以上是一个使用excelize库创建Excel下拉列表的简单示例。
英文:
Drop Down list on Excel are DataValidations you can use this library :
https://github.com/qax-os/excelize/
Here is a simple example :
// create new file
f := excelize.NewFile()
defer func() {
if err := f.Close(); err != nil {
fmt.Println(err)
}
}()
index, err := f.NewSheet("Sheet2")
if err != nil {
fmt.Println(err)
return
}
// Create the new Data Validation
dv := excelize.NewDataValidation(true)
// add the keys that you want for your droplist
myKeys := []string{"test,test2"}
// add the keys to your drop list
dv.SetDropList(myKeys)
// What cells will be the dropdown list
dv.Sqref = "A1:A1"
// Add the datavalidation to your sheet
f.AddDataValidation("Sheet2", dv)
f.SetActiveSheet(index)
f.SaveAs("bok1.xlsx")
答案3
得分: 0
根据 @andrew-arrow 的建议,我查看了 tealeg 库,并成功获得了所需的结果,完整的代码在 这里。
再次感谢 @andrew-arrow
file := xlsx.NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "Column 1"
cell = row.AddCell()
cell.Value = "Column 2 (Dropdown)"
dropdownValues := []string{"AI | AirIndia", "UK | Vistara"}
for i := 0; i < 10; i++ {
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = fmt.Sprintf("Test %d", i)
cell = row.AddCell()
dd := xlsx.NewXlsxCellDataValidation(true)
dd.SetDropList(dropdownValues)
cell.SetDataValidation(dd)
}
file.Save("output.xlsx")
英文:
As suggested by @andrew-arrow, i took a look at tealeg library and i was able to get desired result, complete code is here.
Thanks again @andrew-arrow
file := xlsx.NewFile()
sheet, _ := file.AddSheet("Sheet1")
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = "Column 1"
cell = row.AddCell()
cell.Value = "Column 2 (Dropdown)"
dropdownValues := []string{"AI | AirIndia", "UK | Vistara"}
for i := 0; i < 10; i++ {
row := sheet.AddRow()
cell := row.AddCell()
cell.Value = fmt.Sprintf("Test %d", i)
cell = row.AddCell()
dd := xlsx.NewXlsxCellDataValidation(true)
dd.SetDropList(dropdownValues)
cell.SetDataValidation(dd)
}
file.Save("output.xlsx")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论