Private Function ImportData() As Boolean
Try
Dim dsImport As New DataSet
If Not ImportByExcel(dsImport) Then
Return False
End If
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
If Not ImportCheck(dsImport) Then
Return False
End If
If Not ImportedCheck(dsImport) Then
Return False
End If
OpenTransaction()
mlogic.InsertSearchData(dsImport)
CommitTransaction()
MsgBox("导入成功!", MsgBoxStyle.Information, Me.Text)
Return True
Catch ex As Exception
RollbackTransaction()
MsgBox(ex.Message, MsgBoxStyle.Critical, Me.Text)
Finally
CloseConnection()
Me.Cursor = System.Windows.Forms.Cursors.Default
End Try
End Function
Private Function ImportByExcel(ByRef dsImport As DataSet) As Boolean
Dim openFileDialog As New System.Windows.Forms.OpenFileDialog
openFileDialog.Filter = "Excel 文件(*.xlsx;*.xls;*.xlsm)|*.xlsx;*.xls;*.xlsm"
openFileDialog.FilterIndex = 1
openFileDialog.RestoreDirectory = True
If (openFileDialog.ShowDialog() = Windows.Forms.DialogResult.OK) Then
If CheckExcelIsOpen(openFileDialog.FileName) Then
Return False
End If
dsImport = GetDataFromExcel(openFileDialog.FileName)
If Not checkDataColumnsCount(dsImport) Then
Return False
End If
Else
Return False
End If
Return True
End Function
Public Function CheckExcelIsOpen(ByVal sfilename As String) As Boolean
Dim fs As System.IO.FileStream = Nothing
Try
fs = New System.IO.FileStream(sfilename, IO.FileMode.Open, IO.FileAccess.ReadWrite, IO.FileShare.Write)
If Not fs.CanWrite Then
MsgBox("请先关闭导入的EXCEL文件!", MsgBoxStyle.Information, Me.Text)
Return True
End If
Return False
Catch ex As Exception
MsgBox(showMessage("请先关闭导入的EXCEL文件!"), MsgBoxStyle.Information, Me.Text)
Return True
Finally
If fs IsNot Nothing Then
fs.Close()
fs.Dispose()
End If
End Try
End Function
Private Function GetDataFromExcel(ByVal sfilename As String) As DataSet
Dim dsReturn As New DataSet
Dim ds As New DataSet
Dim xlApp As Excel.Application = Nothing
Dim xlBook As Excel.Workbook = Nothing
Dim oDbConn As System.Data.OleDb.OleDbConnection = Nothing
Dim sConn As String
Try
Me.Cursor = System.Windows.Forms.Cursors.WaitCursor
Dim fileNameList As String() = sfilename.Split(".")
If fileNameList(fileNameList.Length - 1).Equals("xlsx") OrElse fileNameList(fileNameList.Length - 1).Equals("xlsm") Then
sConn = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
"Data Source=" & sfilename & ";" & "Extended Properties='Excel 12.0;HDR=NO;IMEX=1'"
Else
sConn = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
"Data Source=" & sfilename & ";" & "Extended Properties='Excel 8.0;HDR=NO;IMEX=1'"
End If
oDbConn = New System.Data.OleDb.OleDbConnection(sConn)
oDbConn.Open()
Dim oDataAdapter As System.Data.OleDb.OleDbDataAdapter
xlApp = New Excel.Application
xlApp.Visible = False
xlBook = xlApp.Workbooks.Open(sfilename)
Dim sSheetName As String = ""
Dim sSqlSelect As String = ""
Dim count As Integer = 0
For i As Integer = 0 To xlBook.Sheets.Count - 1
sSheetName = xlBook.Sheets(count + i + 1).Name
sSqlSelect = "SELECT * FROM [" & sSheetName & "$]"
oDataAdapter = New System.Data.OleDb.OleDbDataAdapter(sSqlSelect, oDbConn)
oDataAdapter.Fill(ds)
ds.Tables(i).TableName = sSheetName
dsReturn.Tables.Add(ds.Tables(i).Copy())
Next
GetDataFromExcel = dsReturn
Catch ex As Exception
Throw New Exception(ex.Message)
Finally
If oDbConn.State <> ConnectionState.Broken And _
oDbConn.State <> ConnectionState.Closed Then
oDbConn.Close()
End If
If xlBook IsNot Nothing Then
xlBook.Saved = True
xlBook.Close()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlBook)
End If
xlBook = Nothing
If xlApp IsNot Nothing Then
xlApp.Quit()
System.Runtime.InteropServices.Marshal.ReleaseComObject(xlApp)
End If
xlApp = Nothing
GC.Collect()
Me.Cursor = System.Windows.Forms.Cursors.Default
End Try
End Function
Private Function checkDataColumnsCount(ByVal ds As DataSet) As Boolean
Dim strTableName As String = ""
If ds.Tables.Count < 1 Then
MsgBox(showMessage("PLP2010_006"), MsgBoxStyle.Information, Me.Text)
Return False
End If
If ds.Tables(0).Columns.Count < 31 Then
MsgBox(showMessage("PLP2010_006"), MsgBoxStyle.Information, Me.Text)
Return False
End If
Return True
End Function
Private Function ImportCheck(ByRef dsImport As DataSet) As Boolean
ImportCheck = False
Dim strMsg As String = ""
Dim intRowsCount As Integer = dsImport.Tables(0).Rows.Count - 1
Dim cmdParms(intRowsCount) As DictionaryEntry
Dim strPO As String = ""
Dim strF_Code As String = ""
Dim strPO_Count As String = ""
dsImport.Tables(0).Columns(1).ColumnName = "PO"
dsImport.Tables(0).Columns(20).ColumnName = "F_CODE"
dsImport.Tables(0).Columns(28).ColumnName = "PO_COUNT"
For inti As Integer = 1 To dsImport.Tables(0).Rows.Count - 1
strPO = dsImport.Tables(0).Rows(inti)("PO").ToString().Trim
strF_Code = dsImport.Tables(0).Rows(inti)("F_CODE").ToString().Trim
strPO_Count = dsImport.Tables(0).Rows(inti)("PO_COUNT").ToString().Trim
If strPO.Equals("") Or strF_Code.Equals("") Then
Continue For
End If
Dim keyPO As String
Dim valFcode As String
For index As Integer = 0 To cmdParms.Length - 1
keyPO = Convert.ToString(cmdParms(index).Key)
valFcode = Convert.ToString(cmdParms(index).Value)
If strPO.Equals(keyPO) AndAlso strF_Code.Equals(valFcode) Then
strMsg &= String.Format("第{0}行:PO号+机种【{1}+{2}】重复存在!", inti + 1, strPO, strF_Code) & Environment.NewLine
Continue For
End If
Next
cmdParms(inti - 1) = New DictionaryEntry(strPO, strF_Code)
Next
If strMsg.Length > 0 Then
Using frm As New PLP010(strMsg)
frm.ShowDialog()
End Using
Exit Function
End If
ImportCheck = True
End Function
Private Function ImportedCheck(ByRef dsImport As DataSet) As Boolean
ImportedCheck = False
Dim strMsg As String = ""
Dim dt As DataTable
dt = mlogic.SearchAll_Logic().Tables(0)
Dim cmdParms(dt.Rows.Count - 1) As DictionaryEntry
For index As Integer = 0 To dt.Rows.Count - 1
cmdParms(index) = New DictionaryEntry(dt.Rows(index).Item(0).ToString(), dt.Rows(index).Item(1).ToString())
Next
Dim strPO As String = ""
Dim strF_Code As String = ""
Dim strPO_Count As String = ""
dsImport.Tables(0).Columns(1).ColumnName = "PO"
dsImport.Tables(0).Columns(20).ColumnName = "F_CODE"
dsImport.Tables(0).Columns(28).ColumnName = "PO_COUNT"
For inti As Integer = 1 To dsImport.Tables(0).Rows.Count - 1
strPO = dsImport.Tables(0).Rows(inti)(0).ToString()
strF_Code = dsImport.Tables(0).Rows(inti)(21).ToString()
strPO_Count = dsImport.Tables(0).Rows(inti)(29).ToString()
Dim keyPO As String
Dim valFcode As String
For index As Integer = 0 To cmdParms.Length - 1
keyPO = cmdParms(index).Key.ToString()
valFcode = cmdParms(index).Value.ToString()
If strPO.Equals(keyPO) AndAlso strF_Code.Equals(valFcode) Then
strMsg &= String.Format("第{0}行:PO号+机种【{1}+{2}】已导入!", inti + 1, strPO, strF_Code) & Environment.NewLine
Continue For
End If
Next
Next
If strMsg.Length > 0 Then
Using frm As New PLP010(strMsg)
frm.ShowDialog()
End Using
Exit Function
End If
ImportedCheck = True
End Function