7,789
社区成员
发帖
与我相关
我的任务
分享
Option Explicit
Private xlApp As Excel.Application
Private xlSheet As Excel.Worksheet
Private FSO As Scripting.FileSystemObject
Private FS As TextStream
Private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Sub cmdBrowse_Click(Index As Integer)
With cdlShowFolder
Select Case Index
Case 0
.InitDir = App.Path
.DialogTitle = "请选择Excel工资表文件"
.FileName = ""
.Filter = "Excel 工作薄 (*.xls)|*.xls"
.ShowOpen
If .FileName <> "" Then txtPath(0) = .FileName
Case 1
.InitDir = App.Path
.DialogTitle = "请选择文本文件保存路径"
.FileName = "工资表"
.Filter = "文本文件 (*.txt)|*.txt"
.ShowSave
If .FileName <> "工资表" Then txtPath(1) = .FileName
End Select
End With
End Sub
Private Sub cmdSave_Click()
Dim strExcelFile As String, strTxtFile As String
Dim i As Long, ii As Long, iii As Long
Dim lngCol As Long, lngRow As Long
Dim strContent(2) As String, strContentLine As String
Dim strExploerFile As String
On Error GoTo ErrHandle
If Dir(txtPath(0)) = "" Then
MsgBox "工资表文件不存在,可能已被删除!", vbCritical, "错误信息"
Exit Sub
End If
strExcelFile = txtPath(0)
If ExcelOpened(strExcelFile) Then Exit Sub
DoEvents
strTxtFile = txtPath(1)
If Dir(strTxtFile) <> "" Then Kill strTxtFile
Set xlApp = New Excel.Application
xlApp.Workbooks.Open strExcelFile
Set xlSheet = xlApp.Workbooks(1).Worksheets(1)
With xlSheet
.Activate
lblAction.Caption = "正在检测工作薄内容....."
DoEvents
For i = 1 To .Columns.Count
If .Cells(2, i).Borders(xlEdgeRight).LineStyle < 0 And .Cells(2, i).Text = "" Then
lngCol = i
Exit For
End If
Next i
For i = 1 To lngCol
For ii = 2 To .Rows.Count
If .Cells(ii, i).Borders(xlEdgeRight).LineStyle < 0 And .Cells(ii, i).Text = "" Then
iii = ii
If iii > lngRow Then lngRow = iii
GoTo ForNext
End If
Next ii
ForNext:
Next i
lngRow = lngRow - 1: lngCol = lngCol - 1
lblAction.Caption = "共有" & lngCol & "列," & lngRow & "行!现在开始导出内容....."
DoEvents
pbrInfo.Min = 0: pbrInfo.Max = lngRow: pbrInfo.Visible = True
Set FSO = New Scripting.FileSystemObject
Set FS = FSO.OpenTextFile(strTxtFile, ForAppending, True)
For i = 3 To lngRow
strContentLine = ""
strContent(0) = .Cells(i, 1).Text
If CheckLength(strContent(0)) < 12 Then
strContent(0) = strContent(0) & String(20 - CheckLength(strContent(0)), " ")
End If
strContent(1) = .Cells(i, 2).Text
If CheckLength(strContent(1)) < 23 Then
strContent(1) = strContent(1) & String(30 - CheckLength(strContent(1)), " ")
End If
strContent(2) = Replace(.Cells(i, 3).Text, ".", "")
If CheckLength(strContent(2)) < 10 Then
strContent(2) = String(10 - CheckLength(strContent(2)), " ") & strContent(2)
End If
For ii = LBound(strContent) To UBound(strContent)
strContentLine = strContentLine & strContent(ii) & " "
Next ii
FS.WriteLine strContentLine
If pbrInfo.Value < pbrInfo.Max Then
pbrInfo.Value = pbrInfo.Value + 1
End If
Next i
FS.Close
Set FS = Nothing
Set FSO = Nothing
If MsgBox("文件导出完毕,是否现在打开?", vbYesNo + vbInformation, "询问") = vbYes Then
strExploerFile = IIf(Right(SystemPath, 1) <> "\", SystemPath & "\notepad.exe", SystemPath & "notepad.exe")
If Dir(strExploerFile) = "" Then
MsgBox "程序不能找到文本编辑器" & vbCrLf & vbCrLf & strTxtFile & "不能打开!", vbCritical, "出错信息"
Else
Shell strExploerFile & " " & strTxtFile, vbMaximizedFocus
End If
End If
lblAction.Visible = False: lblAction.Caption = ""
pbrInfo.Value = pbrInfo.Min: pbrInfo.Visible = False
End With
Exit Sub
ErrHandle:
MsgBox "发生错误,请检查你选择的文件格式是否符合工资表的格式!" & vbCrLf & vbCrLf & _
"工资表格式应该为:第一行为文件标题、第二行为内容标题、第三行以下为内容;总共分三列!", vbCritical, "出错信息"
MsgBox Err.Description
xlApp.Workbooks.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
End Sub
Private Sub Form_Unload(Cancel As Integer)
On Error Resume Next
xlApp.Workbooks.Close
xlApp.Quit
Set xlSheet = Nothing
Set xlApp = Nothing
End Sub
Private Function CheckLength(strString As String) As Long
Dim lngLength As Long, i As Long
lngLength = Len(strString)
For i = 1 To lngLength
If Asc(Mid(strString, i, 1)) >= 0 And Asc(Mid(strString, i, 1)) <= 255 Then
CheckLength = CheckLength + 1
Else
CheckLength = CheckLength + 2
End If
Next i
End Function
Private Function ExcelOpened(Optional strFileName As String) As Boolean
Dim MyXL As Object, strMessage As String
On Error Resume Next
Set MyXL = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Err.Clear
ExcelOpened = False
Else
If MyXL.Workbooks(1).FullName = "" Then
strMessage = "程序检测到你已经打开了一个Excel窗口,但没有打开任何文件!" & vbCrLf & vbCrLf
strMessage = strMessage & "是否将它关闭?"
If MsgBox(strMessage, vbYesNo + vbInformation, "询问") = vbYes Then
MyXL.Quit
ExcelOpened = False
Else
MsgBox "因为已有一个Excel窗口打开,程序无法继续工作!", vbExclamation, "出错信息"
ExcelOpened = True
End If
ElseIf MyXL.Workbooks(1).FullName = strFileName And strFileName <> "" Then
strMessage = "需要导出数据的工作薄已被Excel打开,是否需要保存并退出?"
If MsgBox(strMessage, vbYesNo + vbInformation, "询问") = vbYes Then
MyXL.Workbooks(1).Save
MyXL.Workbooks(1).Close
MyXL.Quit
ExcelOpened = False
Else
strMessage = "程序无法继续执行操作,请先关闭你正在操作的工作薄!"
MsgBox strMessage, vbExclamation, "出错信息"
ExcelOpened = True
End If
Else
strMessage = "程序检测到你已经打开了一个Excel窗口,并且已经打开了一个(或多个)文档!" & vbCrLf & vbCrLf
strMessage = strMessage & "你需要将它关闭才能继续工作!是否现在将它关闭?"
If MsgBox(strMessage, vbYesNo + vbInformation, "询问") = vbYes Then
MyXL.Workbooks(1).Save
MyXL.Workbooks(1).Close
MyXL.Quit
ExcelOpened = False
Else
MsgBox "因为已有一个Excel窗口打开并且存在一个文档,程序无法继续工作!", vbExclamation, "出错信息"
ExcelOpened = True
End If
End If
End If
Set MyXL = Nothing
End Function
Public Function SystemPath() As String
Dim s As String, l As Long
l = 255
s = String(l, 0)
GetSystemDirectory s, l
SystemPath = Left(s, InStr(s, Chr(0)) - 1)
End Function