📅  最后修改于: 2023-12-03 14:48:17.345000             🧑  作者: Mango
VBA文本转通用是一个基于VBA开发的工具,能够将指定格式的文本数据转换成通用的格式,以便于在不同的应用场景中进行使用。
VBA文本转通用提供了以下几个功能:
Sub CsvToExcel()
'打开CSV文件
Dim csvFilePath As String: csvFilePath = "C:\test.csv"
Open csvFilePath For Binary Access Read As #1
csvData = Space$(LOF(1))
Get #1, , csvData
Close #1
'转换为Excel表格格式
excelData = CsvToExcel(csvData)
'打开Excel文件
Dim excelFilePath As String: excelFilePath = "C:\test.xlsx"
Set excelApp = CreateObject("Excel.Application")
Set excelWorkbook = excelApp.Workbooks.Open(excelFilePath)
'将数据写入Excel表格
excelWorkbook.Sheets(1).Range("A1").Value = excelData
'保存并关闭Excel文件
excelWorkbook.Save
excelWorkbook.Close
excelApp.Quit
End Sub
Sub ExcelToJson()
'打开Excel文件
Dim excelFilePath As String: excelFilePath = "C:\test.xlsx"
Set excelApp = CreateObject("Excel.Application")
Set excelWorkbook = excelApp.Workbooks.Open(excelFilePath)
Set excelSheet = excelWorkbook.Sheets(1)
'转换为JSON格式
jsonData = ExcelToJson(excelSheet)
'保存JSON文件
Dim jsonFilePath As String: jsonFilePath = "C:\test.json"
Open jsonFilePath For Output As #1
Print #1, jsonData
Close #1
'关闭Excel文件
excelWorkbook.Close
excelApp.Quit
End Sub
Sub XmlToExcel()
'打开XML文件
Dim xmlFilePath As String: xmlFilePath = "C:\test.xml"
Open xmlFilePath For Binary Access Read As #1
xmlData = Space$(LOF(1))
Get #1, , xmlData
Close #1
'转换为Excel表格格式
excelData = XmlToExcel(xmlData)
'打开Excel文件
Dim excelFilePath As String: excelFilePath = "C:\test.xlsx"
Set excelApp = CreateObject("Excel.Application")
Set excelWorkbook = excelApp.Workbooks.Open(excelFilePath)
'将数据写入Excel表格
excelWorkbook.Sheets(1).Range("A1").Value = excelData
'保存并关闭Excel文件
excelWorkbook.Save
excelWorkbook.Close
excelApp.Quit
End Sub
Sub ExcelToHtml()
'打开Excel文件
Dim excelFilePath As String: excelFilePath = "C:\test.xlsx"
Set excelApp = CreateObject("Excel.Application")
Set excelWorkbook = excelApp.Workbooks.Open(excelFilePath)
Set excelSheet = excelWorkbook.Sheets(1)
'转换为HTML格式
htmlData = ExcelToHtml(excelSheet)
'保存HTML文件
Dim htmlFilePath As String: htmlFilePath = "C:\test.html"
Open htmlFilePath For Output As #1
Print #1, htmlData
Close #1
'关闭Excel文件
excelWorkbook.Close
excelApp.Quit
End Sub