Microsoft Excel是Microsoft為使用Windows和Apple Macintosh操作系統(tǒng)的電腦編寫(xiě)的一款電子表格軟件。直觀的界面、出色的計(jì)算功能和圖表工具,再加上成功的市場(chǎng)營(yíng)銷,使Excel成為最流行的個(gè)人計(jì)算機(jī)數(shù)據(jù)處理軟件。 本文將指導(dǎo)您如何將Excel中工作簿中每個(gè)工作表保存為的單獨(dú)CSV文件。如何在Excel 2013/2016中保存多個(gè)或所有工作表為單獨(dú)的csv文件。如何使用Excel中的VBA宏導(dǎo)出所有工作表為單獨(dú)的文本文件。
導(dǎo)出每個(gè)工作表為單獨(dú)的CSV文件假設(shè)您有一個(gè)包含四個(gè)不同工作表的工作簿,sheet1,sheet2,sheet3和sheet4。并且您希望每個(gè)工作表都導(dǎo)出為單獨(dú)的CSV文件,并且每個(gè)CSV文件的文件名與其所在的工作簿的工作表名相同。要實(shí)現(xiàn)導(dǎo)出多個(gè)工作表為單獨(dú)的csv文件,您必須使用VBA宏將當(dāng)前工作簿中的所有工作表保存為單獨(dú)的csv文件。執(zhí)行下面的步驟: 步驟1:打開(kāi)您的Excel工作簿,然后單擊開(kāi)發(fā)工具選項(xiàng)卡下的“Visual Basic”命令,或者只需按“ALT + F11”快捷方式。 步驟2:然后將出現(xiàn)“Visual Basic編輯器”窗口。 步驟3:?jiǎn)螕簟安迦搿?– >“模塊”以創(chuàng)建新模塊。 步驟4:將以下VBA代碼粘貼到代碼窗口中。然后單擊“保存”按鈕。 Public Sub SaveWorksheetsAsCsv()Dim WS As Excel.WorksheetDim SaveToDirectory As StringDim CurrentWorkbook As StringDim CurrentFormat As Long CurrentWorkbook = ThisWorkbook.FullName CurrentFormat = ThisWorkbook.FileFormat' Store current details for the workbook SaveToDirectory = "D:\tmp\" For Each WS In ThisWorkbook.Worksheets WS.SaveAs SaveToDirectory & WS.Name, xlCSV Next Application.DisplayAlerts = False ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat Application.DisplayAlerts = True' Temporarily turn alerts off to prevent the user being prompted' about overwriting the original file.End Sub? 步驟5:返回當(dāng)前工作表,然后運(yùn)行上面的excel宏。點(diǎn)擊執(zhí)行按鈕。 步驟6:您將看到所有工作表已轉(zhuǎn)換為指定目錄中的單個(gè)CSV文件。此示例將csv文件保存到D:tmp目錄中。您需要根據(jù)需要更改變量“SaveToDirectory”。或者您也可以在磁盤(pán)D分區(qū)中創(chuàng)建一個(gè)名為tmp的臨時(shí)目錄。 導(dǎo)出每個(gè)工作表為單獨(dú)的文本文件如果只想將每個(gè)工作表轉(zhuǎn)換為活動(dòng)工作簿的單獨(dú)文本文件,還可以使用Excel VBA宏來(lái)實(shí)現(xiàn)結(jié)果。只需使用以下VBA代碼: Public Sub SaveWorksheetsAsCsv()Dim WS As Excel.WorksheetDim SaveToDirectory As StringDim CurrentWorkbook As StringDim CurrentFormat As Long CurrentWorkbook = ThisWorkbook.FullName CurrentFormat = ThisWorkbook.FileFormat' Store current details for the workbook SaveToDirectory = "D:\tmp\" For Each WS In ThisWorkbook.Worksheets WS.SaveAs SaveToDirectory & WS.Name & ".txt" Next Application.DisplayAlerts = False ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=xlText Application.DisplayAlerts = True' Temporarily turn alerts off to prevent the user being prompted' about overwriting the original file.End Sub? Excel整體界面趨于平面化,顯得清新簡(jiǎn)潔。流暢的動(dòng)畫(huà)和平滑的過(guò)渡,帶來(lái)不同以往的使用體驗(yàn)。 |
溫馨提示:喜歡本站的話,請(qǐng)收藏一下本站!