{"id":19193,"date":"2025-11-18T12:02:47","date_gmt":"2025-11-18T04:02:47","guid":{"rendered":"https:\/\/blog.ywdevops.cn\/?p=19193"},"modified":"2026-09-14T15:19:20","modified_gmt":"2026-09-14T07:19:20","slug":"excel","status":"publish","type":"post","link":"https:\/\/blog.ywdevops.cn\/index.php\/2025\/11\/18\/excel\/","title":{"rendered":"Golang\u5c06excel\u8f6c\u6362\u4e3atxt\u683c\u5f0f"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u5c06excel\u8f6c\u6362\u4e3atxt\u7684\u65b9\u6cd5\u5f88\u591a\uff0c\u5982\u679c\u901a\u8fc7\u811a\u672c\u6765\u5b9e\u73b0\uff0cpython\u53ef\u4ee5\uff0cgolang\u4e5f\u53ef\u4ee5\uff0c\u4e0b\u9762\u8bb0\u5f55\u4e0b\u5982\u4f55\u4f7f\u7528golang\u6765\u5c06excel\u8f6c\u6362\u4e3atxt\u6587\u4ef6\uff0c\u9700\u8981\u7528\u5230\u7684\u5e93\u4e3a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>github.com\/xuri\/excelize\/v2<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u5927\u81f4\u539f\u7406\u662f\u5148\u8bfb\u53d6excel\u7684\u6bcf\u4e2a\u5de5\u4f5c\u8868\uff0c\u4e5f\u5c31\u662fexcel\u4e0b\u9762\u7684\u90a3\u4e2asheet\uff0c\u5c06\u6240\u6709\u7684sheet\u8bfb\u53d6\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\u5207\u7247\uff0c\u6bcf\u4e2a\u5207\u7247\u7684\u5185\u5bb9\u5c31\u662f\u7531\u6bcf\u4e2asheet\u7ec4\u6210\u7684\u5b57\u7b26\u4e32\uff0c\u7136\u540e\u5faa\u73af\u8bfb\u53d6\u6bcf\u4e00\u884c\u6570\u636e\uff0c\u5728\u5199\u5165\u5230txt\u6587\u4ef6\u79cd\u5373\u53ef\uff0c\u8f6c\u6362\u4ee3\u7801excel.go\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>package main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"os\"\n\t\"strings\"\n\t\"github.com\/xuri\/excelize\/v2\"\n)\n\nfunc excelToTxt(excelPath, txtPath string) error {\n\t\/\/ \u6253\u5f00 Excel \u6587\u4ef6\n\tf, err := excelize.OpenFile(excelPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"\u65e0\u6cd5\u6253\u5f00 Excel \u6587\u4ef6: %v\", err)\n\t}\n\tdefer f.Close()\n\n\t\/\/ \u521b\u5efa\u6216\u6e05\u7a7a TXT \u6587\u4ef6\n\ttxtFile, err := os.Create(txtPath)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"\u65e0\u6cd5\u521b\u5efa TXT \u6587\u4ef6: %v\", err)\n\t}\n\tdefer txtFile.Close()\n\n\t\/\/ \u83b7\u53d6\u6240\u6709\u5de5\u4f5c\u8868\n\tsheets := f.GetSheetList()\n\n\t\/\/ \u904d\u5386\u6bcf\u4e2a\u5de5\u4f5c\u8868\n\tfor _, sheet := range sheets {\n\t\t\/\/ \u83b7\u53d6\u5de5\u4f5c\u8868\u4e2d\u7684\u6240\u6709\u884c,\u5b9e\u9645\u8bfb\u53d6\u7684\u662f\u6bcf\u4e2asheet\uff0c\u8bfb\u53d6\u540e\u4e3a\u5207\u7247\n\t\trows, err := f.GetRows(sheet)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"\u8bfb\u53d6\u5de5\u4f5c\u8868\u6570\u636e\u5931\u8d25: %v\", err)\n\t\t}\n\n\t\t\/\/ \u5199\u5165\u6bcf\u884c\u6570\u636e,rows\u4e5f\u662f\u5207\u7247, \u6bcf\u4e2a\u5143\u7d20\u4e5f\u662f\u4e00\u4e2a\u5207\u7247(\u8fd9\u4e2a\u5207\u7247\u5c31\u662f\u6bcf\u884c\u7684\u6570\u636e)\n\t\tfor _, row := range rows {\n                                \/*\n                                  \u8fd9\u4e00\u6b65\u7684\u76ee\u7684\u662f\u5c06\u8868\u4e2d\u6570\u636e\u6307\u5b9a\u7684\u5217\u7684\u5185\u5bb9\u524d\u540e\u7a7a\u683c\u53bb\u6389\u518d\u66ff\u6362\u56de\u539f\u6765\u6570\u636e\u4e2d(\u53ef\u4ee5\u907f\u514d\u8f6c\u6362\u4e3atxt\u540e\u6570\u636e\u4e0d\u51c6\u786e\uff0c\u6216\u8005\u5217\u540d\u4e0d\u5bf9\u9f50\u7684\u60c5\u51b5)\uff0crow\u5c31\u662f\u4e00\u4e2a\u5207\u7247\uff0c\u91cc\u9762\u5305\u542b\u6bcf\u4e00\u884c\u6570\u636e\uff0c\u5982\u679c\u6709\u591a\u4e2a\u5217\u8981\u53bb\u9664\u7a7a\u683c\uff0c\u5c31\u5199\u591a\u884c\n                                *\/\n\t\t\t        row&#91;2] = strings.TrimSpace(row&#91;2])\n                                \/\/row&#91;3] = strings.TrimSpace(row&#91;3])\n\t\t\t\t\/\/ \u5c06\u884c\u6570\u636e\u62fc\u63a5\u4e3a\u5b57\u7b26\u4e32\uff08\u7528\u5236\u8868\u7b26\u5206\u9694\uff09\n\t\t\t\tline := strings.Join(row, \"\\t\") + \"\\n\"\n\t\t\t\tif _, err := txtFile.WriteString(line); err != nil {\n\t\t\t\t\treturn fmt.Errorf(\"\u5199\u5165\u884c\u6570\u636e\u5931\u8d25: %v\", err)\n\t\t\t\t}\n\t\t}\n\t\tfmt.Println()\n\t\t\/\/ \u5206\u9694\u4e0d\u540c\u5de5\u4f5c\u8868\uff08\u53ef\u9009\uff09\n\t\tif _, err := txtFile.WriteString(\"\\n\"); err != nil {\n\t\t\treturn fmt.Errorf(\"\u5199\u5165\u5206\u9694\u7b26\u5931\u8d25: %v\", err)\n\t\t}\n\t}\n\treturn nil\n}\n\nfunc main() {\n\t\/\/ \u8f93\u5165\u548c\u8f93\u51fa\u6587\u4ef6\u8def\u5f84\n\texcelFile := \"input.xlsx\" \/\/ \u8f93\u5165\u7684 Excel \u6587\u4ef6\n\ttxtFile := \"output.txt\"   \/\/ \u8f93\u51fa\u7684 TXT \u6587\u4ef6\n\n\t\/\/ \u6267\u884c\u8f6c\u6362\n\tif err := excelToTxt(excelFile, txtFile); err != nil {\n\t\tlog.Fatalf(\"\u8f6c\u6362\u5931\u8d25: %v\", err)\n\t}\n\n\tlog.Println(\"\u8f6c\u6362\u6210\u529f\uff01\")\n}\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4e3e\u4f8b<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\u5b9a\u4e49\u4e00\u4e2adata.xlsx\u8868\u683c\u6587\u4ef6\uff0c\u5185\u5bb9\u5982\u4e0b\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"658\" height=\"277\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/6.png\" alt=\"\" class=\"wp-image-19201\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/6.png 658w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/6-300x126.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/6-600x253.png 600w\" sizes=\"auto, (max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5c06excel.go\u548cdata.xlsx\u6587\u4ef6\u653e\u5728\u540c\u7ea7\u76ee\u5f55\u4e0b\uff0c\u6267\u884cexcel.go\u6587\u4ef6\uff0c\u67e5\u770b\u751f\u6210\u7684a.txt\u6587\u4ef6\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"658\" height=\"308\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/7.png\" alt=\"\" class=\"wp-image-19202\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/7.png 658w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/7-300x140.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/7-600x281.png 600w\" sizes=\"auto, (max-width: 658px) 100vw, 658px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u5982\u679cexcel\u4e2d\u6709\u591a\u4e2asheet\uff0c\u90a3\u4e48\u6700\u7ec8\u7684\u8f6c\u6362\u7ed3\u679c\u4f1a\u5728\u540c\u4e00\u4e2atxt\u4e2d\uff0c\u5e76\u7528\u7a7a\u683c\u5206\u9694\u5f00\uff0c\u5982\u56fe\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"664\" height=\"346\" src=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/8.png\" alt=\"\" class=\"wp-image-19203\" srcset=\"https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/8.png 664w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/8-300x156.png 300w, https:\/\/blog.ywdevops.cn\/wp-content\/uploads\/2025\/11\/8-600x313.png 600w\" sizes=\"auto, (max-width: 664px) 100vw, 664px\" \/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>\u5c06excel\u8f6c\u6362\u4e3atxt\u7684\u65b9\u6cd5\u5f88\u591a\uff0c\u5982\u679c\u901a\u8fc7\u811a\u672c\u6765\u5b9e\u73b0\uff0cpython\u53ef\u4ee5\uff0cgolang\u4e5f\u53ef\u4ee5\uff0c\u4e0b\u9762\u8bb0\u5f55\u4e0b\u5982\u4f55\u4f7f [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":19196,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[111],"tags":[],"class_list":["post-19193","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-go"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":8}},"_links":{"self":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/19193","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/comments?post=19193"}],"version-history":[{"count":7,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/19193\/revisions"}],"predecessor-version":[{"id":21085,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/posts\/19193\/revisions\/21085"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media\/19196"}],"wp:attachment":[{"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/media?parent=19193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/categories?post=19193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ywdevops.cn\/index.php\/wp-json\/wp\/v2\/tags?post=19193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}