用 VB.net,VBA 两种方式 读取单元格内的 换行数据,并出力到 CSV文件
需求
如下图所示,为了生成csv文件导入数据库,需要将下图 的 1 和 2 拼接成 如下 3 所示的一行数据,
开头为 1 ,往后为 2 的换行数据 将换行符 替换为 如 3 所示的<br>
, 同时 1 和 2 用双引号 括起来,中间用 逗号 隔开。
VB.net 代码如下
Imports System.IOClass MainWindowPrivate Sub Button_Click(sender As Object, e As RoutedEventArgs)Dim AppXls As Microsoft.Office.Interop.Excel.Application '声明Excel对象Dim AppWokBook As Microsoft.Office.Interop.Excel.Workbook '声明工作簿对象Dim AppSheet As Microsoft.Office.Interop.Excel.Worksheet '声明工作表对象AppXls = New Microsoft.Office.Interop.Excel.Application '实例化Excel对象AppXls.Workbooks.Open("C:\wang\VB_family\VBA\tool\ReadExcelTest.xlsx") '打开已经存在的EXCEL文件AppXls.Visible = False '使Excel不可见AppWokBook = AppXls.Workbooks(1) 'AppWokBook对象指向工作簿"C:\wang\VB系列\VBA\tool\ReadExcelTest.xlsx"AppSheet = AppWokBook.Sheets("Sheet1") 'AppSheet对象指向AppWokBook对象中的表“Sheet1”,即:"C:\wang\VB系列\VBA\tool\ReadExcelTest.xlsx"中的表“Sheet1”' CSV文件路径Dim i, tDim str As StringDim str_Tmp As String = ""Dim arr = AppSheet.UsedRange.ValueDim csvFilePath As String = "C:\wang\VB_family\VBA\tool\file.csv"' 创建CSV文件并写入数据Using writer As New StreamWriter(csvFilePath)' 写入标题行writer.WriteLine("msgId, msgInfo")' 写入数据行For i = 5 To 12For Each t In Split(arr(i, 8), Chr(10)) '拆分第8列,如果第8列是单行,循环只执行一次If str_Tmp = "" Thenstr_Tmp = tElsestr_Tmp = str_Tmp & "<br>" & tEnd IfNext t'生成第i行数据str = """" & arr(i, 7) & """," & """" & str_Tmp & """"writer.WriteLine(str) '写入第i行数据str = "" '初始化数据str_Tmp = "" '初始化数据Next iEnd UsingMsgBox("CSV file generated successfully.")Console.WriteLine("CSV file generated successfully.")Console.ReadLine()'使用完毕必须关闭EXCEL,并退出AppXls.ActiveWorkbook.Close(SaveChanges:=True)AppXls.Quit()End SubEnd Class
VBA 代码如下
Attribute VB_Name = "模块1"
Option Explicit
Sub 宏1()Dim arr, i, tDim rowData_tmp As StringDim rowData As StringDim fileNumber As IntegerDim csvFilePath As Stringarr = activesheet.usedrangerowData_tmp = ""csvFilePath = "C:\wang\VB_family\VBA\tool\file_vba.csv"' 打开CSV文件以进行写入fileNumber = FreeFileOpen csvFilePath For Output As #fileNumber' 写入数据行rowData = "msgId, msgInfo" ' CSV文件的列标题Print #fileNumber, rowDataFor i = 5 To 12For Each t In Split(arr(i, 8), Chr(10)) '拆分第8列,如果第8列是单行,循环只执行一次If rowData_tmp = "" ThenrowData_tmp = tElserowData_tmp = rowData_tmp & "<br>" & tEnd IfNext t'生成第i行数据rowData = """" & arr(i, 7) & """," & """" & rowData_tmp & """"Print #fileNumber, rowData '写入第i行数据rowData = "" '初始化数据rowData_tmp = "" '初始化数据Next i' 关闭CSV文件Close #fileNumberMsgBox "CSV文件已创建成功!"End Sub
生成文件如下
file_vba.csv
msgId, msgInfo
"H220 G663","aaaaaaaaaaaaaaa1<br>bbbbbbbbbbbbbb1<br>cccccccccccccccc1"
"H220 G664","aaaaaaaaaaaaaaa2<br>bbbbbbbbbbbbbb2<br>cccccccccccccccc2"
"H220 G665","aaaaaaaaaaaaaaa3<br>bbbbbbbbbbbbbb3<br>cccccccccccccccc3"
"H220 G666","aaaaaaaaaaaaaaa4<br>bbbbbbbbbbbbbb4<br>cccccccccccccccc4"
"H220 G667","aaaaaaaaaaaaaaa5<br>bbbbbbbbbbbbbb5<br>cccccccccccccccc5"
"H220 G668","aaaaaaaaaaaaaaa6<br>bbbbbbbbbbbbbb6<br>cccccccccccccccc6"
"H220 G669","aaaaaaaaaaaaaaa7<br>bbbbbbbbbbbbbb7<br>cccccccccccccccc7"
"H220 G670","aaaaaaaaaaaaaaa8<br>bbbbbbbbbbbbbb8<br>cccccccccccccccc8"
总结
1 VB.NET Core WPF是一种用于开发Windows Presentation Foundation(WPF)
应用程序的框架。它是基于VB.NET语言的.NET Core平台上的一个开发框架。
WPF是一种用于创建图形用户界面(GUI)的技术,它提供了许多丰富的可视化和交互功能。通过使用VB.NET语言和WPF,开发人员可以创建现代化和可定制的桌面应用程序。
在使用VB.NET Core WPF时,您可以利用.NET Core平台的许多优势,例如跨平台开发和高性能。您可以使用Visual Studio等集成开发环境来创建、调试和发布您的VB.NET Core WPF应用程序。
2 由于用到了 Microsoft.Office.Interop.Excel ,需要引入以下依赖。