รบกวนช่วยดูโค้ด บนVB ให้หน่อยคะ พยายามแก้แล้ว
จะเขียนฟังก์ชั่นสำหรับ import excel เป็น text โดยให้มีเครื่องหมาย ," ตาม Default เดิมของ text
แต่ ด้านขวาสุดของไฟล์ มันมีเครื่องหมาย ,"," เกินมาเยอะเลยคะ
** แก้ไขรูป
Public Sub ImportfromExcel(ByVal excelFilePath As String, ByVal sysfilePath As String)
Dim strFileName As String = sysfilePath
If System.IO.File.Exists(strFileName) Then
If (MessageBox.Show("Do you want to replace from the existing file?", "Import from Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = System.Windows.Forms.DialogResult.Yes) Then
System.IO.File.Delete(strFileName)
Else
Return
End If
End If
Dim _excel As Excel.Application = CreateObject("Excel.Application")
Dim wBook As Excel.Workbook
Dim wSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim oldCI As System.Globalization.CultureInfo = _
System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture = _
New System.Globalization.CultureInfo("en-US")
' Open Excel spreadsheet.
wBook = _excel.Workbooks.Open(excelFilePath)
' Get sheet.
wSheet = wBook.Sheets("sheet1")
' Get range.
Dim r As Range = wSheet.UsedRange
' Load all cells into 2d array.
Dim array(,) As Object = r.Value(XlRangeValueDataType.xlRangeValueDefault)
Dim str As String
Dim line As String
' Scan the cells.
If Not (array Is Nothing) Then
Console.WriteLine("Length: {0}", array.Length)
' Get bounds of the array.
Dim bound0 As Integer = array.GetUpperBound(0)
Dim bound1 As Integer = array.GetUpperBound(1)
Console.WriteLine("Dimension 0: {0}", bound0)
Console.WriteLine("Dimension 1: {0}", bound1)
str = ""
' Loop over all elements.
For j As Integer = 1 To bound0
'After Write Header
'Skip 2 line
If (j <= 3 Or j > 3 + 2) Then
line = ""
For x As Integer = 1 To bound1
Dim s1 As String = array(j, x)
If (j >= 1 And j <= 3) Then
line = line & """" & s1 & ""","
ElseIf (j > 3) Then
If (x = 1) Then
line = line & s1 & ","
Else
line = line & """" & s1 & ""","
End If
End If
Next
Console.Write(line)
If (str.Length <= 0) Then
str = line.Remove(line.Length - 1, 1)
Else
str = str & vbCrLf & line.Remove(line.Length - 1, 1)
End If
End If
Next
End If
'release the objects
ReleaseObject(wSheet)
wBook.Close(False)
ReleaseObject(wBook)
_excel.Quit()
ReleaseObject(_excel)
' some time Office application does not quit after automation: so i am calling GC.Collect method.
GC.Collect()
ToolbarSave(sysfilePath, str)
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI
MessageBox.Show("File Import Successfully!")
End Sub
สอบถามพี่ๆ ที่เขียนโค้ดบน VB รบกวนช่วยด้วยคะ
จะเขียนฟังก์ชั่นสำหรับ import excel เป็น text โดยให้มีเครื่องหมาย ," ตาม Default เดิมของ text
แต่ ด้านขวาสุดของไฟล์ มันมีเครื่องหมาย ,"," เกินมาเยอะเลยคะ
** แก้ไขรูป
Public Sub ImportfromExcel(ByVal excelFilePath As String, ByVal sysfilePath As String)
Dim strFileName As String = sysfilePath
If System.IO.File.Exists(strFileName) Then
If (MessageBox.Show("Do you want to replace from the existing file?", "Import from Excel", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = System.Windows.Forms.DialogResult.Yes) Then
System.IO.File.Delete(strFileName)
Else
Return
End If
End If
Dim _excel As Excel.Application = CreateObject("Excel.Application")
Dim wBook As Excel.Workbook
Dim wSheet As Excel.Worksheet
Dim misValue As Object = System.Reflection.Missing.Value
Dim oldCI As System.Globalization.CultureInfo = _
System.Threading.Thread.CurrentThread.CurrentCulture
System.Threading.Thread.CurrentThread.CurrentCulture = _
New System.Globalization.CultureInfo("en-US")
' Open Excel spreadsheet.
wBook = _excel.Workbooks.Open(excelFilePath)
' Get sheet.
wSheet = wBook.Sheets("sheet1")
' Get range.
Dim r As Range = wSheet.UsedRange
' Load all cells into 2d array.
Dim array(,) As Object = r.Value(XlRangeValueDataType.xlRangeValueDefault)
Dim str As String
Dim line As String
' Scan the cells.
If Not (array Is Nothing) Then
Console.WriteLine("Length: {0}", array.Length)
' Get bounds of the array.
Dim bound0 As Integer = array.GetUpperBound(0)
Dim bound1 As Integer = array.GetUpperBound(1)
Console.WriteLine("Dimension 0: {0}", bound0)
Console.WriteLine("Dimension 1: {0}", bound1)
str = ""
' Loop over all elements.
For j As Integer = 1 To bound0
'After Write Header
'Skip 2 line
If (j <= 3 Or j > 3 + 2) Then
line = ""
For x As Integer = 1 To bound1
Dim s1 As String = array(j, x)
If (j >= 1 And j <= 3) Then
line = line & """" & s1 & ""","
ElseIf (j > 3) Then
If (x = 1) Then
line = line & s1 & ","
Else
line = line & """" & s1 & ""","
End If
End If
Next
Console.Write(line)
If (str.Length <= 0) Then
str = line.Remove(line.Length - 1, 1)
Else
str = str & vbCrLf & line.Remove(line.Length - 1, 1)
End If
End If
Next
End If
'release the objects
ReleaseObject(wSheet)
wBook.Close(False)
ReleaseObject(wBook)
_excel.Quit()
ReleaseObject(_excel)
' some time Office application does not quit after automation: so i am calling GC.Collect method.
GC.Collect()
ToolbarSave(sysfilePath, str)
System.Threading.Thread.CurrentThread.CurrentCulture = oldCI
MessageBox.Show("File Import Successfully!")
End Sub