Imports System.IO
Imports Microsoft.Office.Interop
Imports System.Windows.Forms
Imports System.Data
Imports System.Text.RegularExpressions
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim folderBrowserDialog As New FolderBrowserDialog()
' 显示对话框并检查用户是否点击了“确定”按钮
If folderBrowserDialog.ShowDialog() = DialogResult.OK Then
' 将选定的文件夹路径设置为TextBox的Text属性
TextBox1.Text = folderBrowserDialog.SelectedPath
End If
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim openFileDialog As New OpenFileDialog()
openFileDialog.Filter = "Excel Files|*.xls;*.xlsx"
If openFileDialog.ShowDialog() = DialogResult.OK Then
Dim filePath As String = openFileDialog.FileName
ImportExcelToDataGridView(filePath)
End If
End Sub
Private Sub ImportExcelToDataGridView(filePath As String)
Dim excelApp As New Excel.Application()
Dim workbook As Excel.Workbook = excelApp.Workbooks.Open(filePath)
Dim worksheet As Excel.Worksheet = CType(workbook.Worksheets(1), Excel.Worksheet)
Dim rowCount As Integer = worksheet.UsedRange.Rows.Count
Dim colCount As Integer = worksheet.UsedRange.Columns.Count
DataGridView1.Rows.Clear()
DataGridView1.Columns.Clear()
For i As Integer = 1 To colCount
DataGridView1.Columns.Add(worksheet.Cells(1, i).Value, worksheet.Cells(1, i).Value)
Next
For i As Integer = 2 To rowCount
Dim dataRow As String() = New String(colCount - 1) {}
For j As Integer = 1 To colCount
dataRow(j - 1) = worksheet.Cells(i, j).Value
Next
DataGridView1.Rows.Add(dataRow)
Next
workbook.Close()
excelApp.Quit()
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
Dim folderBrowserDialog As New FolderBrowserDialog()
If folderBrowserDialog.ShowDialog() = DialogResult.OK Then
TextBox2.Text = folderBrowserDialog.SelectedPath
End If
End Sub
Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
For Each row As DataGridViewRow In Me.DataGridView1.Rows
Dim oldFileName As String = row.Cells("报名号").Value.ToString & ".jpg"
Dim newFileName As String = row.Cells("证书号码").Value.ToString().Substring(row.Cells("证书号码").Value.ToString().Length - 4) & _
row.Cells("姓名").Value.ToString & _
row.Cells("证件号码").Value.ToString().Substring(row.Cells("证件号码").Value.ToString().Length - 4) & ".jpg"
Dim oldFilePath As String = Path.Combine(Me.TextBox1.Text, oldFileName)
Dim newFilePath As String = Path.Combine(Me.TextBox2.Text, newFileName)
If File.Exists(oldFilePath) Then
File.Copy(oldFilePath, newFilePath)
End If
Next
End Sub
End Class