大家好,欢迎来到IT知识分享网。
简介
开发步骤
解析源文件
内存调整
func isProvince(id int) bool {
str := strconv.Itoa(id) // if strings.Compare("0000", str[2:]) == 0 {
return true } return false } func isCity(id int) bool {
str := strconv.Itoa(id) // if strings.Compare("0000", str[2:]) != 0 && strings.Compare("00", str[4:]) == 0 {
return true } return false }
type Area struct {
areaId int areaName string areaParent int areaType int // 1-p 2-city 3-country areaList []Area }
理论上已完成了层级关系的处理。
考虑如何转化为xml字符串
type AreaList struct {
ProvinceList []Province `xml:"Province"` } type County struct {
ID int `xml:"areaId,attr"` Name string `xml:"areaName,attr"` ParentId int `xml:"-"` } type City struct {
ID int `xml:"areaId,attr"` Name string `xml:"areaName,attr"` ParentId int `xml:"-"` Areas []County `xml:"County"` } type Province struct {
ID int `xml:"areaId,attr"` Name string `xml:"areaName,attr"` Cites []City `xml:"City"` }
contentXML, err := xml.Marshal(areaList)
写到文件
package main import ( "encoding/xml" "fmt" "github.com/tealeg/xlsx" "os" "strconv" "strings" ) type AreaList struct {
ProvinceList []Province `xml:"Province"` } type County struct {
ID int `xml:"areaId,attr"` Name string `xml:"areaName,attr"` ParentId int `xml:"-"` } type City struct {
ID int `xml:"areaId,attr"` Name string `xml:"areaName,attr"` ParentId int `xml:"-"` Areas []County `xml:"County"` } type Province struct {
ID int `xml:"areaId,attr"` Name string `xml:"areaName,attr"` Cites []City `xml:"City"` } func main() {
areaList := AreaList{
ProvinceList: ExcelParse2("area.xlsx"), } contentXML, err := xml.Marshal(areaList) if err != nil {
fmt.Println("err ", err) } else {
WriteWithFileWrite("out.xml", string(contentXML)) } } type Area struct {
areaId int `xml:"areaId"` areaName string `xml:"areaName"` areaParent int `xml:"-"` areaType int // 1-p 2-city 3-country areaList []Area } //xlsx文件解析 func ExcelParse2(fileName string) []Province {
filePath := "upload/" + fileName xlFile, err := xlsx.OpenFile(filePath) if err != nil {
} //开辟除表头外的行数的数组内存 provinceArr := make([]Province, 0) areaArr := make([]Area, ) //遍历sheet for _, sheet := range xlFile.Sheets {
//遍历每一行 for rowIndex, row := range sheet.Rows {
//跳过第一行表头信息 if rowIndex == 0 {
continue } //遍历每一个单元 id, _ := row.Cells[0].Int() name := row.Cells[1].String() parent, _ := row.Cells[2].Int() aarea := Area{
areaId: id, areaName: name, areaParent: parent, } if isProvince(id) {
aarea.areaType = 1 //areass = append(areass, p) } else if isCity(id) {
aarea.areaType = 2 //areass[id] = c //areass = append(areass, c) } else {
aarea.areaType = 3 //areass = append(areass, a) } areaArr[aarea.areaId] = aarea } } //第一遍扫描将所有的Country放在City类型里 for _, coun := range areaArr {
if coun.areaType == 3 {
areaArr[coun.areaParent].areaList = append(areaArr[coun.areaParent].areaList, coun) } } //第二遍扫描将所有的City放在Province类型中 for _, ci := range areaArr {
if ci.areaType == 2 {
areaArr[ci.areaParent].areaList = append(areaArr[ci.areaParent].areaList, ci) } } //生成返回数组 for _, p := range areaArr {
if p.areaType == 1 {
citys := make([]City, 0) for _, c := range p.areaList {
countrys := make([]County, 0) for _, a := range c.areaList {
countrys = append(countrys, County{
ID: a.areaId, Name: a.areaName, }) } citys = append(citys, City{
ID: c.areaId, Name: c.areaName, Areas: countrys, }) } provinceArr = append(provinceArr, Province{
ID: p.areaId, Name: p.areaName, Cites: citys}) } } return provinceArr } func isProvince(id int) bool {
str := strconv.Itoa(id) // if strings.Compare("0000", str[2:]) == 0 {
return true } return false } func isCity(id int) bool {
str := strconv.Itoa(id) // if strings.Compare("0000", str[2:]) != 0 && strings.Compare("00", str[4:]) == 0 {
return true } return false } // //func isArea(id int) bool {
// if !isProvince(id) && !isCity(id) {
// return true // } // return false //} func WriteWithFileWrite(name, content string) {
fileObj, err := os.OpenFile(name, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644) if err != nil {
fmt.Println("Failed to open the file", err.Error()) os.Exit(2) } defer fileObj.Close() if _, err := fileObj.WriteString(content); err == nil {
fmt.Println("Successful writing to the file with os.OpenFile and *File.WriteString method.", content) } }
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://haidsoft.com/121746.html