我有一个Excel文件 包含 报名号,姓名,身份证号,证书号码(证书尾4位按照0001 0002 0003排序),等各类信息,数据量几千条,每一条信息对应一个以报名号命名的JPG图片文件,如何批量更名图片名字为证书号码尾4位+姓名+身份证号.jpg
或者说如何自定义根据EXCEL文件的列组成的文件 批量更名为另外一个组合的名字。
此主题相关图片如下:企业微信截图_17275933354564.png
[此贴子已经被作者于2024/9/29 15:03:46编辑过]
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