Imports Microsoft.Office.Interop.Excel
Imports System.Globalization
Imports System.Runtime.InteropServices
Public Class Form1
' دکمه انتخاب فایل اکسل
Private Sub BtnSelectExcl_Click(sender As Object, e As EventArgs) Handles BtnSelectExcl.Click
Using ofd As New OpenFileDialog()
ofd.Filter = "Excel Files|*.xlsx;*.xls"
ofd.Multiselect = True
If ofd.ShowDialog() = DialogResult.OK Then
TxtAddssExcl.Lines = ofd.FileNames
End If
End Using
End Sub
' دکمه شروع تبدیل
Private Sub btnstart_Click(sender As Object, e As EventArgs) Handles btnstart.Click
If TxtAddssExcl.Lines.Length = 0 Then
MessageBox.Show("هیچ فایل اکسل انتخاب نشده است.", "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
For Each filePath In TxtAddssExcl.Lines
Try
ConvertExcelDatesToShamsi(filePath)
Catch ex As Exception
MessageBox.Show($"خطا در پردازش فایل {filePath}:" & vbCrLf & ex.Message, "خطا", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Next
MessageBox.Show("عملیات تبدیل تاریخها به پایان رسید.", "اطلاع", MessageBoxButtons.OK, MessageBoxIcon.Information)
End Sub
Private Sub ConvertExcelDatesToShamsi(filePath As String)
Dim excelApp As Application = Nothing
Dim workbook As Workbook = Nothing
Dim worksheet As Worksheet = Nothing
Try
excelApp = New Application With {
.Visible = False,
.DisplayAlerts = False
}
workbook = excelApp.Workbooks.Open(filePath)
worksheet = CType(workbook.Sheets(1), Worksheet) ' فرض بر این است که دادهها در شیت اول است
' پیدا کردن آخرین ردیف پر شده در ستون A
Dim lastRow As Integer = worksheet.Cells(worksheet.Rows.Count, 1).End(XlDirection.xlUp).Row
If lastRow < 2 Then
MessageBox.Show("هیچ دادهای برای تبدیل در ستون A وجود ندارد.", "اطلاع", MessageBoxButtons.OK, MessageBoxIcon.Information)
Return
End If
' خواندن کل دادههای ستون A از ردیف 2 تا آخر در آرایه
Dim range As Range = worksheet.Range("A2", "A" & lastRow)
Dim values As Object = range.Value2
' اگر فقط یک سلول باشد، آن را به آرایه تبدیل میکنیم
Dim arr As Object(,) = Nothing
If TypeOf values Is Object(,) Then
arr = CType(values, Object(,))
Else
arr = New Object(1, 1) {{values}}
End If
' تبدیل تاریخها
Dim persianCalendar As New PersianCalendar()
For i As Integer = 1 To arr.GetLength(0)
Dim cellValue As Object = arr(i, 1)
If cellValue IsNot Nothing Then
Dim dateStr As String = cellValue.ToString()
If dateStr.Length >= 10 Then
Dim datePart As String = dateStr.Substring(0, 10) ' 10 کاراکتر اول
' تبدیل رشته به DateTime
Dim dt As DateTime
If DateTime.TryParse(datePart, dt) Then
' تبدیل به تاریخ شمسی
Dim shamsiDate As String = String.Format("{0}/{1:D2}/{2:D2}",
persianCalendar.GetYear(dt),
persianCalendar.GetMonth(dt),
persianCalendar.GetDayOfMonth(dt))
arr(i, 1) = shamsiDate
End If
End If
End If
Next
' نوشتن آرایه به اکسل
range.Value2 = arr
' ذخیره و بستن فایل
workbook.Save()
workbook.Close()
excelApp.Quit()
Finally
' آزادسازی منابع COM
If worksheet IsNot Nothing Then Marshal.ReleaseComObject(worksheet)
If workbook IsNot Nothing Then Marshal.ReleaseComObject(workbook)
If excelApp IsNot Nothing Then Marshal.ReleaseComObject(excelApp)
worksheet = Nothing
workbook = Nothing
excelApp = Nothing
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
End Sub
End Class