VB实现excel转txt,或者用vba,如何弄,能否写一个实例
VB实现excel转txt,或者用vba,如何弄,能否写一个实例
[解决办法]
Option Explicit'首先请添加对Excel Object 11的引用Private xlsApp As Excel.Application 'Excel应用对象Private xlsBook As Excel.Workbook 'Excel工作薄对象Private xlsSheet As Excel.Worksheet 'Excel工作表对象Private Type ExcelRow CellA As String * 5 CellB As String * 20 '定义为定长字符串或其他类型,这个看你的具体情况而定 CellC As String * 20 '每个列定义一个元素 CellD As String * 20 CellE As String * 20 CellF As String * 20End TypeDim RowMemo(1 To 1000) As ExcelRowPrivate Sub Command1_Click() Dim bolP As Boolean Dim intRow As Integer Dim intFileNo As Integer bolP = funOpenExcelFile(xlsApp, xlsBook, xlsSheet, App.Path & "\1.xls", "Sheet1", "", False) For intRow = 1 To 1000 If funReadCellText(xlsSheet, intRow, 1) = "" Then Exit For RowMemo(intRow).CellA = funReadCellText(xlsSheet, intRow, 1) RowMemo(intRow).CellB = funReadCellText(xlsSheet, intRow, 2) RowMemo(intRow).CellC = funReadCellText(xlsSheet, intRow, 3) RowMemo(intRow).CellD = funReadCellText(xlsSheet, intRow, 4) RowMemo(intRow).CellE = funReadCellText(xlsSheet, intRow, 5) RowMemo(intRow).CellF = funReadCellText(xlsSheet, intRow, 6) intFileNo = FreeFile Open App.Path & "\1.txt" For Random As intFileNo Len = Len(RowMemo(intRow)) Put intFileNo, intRow + 1, RowMemo(intRow) Close intFileNo Next intRow bolP = funCloseExcelFile(xlsApp, xlsBook, xlsSheet, False) End Sub'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'函数功能:打开指定的Excel文件'参数说明:xlsAPP:Excel应用对象' :xlsWork:Excel工作薄对象' :xlsSheet:Excel工作表对象' :strExcelFile:Excel文件路径' :strSheetName:工作表名' :strPWD:密码' :bolVisible:表的可见性'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Private Function funOpenExcelFile(ByRef xlsApp As Excel.Application, _ ByRef xlsWork As Excel.Workbook, _ ByRef xlsSheet As Excel.Worksheet, _ ByVal strExcelFile As String, _ ByVal strSheetName As String, _ ByVal strPWD As String, _ ByVal bolVisible As Boolean) As BooleanOn Error GoTo errFun funOpenExcelFile = False Set xlsApp = CreateObject("Excel.Application") Set xlsWork = xlsApp.Workbooks.Open(strExcelFile, , False, , strPWD, strPWD) Set xlsSheet = xlsBook.Worksheets(strSheetName) xlsSheet.Activate xlsApp.Visible = bolVisible funOpenExcelFile = True Exit FunctionerrFun: funOpenExcelFile = FalseEnd Function'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'函数功能:关闭指定的Excel文件'参数说明:xlsAPP:Excel应用对象' :xlsWork:Excel工作薄对象' :xlsSheet:Excel工作表对象' :bolSave:是否保存'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Private Function funCloseExcelFile(ByRef xlsApp As Excel.Application, _ ByRef xlsWork As Excel.Workbook, _ ByRef xlsSheet As Excel.Worksheet, _ ByVal bolSave As Boolean) As BooleanOn Error GoTo errFun If bolSave Then xlsBook.Save Set xlsSheet = Nothing xlsBook.Close Set xlsBook = Nothing Set xlsApp = Nothing funCloseExcelFile = True Exit FunctionerrFun: funCloseExcelFile = FalseEnd Function'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'函数功能:读取指定单元格的内容'参数说明:xlsSheet:工作表对象' :lngRow:行号' :lngCol:列号'%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%Private Function funReadCellText(ByRef xlsSheet As Excel.Worksheet, _ ByVal lngRow As Long, _ ByVal lngCol As Long) As String On Error GoTo errFun funReadCellText = "" If lngRow <= 0 Or lngCol <= 0 Then Exit Function funReadCellText = xlsSheet.Cells(lngRow, lngCol) Exit FunctionerrFun: funReadCellText = ""End Function