{"id":3665,"date":"2026-03-25T10:33:09","date_gmt":"2026-03-25T02:33:09","guid":{"rendered":"https:\/\/www.urbin.top\/?p=3665"},"modified":"2026-03-25T10:33:09","modified_gmt":"2026-03-25T02:33:09","slug":"excel%e5%90%88%e5%b9%b6%e8%a1%a8%e6%a0%bc","status":"publish","type":"post","link":"https:\/\/www.urbin.top\/?p=3665","title":{"rendered":"Excel\u5408\u5e76\u8868\u683c"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u65e5\u5e38\u6709\u5f88\u591a\u65e5\u3001\u5468\u3001\u6708\u62a5\u8868\uff0c\u6216\u8005\u540c\u7c7b\u578b\u7684\u4e0d\u540c\u53d1\u9001\u65b9\u63d0\u4f9b\u7684\u62a5\u8868\uff0c\u8981\u600e\u6837\u5c06\u4e00\u5806\u6587\u4ef6\u7684\u6570\u636e\u6574\u5408\u5230\u4e00\u4e2a\u6587\u4ef6\u91cc\u5462\uff1f\u4e0b\u9762\u662f\u901a\u8fc7PYTHON\u548cVBA\u5b9e\u73b0\u8fd9\u4e00\u529f\u80fd\u7684\u4e24\u4e2a\u6848\u4f8b\uff1a<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">*\u5c0f\u6279\u91cf\u7684\u591a\u6587\u4ef6\u8868\u683c\u5408\u5e76\u63a8\u8350\u4f7f\u7528PowerQuery\uff0c\u66f4\u9002\u5408\u5c0f\u767d\u3002VBA\u9002\u5408\u5bf9\u7f16\u7a0b\u4e86\u89e3\u4e00\u4e9b\u7684\u670b\u53cb\uff0cpython\u53ef\u80fd\u9700\u8981\u5b66\u4e60\u4e00\u4e0b\u73af\u5883\u642d\u5efa\u5148\u3002<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Python\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-text-color has-link-color wp-elements-a0d90580bbede00da7f32c7378e52f74\" style=\"color:#b54800\"><code>import pandas as pd\nimport os\n\n# 1. Set the root folder path where your Excel files are located\nroot_path = r'C:\\Your\\Main\\Folder\\Path'\noutput_file = 'combined_data.xlsx'\n\nall_data = &#91;]\n\n# 2. Walk through all directories and subdirectories\nfor root, dirs, files in os.walk(root_path):\n    for file in files:\n        if file.endswith('.xlsx') or file.endswith('.xls'):\n            # Skip the output file if it already exists in the same folder\n            if file == output_file:\n                continue\n                \n            file_path = os.path.join(root, file)\n            print(f\"Processing: {file_path}\")\n            \n            # Read the first sheet (change header\/sheet_name if needed)\n            df = pd.read_excel(file_path)\n            \n            # Optional: Add a column to track which file the data came from\n            df&#91;'Source_File'] = file\n            \n            all_data.append(df)\n\n# 3. Merge everything and save\nif all_data:\n    combined_df = pd.concat(all_data, ignore_index=True)\n    combined_df.to_excel(output_file, index=False)\n    print(f\"Successfully combined {len(all_data)} files into {output_file}!\")\nelse:\n    print(\"No Excel files found.\")\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>VBA\u4ee3\u7801\uff1a<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-text-color has-link-color wp-elements-3750aa332888cfbb355ffc041e228ef1\" style=\"color:#b54800\"><code>Sub CombineExcelFilesFromSubfolders()\n    Dim FSO As Object\n    Dim rootFolder As String\n    Dim targetSheet As Worksheet\n    \n    ' 1. Set your main folder path here\n    rootFolder = \"C:\\Your\\Main\\Folder\\Path\\\" \n    \n    Set targetSheet = ThisWorkbook.Sheets(1)\n    targetSheet.Cells.Clear ' Clear previous data\n    \n    Set FSO = CreateObject(\"Scripting.FileSystemObject\")\n    \n    ' 2. Start the recursive search\n    Call ProcessFolder(FSO.GetFolder(rootFolder), targetSheet)\n    \n    MsgBox \"Done! All data combined.\", vbInformation\nEnd Sub\n\nSub ProcessFolder(Folder As Object, targetSheet As Worksheet)\n    Dim SubFolder As Object\n    Dim File As Object\n    Dim wbSource As Workbook\n    Dim lastRow As Long\n    Dim nextRow As Long\n\n    ' Loop through files in the current folder\n    For Each File In Folder.Files\n        If InStr(File.Name, \".xls\") > 0 And File.Name &lt;> ThisWorkbook.Name Then\n            Set wbSource = Workbooks.Open(File.Path)\n            \n            ' Find last row of source and next empty row in target\n            lastRow = wbSource.Sheets(1).Cells(Rows.Count, 1).End(xlUp).Row\n            nextRow = targetSheet.Cells(Rows.Count, 1).End(xlUp).Row + 1\n            If nextRow = 2 And targetSheet.Range(\"A1\") = \"\" Then nextRow = 1\n            \n            ' Copy data (assumes headers are in row 1; skips header after first file)\n            If nextRow = 1 Then\n                wbSource.Sheets(1).Rows(\"1:\" &amp; lastRow).Copy targetSheet.Rows(nextRow)\n            Else\n                wbSource.Sheets(1).Rows(\"2:\" &amp; lastRow).Copy targetSheet.Rows(nextRow)\n            End If\n            \n            wbSource.Close False\n        End If\n    Next\n\n    ' Recursive call for each subfolder\n    For Each SubFolder In Folder.SubFolders\n        ProcessFolder SubFolder, targetSheet\n    Next\nEnd Sub\n<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u65e5\u5e38\u6709\u5f88\u591a\u65e5\u3001\u5468\u3001\u6708\u62a5\u8868\uff0c\u6216\u8005\u540c\u7c7b\u578b\u7684\u4e0d\u540c\u53d1\u9001\u65b9\u63d0\u4f9b\u7684\u62a5\u8868\uff0c\u8981\u600e\u6837\u5c06\u4e00\u5806\u6587\u4ef6\u7684\u6570\u636e\u6574\u5408\u5230\u4e00\u4e2a\u6587\u4ef6\u91cc\u5462 [&hellip;]<\/p>\n","protected":false},"author":3,"featured_media":3589,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,5,7],"tags":[],"class_list":["post-3665","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-officeski","category-dataana","category-7"],"_links":{"self":[{"href":"https:\/\/www.urbin.top\/index.php?rest_route=\/wp\/v2\/posts\/3665","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.urbin.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.urbin.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.urbin.top\/index.php?rest_route=\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/www.urbin.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3665"}],"version-history":[{"count":4,"href":"https:\/\/www.urbin.top\/index.php?rest_route=\/wp\/v2\/posts\/3665\/revisions"}],"predecessor-version":[{"id":3669,"href":"https:\/\/www.urbin.top\/index.php?rest_route=\/wp\/v2\/posts\/3665\/revisions\/3669"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.urbin.top\/index.php?rest_route=\/wp\/v2\/media\/3589"}],"wp:attachment":[{"href":"https:\/\/www.urbin.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3665"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.urbin.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3665"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.urbin.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3665"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}