Advertisement
LittleBoy0311

Visure v2

Apr 5th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 57.21 KB | None | 0 0
  1. Imports iTextSharp.text
  2. Imports iTextSharp.text.api
  3. Imports iTextSharp.text.pdf
  4. Imports iTextSharp.text.pdf.parser
  5. Imports System.Text
  6. Imports System.IO
  7. Imports System
  8. Imports System.Data
  9. Imports System.Data.OleDb
  10. Imports System.Text.RegularExpressions
  11. Imports System.Data.SqlClient
  12. Imports Microsoft.VisualBasic.CompilerServices
  13. Imports System.Configuration
  14.  
  15.  
  16.  
  17. Public Class Visure
  18.  
  19.     Public Shared tabella As New Data.DataTable
  20.     Public Shared CnSql As SqlConnection
  21.     Public Shared CnSOGEUS As SqlConnection
  22.     Public Shared PATH_SERVER_PDF As String
  23.     Public Shared PATH_SERVER_REPORT As String
  24.     Public Shared PATH_SERVER_SPEDIZIONI As String
  25.     Public Shared PATH_SERVER_SOGEUS As String
  26.  
  27.     Public Shared IP_PATH_SERVER_PDF As String
  28.     Public Shared PATH_SERVER_PDF_OUTPUT As String
  29.     Public Shared IP_PATH_SERVER_PDF_OUTPUT As String
  30.     Dim DirPdf As String = ""
  31.     Dim fileExcel As String = ""
  32.     Dim nProprietari As Integer
  33.     Dim Tab As String
  34.     Dim TempTab As String
  35.     Dim connectionString As String = "Server=AUTOINFORMSRV;Database=SIMONE;User ID=utente421;Password=simone;Integrated Security=SSPI;"
  36.  
  37.     Private Sub Exec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exec.Click
  38.         'Try
  39.         'Controlli input
  40.         Dim result As DialogResult = Windows.Forms.DialogResult.Yes
  41.         Tab = "VISUREDAPDF_"
  42.         TempTab = "TempExcelTable_"
  43.         ProgressBar1.Step = 1
  44.         If fileExcel = "" Or DirPdf = "" Then
  45.             MsgBox("Selezionare un file excel ed una cartella pdf prima di continuare.")
  46.             Exit Sub
  47.         End If
  48.  
  49.         If Path.GetFileNameWithoutExtension(fileExcel).Contains(Tab) Then
  50.             result = MessageBox.Show("Un file excel già elaborato è stato selezionato, sei sicuro di voler continuare?", "Attenzione!", MessageBoxButtons.YesNo)
  51.         End If
  52.  
  53.         If result = DialogResult.Yes Then
  54.             ProgressBar1.Value = 0
  55.             Dim sDb As String
  56.             If chkPgt.Checked Then
  57.                 sDb = "PEUGEOT"
  58.                 Tab = Tab.Replace("_", "_PAI")
  59.                 TempTab = TempTab.Replace("_", "_PAI")
  60.  
  61.             Else
  62.                 sDb = "CITROEN"
  63.                 Tab = Tab.Replace("_", "_CAI")
  64.                 TempTab = TempTab.Replace("_", "_CAI")
  65.             End If
  66.             CreateTable(TempTab)
  67.             TableCleaner("Anagrafica")
  68.             TableDeleter("Anagrafica")
  69.             TableCleaner(Tab)
  70.             TableDeleter(Tab)
  71.  
  72.             CnSqlOpen(sDb, connectionString)
  73.             ProgressBar1.Visible = True
  74.             LeggiRinoninaPdfByTarga(DirPdf, 1, "aa", 1, Tab)
  75.         Else
  76.             Exit Sub
  77.         End If
  78.         'Catch ex As Exception
  79.         'MsgBox("Un errore è stato rilvevato in Exec_Click. Errore:" & vbLf & ex.Message)
  80.         'Err.Clear()
  81.         'End Try
  82.     End Sub
  83.  
  84.     Public Function LeggiRinoninaPdfByTarga(ByVal sFolderPdfs As String, ByVal idTipoArchiviazionePDF As Integer, ByVal sDesTipo As String, ByVal lTestata As Long, ByVal Tab As String) As Boolean
  85.         Dim dir As DirectoryInfo
  86.         Dim file As FileInfo
  87.         Dim s As String
  88.         Dim sFileInput As String
  89.         Dim sFileTxt As String
  90.         Dim TableToExcel As New Data.DataTable
  91.         Dim counter As Integer = 0
  92.         Dim check As Boolean = True
  93.  
  94.         LeggiRinoninaPdfByTarga = True
  95.  
  96.         'Try
  97.  
  98.         dir = New DirectoryInfo(sFolderPdfs)
  99.  
  100.         For Each file In dir.GetFiles("*.PDF", SearchOption.TopDirectoryOnly)
  101.  
  102.             sFileInput = file.Directory.ToString + "\" + file.Name.ToString
  103.             sFileTxt = file.Directory.ToString + "\" + file.Name.ToString.Replace("PDF", "txt")
  104.  
  105.             s = ParsePdfText(sFileInput)
  106.  
  107.             My.Computer.FileSystem.WriteAllText(sFileTxt, s, False)
  108.  
  109.             LeggiTargaEDateDaPdf(sFileTxt, sFileInput, idTipoArchiviazionePDF, sDesTipo, lTestata, Tab, check)
  110.  
  111.             counter += 1
  112.             check = False
  113.             If counter Mod 20 = 0 Then
  114.                 ProgressBar1.PerformStep()
  115.             End If
  116.         Next file
  117.         ExcelManagment(fileExcel)
  118.         Return LeggiRinoninaPdfByTarga
  119.         'Catch ex As Exception
  120.         'LeggiRinoninaPdfByTarga = False
  121.         'Return LeggiRinoninaPdfByTarga
  122.         'End Try
  123.  
  124.     End Function
  125.  
  126.     Public Shared Function ParsePdfText(ByVal sourcePDF As String, _
  127.                                    Optional ByVal fromPageNum As Integer = 0, _
  128.                                    Optional ByVal toPageNum As Integer = 0) As String
  129.         Dim sb As New System.Text.StringBuilder
  130.         Try
  131.             Dim reader As New iTextSharp.text.pdf.PdfReader(sourcePDF)
  132.             Dim pageBytes() As Byte = Nothing
  133.             Dim token As iTextSharp.text.pdf.PRTokeniser = Nothing
  134.             Dim tknType As Integer = -1
  135.             Dim tknValue As String = String.Empty
  136.  
  137.             If fromPageNum = 0 Then
  138.                 fromPageNum = 1
  139.             End If
  140.             If toPageNum = 0 Then
  141.                 toPageNum = reader.NumberOfPages
  142.             End If
  143.  
  144.             If fromPageNum > toPageNum Then
  145.                 Throw New ApplicationException("Errore parametri")
  146.             End If
  147.  
  148.             For i As Integer = fromPageNum To toPageNum Step 1
  149.                 pageBytes = reader.GetPageContent(i)
  150.                 If Not IsNothing(pageBytes) Then
  151.  
  152.                     token = New iTextSharp.text.pdf.PRTokeniser(pageBytes)
  153.                     While token.NextToken()
  154.                         tknType = token.TokenType()
  155.                         tknValue = token.StringValue
  156.                         Select Case tknType
  157.  
  158.                             Case 1  ' iTextSharp.text.pdf.PRTokeniser.TK_NUMBER
  159.                                 Dim dValue As Double
  160.                                 Dim r As Double
  161.                                 If Double.TryParse(tknValue, dValue, Nothing, r) Then
  162.                                     If dValue < -8000 Then
  163.                                         sb.Append(r)
  164.                                     End If
  165.                                 End If
  166.  
  167.                             Case 2  ' iTextSharp.text.pdf.PRTokeniser.TK_STRING
  168.                                 sb.Append(token.StringValue)
  169.                             Case 3  ' iTextSharp.text.pdf.PRTokeniser.TK_NAME
  170.                                 '
  171.                             Case 4  ' iTextSharp.text.pdf.PRTokeniser.TK_COMMENT    
  172.                                 '
  173.                             Case 5  ' iTextSharp.text.pdf.PRTokeniser.TK_START_ARRAY
  174.                                 '
  175.                             Case 6  ' iTextSharp.text.pdf.PRTokeniser.TK_END_ARRAY
  176.                                 sb.Append(Environment.NewLine)
  177.                                 'sb.Append(" ")
  178.                             Case 7  ' iTextSharp.text.pdf.PRTokeniser.TK_START_DIC
  179.                                 '
  180.                             Case 8  ' iTextSharp.text.pdf.PRTokeniser.TK_END_DIC
  181.                                 '
  182.                             Case 9  ' iTextSharp.text.pdf.PRTokeniser.TK_REF
  183.                                 '
  184.  
  185.                             Case 10 ' iTextSharp.text.pdf.PRTokeniser.TK_OTHER
  186.                                 Select Case tknValue
  187.                                     Case "TJ"
  188.                                         sb.Append(" ")
  189.                                     Case "ET"      ', "TD", "Td" , "Tm" , "T*"
  190.                                         sb.Append(Environment.NewLine)
  191.                                 End Select
  192.  
  193.                         End Select
  194.  
  195.  
  196.                     End While
  197.                 End If
  198.             Next i
  199.             reader.Close()
  200.         Catch ex As Exception
  201.             MsgBox("Un errore è stato rilvevato in ParePdfText. Errore:" & vbLf & ex.Message)
  202.             Err.Clear()
  203.             Return String.Empty
  204.         End Try
  205.  
  206.         Return sb.ToString()
  207.  
  208.     End Function
  209.  
  210.     Public Sub LeggiTargaEDateDaPdf(ByVal sfileTxtInput As String, ByVal sfilePdfInput As String, ByVal TipoArchiviazionePDF As Integer, ByVal DesTipo As String, ByVal lTestata As Long, ByVal Tab As String, ByVal check As Boolean)
  211.         Dim FilePdf As FileInfo
  212.         Dim sRiga As String
  213.         Dim sTarga As String
  214.         Dim sId As String
  215.         Dim FileTxt As FileInfo
  216.         FileTxt = New System.IO.FileInfo(sfileTxtInput)
  217.         Dim reader As StreamReader
  218.         Dim readerDate As StreamReader
  219.         sId = FileTxt.Name.Substring(0, FileTxt.Name.LastIndexOf(".").ToString)
  220.         FilePdf = New System.IO.FileInfo(sfilePdfInput)
  221.         Dim text As New StringBuilder()
  222.         reader = My.Computer.FileSystem.OpenTextFileReader(sfileTxtInput)
  223.         readerDate = My.Computer.FileSystem.OpenTextFileReader(sfileTxtInput)
  224.         Try
  225.  
  226.             Dim sqlCmd As String = "CREATE Table " & Tab & " (Targa NVarChar (7) NOT NULL, "
  227.             Dim sqlIns As String = "INSERT INTO " & Tab & " VALUES(@Targa, "
  228.             Dim MyCmdIns As New SqlCommand(sqlIns, CnSql)
  229.             Dim DateTrovate As MatchCollection = Regex.Matches(text.ToString, "[0-9]*/[0-9]*/[0-9]{4}")
  230.             sRiga = reader.ReadLine.TrimStart
  231.             sTarga = Mid(sRiga, 22, 7)
  232.             MyCmdIns.Parameters.Add("@Targa", SqlDbType.NVarChar)
  233.             MyCmdIns.Parameters("@Targa").Value = sTarga
  234.  
  235.             'Lettura campi scelti e setup sqlCmd
  236.  
  237.             Dim telaioInizio As Integer
  238.             Dim telaioFine As Integer
  239.             Dim telaio As String
  240.  
  241.             telaioInizio = sRiga.IndexOf("Telaio") + 6
  242.             telaioFine = sRiga.IndexOf("Fabbrica / Tipo")
  243.             telaio = sRiga.Substring(telaioInizio, telaioFine - telaioInizio)
  244.  
  245.             sqlCmd = sqlCmd & "Telaio NVarChar (17) NULL, "
  246.             sqlIns = sqlIns & "@Telaio, "
  247.             MyCmdIns.Parameters.Add("@Telaio", SqlDbType.NVarChar)
  248.             MyCmdIns.Parameters("@Telaio").Value = telaio
  249.  
  250.  
  251.  
  252.  
  253.             Dim produttoreInizio As Integer
  254.             Dim produttoreFine As Integer
  255.             Dim produttore As String
  256.  
  257.             produttoreInizio = sRiga.IndexOf("Fabbrica / Tipo") + 15
  258.             produttoreFine = sRiga.IndexOf("Data Immatricolazione")
  259.             produttore = sRiga.Substring(produttoreInizio, produttoreFine - produttoreInizio)
  260.  
  261.             sqlCmd = sqlCmd & "Produttore NVarChar (100) NULL, "
  262.             sqlIns = sqlIns & "@Produttore, "
  263.             MyCmdIns.Parameters.Add("@Produttore", SqlDbType.NVarChar)
  264.             MyCmdIns.Parameters("@Produttore").Value = produttore
  265.  
  266.  
  267.  
  268.             Dim immatricolazioneInizio As Integer
  269.             Dim immatricolazioneFine As Integer
  270.             Dim immatricolazione As String
  271.  
  272.             immatricolazioneInizio = sRiga.IndexOf("Data Aggiornamento Carta Circolazione") + Len("Data Aggiornamento Carta Circolazione")
  273.             immatricolazioneFine = sRiga.IndexOf("KW")
  274.             immatricolazione = sRiga.Substring(immatricolazioneInizio, immatricolazioneFine - immatricolazioneInizio)
  275.  
  276.             sqlCmd = sqlCmd & "DataImmatricolazione smalldatetime NULL, "
  277.             sqlIns = sqlIns & "@DataImmatricolazione, "
  278.             MyCmdIns.Parameters.Add("@DataImmatricolazione", SqlDbType.SmallDateTime)
  279.             MyCmdIns.Parameters("@DataImmatricolazione").Value = Convert.ToDateTime(immatricolazione.Replace("#", ""))
  280.  
  281.  
  282.             Dim cartaInizio As Integer
  283.             Dim cartaFine As Integer
  284.             Dim carta As String
  285.  
  286.             cartaInizio = sRiga.IndexOf("Data Aggiornamento Carta Circolazione") + Len("Data Aggiornamento Carta Circolazione")
  287.             cartaFine = sRiga.IndexOf("KW")
  288.             carta = sRiga.Substring(cartaInizio, cartaFine - cartaInizio)
  289.  
  290.             sqlCmd = sqlCmd & "DataCartaCircolazione smalldatetime NULL, "
  291.             sqlIns = sqlIns & "@DataCartaCircolazione, "
  292.             MyCmdIns.Parameters.Add("@DataCartaCircolazione", SqlDbType.SmallDateTime)
  293.             MyCmdIns.Parameters("@DataCartaCircolazione").Value = Convert.ToDateTime(carta)
  294.  
  295.  
  296.  
  297.             Dim kwInizio As Integer
  298.             Dim kwFine As Integer
  299.             Dim kw As String
  300.  
  301.             kwInizio = sRiga.IndexOf("KW") + 2
  302.             kwFine = sRiga.IndexOf("Classe / Uso")
  303.             kw = sRiga.Substring(kwInizio, kwFine - kwInizio)
  304.  
  305.             sqlCmd = sqlCmd & "KW decimal(10,2) NULL, "
  306.             sqlIns = sqlIns & "@KW, "
  307.             MyCmdIns.Parameters.Add("@KW", SqlDbType.Decimal)
  308.             MyCmdIns.Parameters("@KW").Value = Convert.ToDecimal(kw.Replace(",", "."))
  309.  
  310.  
  311.  
  312.             Dim classeInizio As Integer
  313.             Dim classeFine As Integer
  314.             Dim classe As String
  315.  
  316.             classeInizio = sRiga.IndexOf("Classe / Uso") + 12
  317.             classeFine = sRiga.IndexOf("Carrozzeria")
  318.             classe = sRiga.Substring(classeInizio, classeFine - classeInizio)
  319.  
  320.             sqlCmd = sqlCmd & "Classe NVarChar(100) NULL, "
  321.             sqlIns = sqlIns & "@Classe, "
  322.             MyCmdIns.Parameters.Add("@Classe", SqlDbType.NVarChar)
  323.             MyCmdIns.Parameters("@Classe").Value = classe
  324.  
  325.  
  326.  
  327.             Dim cilindrataInizio As Integer
  328.             Dim cilindrataFine As Integer
  329.             Dim cilindrata As String
  330.  
  331.             cilindrataInizio = sRiga.IndexOf("Cilindrata") + 10
  332.             cilindrataFine = sRiga.IndexOf("Alimentazione")
  333.             cilindrata = sRiga.Substring(cilindrataInizio, cilindrataFine - cilindrataInizio)
  334.  
  335.             sqlCmd = sqlCmd & "Cilindrata nvarchar(4) NULL, "
  336.             sqlIns = sqlIns & "@Cilindrata, "
  337.             MyCmdIns.Parameters.Add("@Cilindrata", SqlDbType.NVarChar)
  338.             MyCmdIns.Parameters("@Cilindrata").Value = cilindrata
  339.  
  340.  
  341.  
  342.             Dim alimentazioneInizio As Integer
  343.             Dim alimentazioneFine As Integer
  344.             Dim alimentazione As String
  345.             alimentazioneInizio = sRiga.IndexOf("Alimentazione") + 13
  346.             alimentazioneFine = sRiga.IndexOf("Tara")
  347.             alimentazione = sRiga.Substring(alimentazioneInizio, alimentazioneFine - alimentazioneInizio)
  348.  
  349.             sqlCmd = sqlCmd & "Alimentazione NVarChar(100) NULL, "
  350.             sqlIns = sqlIns & "@Alimentazione, "
  351.             MyCmdIns.Parameters.Add("@Alimentazione", SqlDbType.NVarChar)
  352.             MyCmdIns.Parameters("@Alimentazione").Value = alimentazione
  353.  
  354.  
  355.  
  356.             Dim taraInizio As Integer
  357.             Dim taraFine As Integer
  358.             Dim tara As String
  359.  
  360.             taraInizio = sRiga.IndexOf("Tara") + 4
  361.             taraFine = sRiga.IndexOf("Portata")
  362.             tara = sRiga.Substring(taraInizio, taraFine - taraInizio)
  363.  
  364.             sqlCmd = sqlCmd & "Tara NVarChar(10) NULL, "
  365.             sqlIns = sqlIns & "@Tara, "
  366.             MyCmdIns.Parameters.Add("@Tara", SqlDbType.NVarChar)
  367.             MyCmdIns.Parameters("@Tara").Value = tara
  368.  
  369.  
  370.  
  371.             Dim postiInizio As Integer
  372.             Dim postiFine As Integer
  373.             Dim posti As String
  374.  
  375.             postiInizio = sRiga.IndexOf("Posti n.ro") + 10
  376.             postiFine = sRiga.IndexOf("Assi n.ro")
  377.             posti = sRiga.Substring(postiInizio, postiFine - postiInizio)
  378.  
  379.             sqlCmd = sqlCmd & "Posti NVarChar(10) NULL, "
  380.             sqlIns = sqlIns & "@Posti, "
  381.             MyCmdIns.Parameters.Add("@Posti", SqlDbType.NVarChar)
  382.             MyCmdIns.Parameters("@Posti").Value = posti
  383.  
  384.  
  385.  
  386.             Dim assiInizio As Integer
  387.             Dim assiFine As Integer
  388.             Dim assi As String
  389.  
  390.             assiInizio = sRiga.IndexOf("Assi n.ro") + 9
  391.             assiFine = sRiga.IndexOf("Prima Formalita'")
  392.             assi = sRiga.Substring(assiInizio, assiFine - assiInizio)
  393.  
  394.             sqlCmd = sqlCmd & "Assi NVarChar(10) NULL, "
  395.             sqlIns = sqlIns & "@Assi, "
  396.             MyCmdIns.Parameters.Add("@Assi", SqlDbType.NVarChar)
  397.             MyCmdIns.Parameters("@Assi").Value = assi
  398.  
  399.  
  400.  
  401.             Dim primaFormalitaInizio As Integer
  402.             Dim primaFormalitaFine As Integer
  403.             Dim primaFormalita As String
  404.  
  405.             primaFormalitaInizio = sRiga.IndexOf("Prima Formalita'") + 16
  406.             primaFormalitaFine = sRiga.IndexOf("Data Prima Formalita'")
  407.             primaFormalita = sRiga.Substring(primaFormalitaInizio, primaFormalitaFine - primaFormalitaInizio)
  408.  
  409.             sqlCmd = sqlCmd & "PrimaFormalita nvarchar(100) NULL, "
  410.             sqlIns = sqlIns & "@PrimaFomalita, "
  411.             MyCmdIns.Parameters.Add("@PrimaFomalita", SqlDbType.NVarChar)
  412.             MyCmdIns.Parameters("@PrimaFomalita").Value = primaFormalita
  413.  
  414.  
  415.  
  416.             Dim dataPrimaFormalitaInizio As Integer
  417.             Dim dataPrimaFormalitaFine As Integer
  418.             Dim dataPrimaFormalita As String
  419.  
  420.             dataPrimaFormalitaInizio = sRiga.IndexOf("Data Prima Formalita'") + 21
  421.             dataPrimaFormalitaFine = dataPrimaFormalitaInizio + 10
  422.             dataPrimaFormalita = sRiga.Substring(dataPrimaFormalitaInizio, dataPrimaFormalitaFine - dataPrimaFormalitaInizio)
  423.  
  424.             sqlCmd = sqlCmd & "DataPrimaFormalita smalldatetime NULL, "
  425.             sqlIns = sqlIns & "@DataPrimaFormalita, "
  426.             MyCmdIns.Parameters.Add("@DataPrimaFormalita", SqlDbType.SmallDateTime)
  427.             MyCmdIns.Parameters("@DataPrimaFormalita").Value = Convert.ToDateTime(dataPrimaFormalita)
  428.  
  429.  
  430.  
  431.             Dim iultimaforminizio As Integer
  432.             Dim iultimaformfine As Integer
  433.             Dim sFormalita As String
  434.  
  435.             iultimaforminizio = sRiga.IndexOf("Ultima Formalita'") + 17
  436.             iultimaformfine = sRiga.IndexOf("Data Ultima Formalita'")
  437.             sFormalita = sRiga.Substring(iultimaforminizio, iultimaformfine - iultimaforminizio)
  438.  
  439.             sqlCmd = sqlCmd & "UltimaFormalita NVarChar(100) NULL, "
  440.             sqlIns = sqlIns & "@UltimaFormalita, "
  441.             MyCmdIns.Parameters.Add("@UltimaFormalita", SqlDbType.NVarChar)
  442.             MyCmdIns.Parameters("@UltimaFormalita").Value = sFormalita
  443.  
  444.  
  445.  
  446.             Dim sDataUltForm As String
  447.             Dim sDataUltFormInizio As Integer
  448.             Dim sDataUltFormFine As Integer
  449.  
  450.             sDataUltFormInizio = sRiga.IndexOf("Data Ultima Formalita'") + 22
  451.             sDataUltFormFine = sDataUltFormInizio + 10
  452.             sDataUltForm = sRiga.Substring(sDataUltFormInizio, sDataUltFormFine - sDataUltFormInizio)
  453.  
  454.             sqlCmd = sqlCmd & "DataUltimaFormalita smalldatetime NULL, "
  455.             sqlIns = sqlIns & "@DataUltimaFormalita, "
  456.             MyCmdIns.Parameters.Add("@DataUltimaFormalita", SqlDbType.SmallDateTime)
  457.             MyCmdIns.Parameters("@DataUltimaFormalita").Value = Convert.ToDateTime(sDataUltForm)
  458.  
  459.  
  460.  
  461.             Dim attoInizio As Integer
  462.             Dim attoFine As Integer
  463.             Dim atto As String
  464.  
  465.             attoInizio = sRiga.IndexOf("Atto") + 4
  466.             attoFine = sRiga.IndexOf("Data Atto")
  467.             atto = sRiga.Substring(attoInizio, attoFine - attoInizio)
  468.  
  469.             sqlCmd = sqlCmd & "Atto NVarChar(100) NULL, "
  470.             sqlIns = sqlIns & "@Atto, "
  471.             MyCmdIns.Parameters.Add("@Atto", SqlDbType.NVarChar)
  472.             MyCmdIns.Parameters("@Atto").Value = atto
  473.  
  474.  
  475.  
  476.             Dim dataAttoInizio As Integer
  477.             Dim dataAttoFine As Integer
  478.             Dim dataAtto As String
  479.  
  480.             dataAttoInizio = sRiga.IndexOf("Data Atto") + 9
  481.             dataAttoFine = sRiga.IndexOf("Prezzo del Veicolo")
  482.             dataAtto = sRiga.Substring(dataAttoInizio, dataAttoFine - dataAttoInizio)
  483.  
  484.             sqlCmd = sqlCmd & "DataAtto smalldatetime NULL, "
  485.             sqlIns = sqlIns & "@DataAtto, "
  486.             MyCmdIns.Parameters.Add("@DataAtto", SqlDbType.SmallDateTime)
  487.             MyCmdIns.Parameters("@DataAtto").Value = Convert.ToDateTime(dataAtto)
  488.  
  489.  
  490.  
  491.             Dim prezzoVeicoloInizio As Integer
  492.             Dim prezzoVeicoloFine As Integer
  493.             Dim prezzoVeicolo As String
  494.  
  495.             prezzoVeicoloInizio = sRiga.IndexOf("Prezzo del Veicolo") + 18
  496.             prezzoVeicoloFine = sRiga.IndexOf("Proprietario")
  497.             prezzoVeicolo = sRiga.Substring(prezzoVeicoloInizio, prezzoVeicoloFine - prezzoVeicoloInizio).Replace("*", "").Replace(".", "").Replace(",", ".").Replace(" ", "").ToUpper.Replace("EURO", "")
  498.  
  499.             sqlCmd = sqlCmd & "PrezzoVeicolo decimal(10,2) NULL, "
  500.             sqlIns = sqlIns & "@PrezzoVeicolo, "
  501.             MyCmdIns.Parameters.Add("@PrezzoVeicolo", SqlDbType.Decimal)
  502.             MyCmdIns.Parameters("@PrezzoVeicolo").Value = Convert.ToDecimal(prezzoVeicolo.Substring(0, prezzoVeicolo.Length - 3))
  503.  
  504.             'Chiusura Select / Insert
  505.             sqlIns = sqlIns.Substring(0, sqlIns.Length - 2)
  506.             sqlIns = sqlIns & ")"
  507.             sqlCmd = sqlCmd & "PRIMARY KEY(Targa))"
  508.  
  509.             If check Then
  510.                 Using myCmd As New SqlCommand(sqlCmd, CnSql)
  511.                     myCmd.CommandTimeout = 0
  512.                     myCmd.CommandType = CommandType.Text
  513.                     myCmd.ExecuteNonQuery()
  514.                 End Using
  515.             Else
  516.                 sqlCmd = ""
  517.             End If
  518.             MyCmdIns.CommandText = sqlIns
  519.             MyCmdIns.CommandTimeout = 0
  520.             MyCmdIns.CommandType = CommandType.Text
  521.             MyCmdIns.ExecuteNonQuery()
  522.  
  523.  
  524.  
  525.  
  526.             'Gestione blocco propretario
  527.             Dim counter As Integer = 0
  528.             MyCmdIns = New SqlCommand(sqlIns, CnSql)
  529.  
  530.             'Ridimensionamento sRiga al blocco proprietario
  531.             sRiga = sRiga.Substring(sRiga.IndexOf("Proprietario"), sRiga.IndexOf("ANNOTAZIONI") - sRiga.IndexOf("Proprietario"))
  532.  
  533.             'Conto n proprietari
  534.             For Each dato As Match In Regex.Matches(sRiga, "Proprietario")
  535.                 counter += 1
  536.             Next
  537.             nProprietari = counter
  538.  
  539.             'Dichiarazione array di indici blocco proprietario
  540.             Dim indexProprietario(counter) As Integer
  541.             Dim indexTipo(counter) As Integer
  542.             Dim indexDataNascita(counter) As Integer
  543.             Dim indexComuneNascita(counter) As Integer
  544.             Dim indexComuneResidenza(counter) As Integer
  545.             Dim indexIndirizzo(counter) As Integer
  546.  
  547.             'Dichiarazione array blocco proprietario
  548.             Dim proprietario(counter) As String
  549.             Dim tipo(counter) As String
  550.             Dim nascita(counter) As String
  551.             Dim comuneNascita(counter) As String
  552.             Dim residenza(counter) As String
  553.             Dim indirizzo(counter) As String
  554.             Dim arrCmd(counter) As String
  555.             Dim arrIns(counter) As String
  556.  
  557.             'Dichiarazione Parametri SQL
  558.             MyCmdIns.Parameters.Add("@Targa", SqlDbType.NVarChar)
  559.             MyCmdIns.Parameters.Add("@Proprietario", SqlDbType.NVarChar)
  560.             MyCmdIns.Parameters.Add("@Tipo", SqlDbType.NVarChar)
  561.             MyCmdIns.Parameters.Add("@DataNascita", SqlDbType.SmallDateTime)
  562.             MyCmdIns.Parameters.Add("@ComuneNascita", SqlDbType.NVarChar)
  563.             MyCmdIns.Parameters.Add("@ComuneResidenza", SqlDbType.NVarChar)
  564.             MyCmdIns.Parameters.Add("@Indirizzo", SqlDbType.NVarChar)
  565.  
  566.             'inizializzazione array di indici blocco proprietario
  567.             counter = 0
  568.  
  569.  
  570.             'Proprietario
  571.             For Each dato As Match In Regex.Matches(sRiga, "Proprietario")
  572.                 indexProprietario(counter) = dato.Index
  573.                 counter += 1
  574.             Next
  575.             counter = 0
  576.  
  577.             'Sesso / Tipo Società
  578.             For Each dato As Match In Regex.Matches(sRiga, "Sesso / Tipo Societa'")
  579.                 indexTipo(counter) = dato.Index
  580.                 counter += 1
  581.             Next
  582.             counter = 0
  583.  
  584.             'Data di nascita
  585.             For Each dato As Match In Regex.Matches(sRiga, "Data di nascita")
  586.                 indexDataNascita(counter) = dato.Index
  587.                 counter += 1
  588.             Next
  589.             counter = 0
  590.  
  591.             'Comune di nascita
  592.             For Each dato As Match In Regex.Matches(sRiga, "Comune di nascita")
  593.                 indexComuneNascita(counter) = dato.Index
  594.                 counter += 1
  595.             Next
  596.             counter = 0
  597.  
  598.             'Comune di residenza
  599.             For Each dato As Match In Regex.Matches(sRiga, "Comune di residenza")
  600.                 indexComuneResidenza(counter) = dato.Index
  601.                 counter += 1
  602.             Next
  603.             counter = 0
  604.  
  605.             'Indirizzo
  606.             For Each dato As Match In Regex.Matches(sRiga, "Indirizzo")
  607.                 indexIndirizzo(counter) = dato.Index
  608.                 counter += 1
  609.             Next
  610.             counter = 0
  611.  
  612.             'Estrazione blocco proprietario da PDF
  613.             For Each dato As Match In Regex.Matches(sRiga, "Proprietario")
  614.                 sqlCmd = "CREATE Table Anagrafica(Targa_FK NVarChar (7) FOREIGN KEY REFERENCES " & Tab & "(Targa), "
  615.                 sqlIns = "INSERT INTO Anagrafica VALUES(@Targa, "
  616.  
  617.                 'Targa_FK
  618.                 MyCmdIns.Parameters("@Targa").Value = sTarga
  619.  
  620.                 'Proprietario
  621.                 proprietario(counter) = sRiga.Substring(indexProprietario(counter) + Len("Proprietario"), indexTipo(counter) - (indexProprietario(counter) + Len("Proprietario")))
  622.                 arrCmd(counter) = "Proprietario nvarchar(100) NULL, "
  623.                 arrIns(counter) = "@Proprietario, "
  624.                 MyCmdIns.Parameters("@Proprietario").Value = proprietario(counter)
  625.                 sqlCmd = sqlCmd & arrCmd(counter)
  626.                 sqlIns = sqlIns & arrIns(counter)
  627.  
  628.  
  629.                 'Sesso / Tipo Società
  630.                 tipo(counter) = sRiga.Substring(indexTipo(counter) + Len("Sesso / Tipo Societa'"), indexDataNascita(counter) - (indexTipo(counter) + Len("Sesso / Tipo Societa'")))
  631.                 arrCmd(counter) = "Tipo nvarchar(100) NULL, "
  632.                 arrIns(counter) = "@Tipo, "
  633.                 MyCmdIns.Parameters("@Tipo").Value = tipo(counter)
  634.                 sqlCmd = sqlCmd & arrCmd(counter)
  635.                 sqlIns = sqlIns & arrIns(counter)
  636.  
  637.  
  638.                 'Data di Nascita
  639.                 nascita(counter) = sRiga.Substring(indexDataNascita(counter) + Len("Data di nascita"), indexComuneNascita(counter) - (indexDataNascita(counter) + Len("Data di nascita")))
  640.                 arrCmd(counter) = "DataNascita smalldatetime NULL, "
  641.                 arrIns(counter) = "@DataNascita, "
  642.                 MyCmdIns.Parameters("@DataNascita").Value = Convert.ToDateTime(nascita(counter))
  643.                 sqlCmd = sqlCmd & arrCmd(counter)
  644.                 sqlIns = sqlIns & arrIns(counter)
  645.  
  646.  
  647.                 'Comune di Nascita
  648.                 comuneNascita(counter) = sRiga.Substring(indexComuneNascita(counter) + Len("Comune di nascita"), indexComuneResidenza(counter) - (indexComuneNascita(counter) + Len("Comune di nascita")))
  649.                 arrCmd(counter) = "ComuneNascita nvarchar(100) NULL, "
  650.                 arrIns(counter) = "@ComuneNascita, "
  651.                 MyCmdIns.Parameters("@ComuneNascita").Value = comuneNascita(counter)
  652.                 sqlCmd = sqlCmd & arrCmd(counter)
  653.                 sqlIns = sqlIns & arrIns(counter)
  654.  
  655.  
  656.                 'Comune di Residenza
  657.                 residenza(counter) = sRiga.Substring(indexComuneResidenza(counter) + Len("Comune di residenza"), indexIndirizzo(counter) - (indexComuneResidenza(counter) + Len("Comune di residenza")))
  658.                 arrCmd(counter) = "ComuneResidenza nvarchar(100) NULL, "
  659.                 arrIns(counter) = "@ComuneResidenza, "
  660.                 MyCmdIns.Parameters("@ComuneResidenza").Value = residenza(counter)
  661.                 sqlCmd = sqlCmd & arrCmd(counter)
  662.                 sqlIns = sqlIns & arrIns(counter)
  663.  
  664.  
  665.                 'Indirizzo
  666.                 If counter = 0 And nProprietari <> 1 Then
  667.                     indirizzo(counter) = sRiga.Substring(indexIndirizzo(counter) + Len("Indirizzo"), indexProprietario(counter + 1) - (indexIndirizzo(counter) + Len("Indirizzo")))
  668.                 Else
  669.                     indirizzo(counter) = sRiga.Substring(indexIndirizzo(counter) + Len("Indirizzo"), sRiga.Length() - (indexIndirizzo(counter) + Len("Indirizzo")))
  670.                 End If
  671.                 arrCmd(counter) = "Indirizzo nvarchar(100) NULL, "
  672.                 arrIns(counter) = "@Indirizzo, "
  673.                 MyCmdIns.Parameters("@Indirizzo").Value = indirizzo(counter)
  674.                 sqlCmd = sqlCmd & arrCmd(counter)
  675.                 sqlIns = sqlIns & arrIns(counter)
  676.  
  677.                 'Chiusura Select / Insert
  678.                 sqlIns = sqlIns.Substring(0, sqlIns.Length - 2) & ")"
  679.                 sqlCmd = sqlCmd & "ID int IDENTITY(1,1) PRIMARY KEY)"
  680.                 If counter = 0 And check Then
  681.                     Using myCmd As New SqlCommand(sqlCmd, CnSql)
  682.                         myCmd.CommandTimeout = 0
  683.                         myCmd.CommandType = CommandType.Text
  684.                         myCmd.ExecuteNonQuery()
  685.                     End Using
  686.                 Else
  687.                     sqlCmd = ""
  688.                 End If
  689.                 MyCmdIns.CommandText = sqlIns
  690.                 MyCmdIns.CommandTimeout = 0
  691.                 MyCmdIns.CommandType = CommandType.Text
  692.                 MyCmdIns.ExecuteNonQuery()
  693.  
  694.                 counter += 1
  695.             Next
  696.  
  697.             reader.Close()
  698.             readerDate.Close()
  699.  
  700.             'Elimina il file txt analizzato
  701.             FileTxt.Delete()
  702.         Catch ex As Exception
  703.             reader.Close()
  704.             readerDate.Close()
  705.             FileTxt.Delete()
  706.             Err.Clear()
  707.         End Try
  708.  
  709.     End Sub
  710.  
  711.     Public Shared Function CnSqlOpen(ByVal db As String, ByVal connectionString As String) As Boolean
  712.         Try
  713.             Dim flag As Boolean
  714.             If (CnSql Is Nothing) Then
  715.                 CnSql = New SqlConnection(connectionString)
  716.                 CnSql.Open()
  717.                 Return True
  718.             End If
  719.             If (CnSql.State = ConnectionState.Open) Then
  720.                 Return True
  721.             End If
  722.             CnSql = New SqlConnection(connectionString)
  723.             CnSql.Open()
  724.             flag = True
  725.             Return flag
  726.         Catch ex As Exception
  727.             MsgBox("Un errore è stato rilevato in CnSqlOpen. Errore:" & vbLf & ex.Message)
  728.         End Try
  729.     End Function
  730.  
  731.     Public Shared Function fsql(ByVal argDato As Object, ByVal argTipo As String) As Object
  732.         Try
  733.             Select Case argTipo
  734.                 'Intero'
  735.                 Case "I"
  736.  
  737.                     If IsNull(argDato) Then
  738.  
  739.                         Return " null "
  740.                     Else
  741.                         If Len(argDato) = 0 Then
  742.                             Return " null "
  743.                         Else
  744.                             Return argDato
  745.                         End If
  746.                     End If
  747.                     'Data'
  748.                 Case "D"
  749.                     If IsNull(argDato) Then
  750.                         Return " null "
  751.                     Else
  752.                         If Len(argDato) = 0 Then
  753.                             Return " null "
  754.                         Else
  755.                             Return "'" & argDato & "'"
  756.                         End If
  757.                     End If
  758.                     'Testo'
  759.                 Case "T"
  760.                     If IsNull(argDato) Then
  761.                         Return " null "
  762.                     Else
  763.                         If Len(argDato) = 0 Then
  764.                             Return " null "
  765.                         Else
  766.                             If InStr(argDato, Chr(39)) = 0 Then
  767.                                 Return "'" & (argDato) & "'"
  768.                             Else
  769.                                 Return "'" & Replace(argDato, "'", "''") & "'"
  770.                             End If
  771.                         End If
  772.                     End If
  773.                     'float'
  774.                 Case "F"
  775.                     If IsNull(argDato) Then
  776.                         Return " null "
  777.                     Else
  778.                         If Len(argDato) = 0 Then
  779.                             Return " null "
  780.                         Else
  781.                             If argDato - Int(argDato) <> 0 Then
  782.                                 Return Replace(argDato, ",", ".")
  783.                             Else
  784.                                 Return argDato
  785.                             End If
  786.                         End If
  787.                     End If
  788.  
  789.                     'boolean'
  790.                 Case "B"
  791.                     If IsNull(argDato) Then
  792.                         Return " null "
  793.                     Else
  794.                         If argDato = "Vero" Then
  795.                             Return " 1 "
  796.                         Else
  797.                             Return " 0 "
  798.                         End If
  799.                     End If
  800.             End Select
  801.         Catch ex As Exception
  802.             MsgBox("Un errore è stato rilevato in fsql. Errore:" & vbLf & ex.Message)
  803.             Err.Clear()
  804.         End Try
  805.         Return "null"
  806.     End Function
  807.  
  808.     Public Shared Function IsNull(ByVal arg As Object) As Boolean
  809.         Try
  810.             If Len(arg) = 0 Then
  811.                 Return True
  812.             Else
  813.                 Return False
  814.             End If
  815.  
  816.  
  817.         Catch ex As Exception
  818.             Return True
  819.         End Try
  820.     End Function
  821.  
  822.     Private Sub chkCitroen_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkCit.CheckedChanged
  823.         chkPgt.Checked = Not chkCit.Checked
  824.     End Sub
  825.  
  826.     Private Sub chkPgt_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkPgt.CheckedChanged
  827.         chkCit.Checked = Not chkPgt.Checked
  828.     End Sub
  829.  
  830.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  831.         chkPgt.Checked = True
  832.         Targa.Checked = True
  833.         Telaio.Checked = True
  834.         UltimaFormalita.Checked = True
  835.         DataUltimaFormalita.Checked = True
  836.         PrezzoVeicolo.Checked = True
  837.         Proprietario.Checked = True
  838.         SessoTipoSocieta.Checked = True
  839.         DataNascita.Checked = True
  840.         TableCleaner("Anagrafica")
  841.         TableDeleter("Anagrafica")
  842.         TableCleaner(Tab)
  843.         TableDeleter(Tab)
  844.     End Sub
  845.  
  846.     Private Sub UP_Excel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UP_Excel.Click
  847.         Try
  848.  
  849.             Excel_Check.ForeColor = Color.Green
  850.             Excel_Check.Text = ""
  851.             OpenFileExcel.InitialDirectory = "C:\"
  852.             OpenFileExcel.Filter = "File Excel|*.xls; *.xlsx"
  853.  
  854.             If OpenFileExcel.ShowDialog = DialogResult.OK Then
  855.                 fileExcel = OpenFileExcel.FileName
  856.                 Excel_Check.Text = "File selezionato correttamente."
  857.                 Excel_TextBox.Text = fileExcel
  858.                 If Path.GetFileNameWithoutExtension(fileExcel).ToUpper.Contains("CIT") Or Path.GetFileNameWithoutExtension(fileExcel).ToUpper.Contains("CAI") Then
  859.                     chkCit.Checked = True
  860.                 Else
  861.                     chkPgt.Checked = True
  862.                 End If
  863.             Else
  864.                 Excel_Check.ForeColor = Color.Red
  865.                 Excel_Check.Text = "Impossibile selezionare il file."
  866.                 Exit Sub
  867.             End If
  868.         Catch ex As Exception
  869.             MsgBox("Un errore è stato rilevato in Excel_Click. Errore:" & vbLf & ex.Message)
  870.             Err.Clear()
  871.         End Try
  872.     End Sub
  873.  
  874.     Private Sub UP_Pdf_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UP_Pdf.Click
  875.         Try
  876.             Pdf_Check.ForeColor = Color.Green
  877.             Pdf_Check.Text = ""
  878.  
  879.             If OpenDirPdf.ShowDialog = DialogResult.OK Then
  880.                 DirPdf = OpenDirPdf.SelectedPath
  881.                 Pdf_Check.Text = "Cartella selezionata correttamente."
  882.                 PDF_TextBox.Text = DirPdf
  883.             Else
  884.                 Pdf_Check.ForeColor = Color.Red
  885.                 Pdf_Check.Text = "Impossibile selezionare la cartella."
  886.                 Exit Sub
  887.             End If
  888.         Catch ex As Exception
  889.             MsgBox("Un errore è stato rilevato in Pdf_Click. Errore:" & vbLf & ex.Message)
  890.             Err.Clear()
  891.         End Try
  892.     End Sub
  893.  
  894.     Private Sub ExcelManagment(ByVal filePath As String)
  895.         Try
  896.             Dim sFileName As String = filePath
  897.             Dim MyConnection As OleDbConnection
  898.             Dim DtTab As New System.Data.DataTable
  899.             Dim sTipoFile As String
  900.             ProgressBar1.Step = 10
  901.  
  902.             'Individuo il tipo di file Excel scelto
  903.             If IO.Path.GetExtension(sFileName.ToUpper) = ".XLS" Then
  904.                 sTipoFile = "Excel 8.0"
  905.                 MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0;Data Source='" & sFileName & "';Extended Properties=" & sTipoFile & ";")
  906.  
  907.             Else
  908.                 sTipoFile = "Excel 12.0"
  909.                 MyConnection = New System.Data.OleDb.OleDbConnection("provider=Microsoft.ACE.OLEDB.12.0;Data Source='" & sFileName & "';Extended Properties=" & sTipoFile & ";")
  910.             End If
  911.             ProgressBar1.PerformStep()
  912.  
  913.             ' Apre connessione con Excel
  914.             MyConnection.Open()
  915.  
  916.             'Popola data table
  917.             Dim myTableName = MyConnection.GetSchema("Tables").Rows(0)("TABLE_NAME")
  918.             Dim MyCommand As OleDbDataAdapter = New OleDbDataAdapter(String.Format("SELECT * FROM [{0}] ", myTableName), MyConnection)
  919.             MyCommand.TableMappings.Add("Table", "    ")
  920.             ProgressBar1.PerformStep()
  921.             MyCommand.Fill(DtTab)
  922.             MyConnection.Close()
  923.  
  924.             'insert and select datatable into db
  925.             TableCleaner(TempTab)
  926.             DataTableToSql(DtTab)
  927.             Dim sqlCmd = "SELECT " & TempTab & ".Targa as 'Targa ACI', Modello, Dealer, CodiceDealer as 'Codice Dealer', Venduto as 'Venduta a', Valore as 'Valore vendite Eurotax', "
  928.  
  929.             'Popola tabella risultante
  930.             If chkPgt.Checked Then
  931.                 sqlCmd = "SELECT " & TempTab & ".Targa as 'Targa ACI', " & TempTab & ".DataVendita as 'Data di Vendita', Modello, Dealer, CodiceDealer as 'Codice Dealer', Venduto as 'Venduta a', Valore as 'Valore vendite Eurotax', "
  932.             End If
  933.  
  934.             'Creazione Select
  935.             If Telaio.Checked Then
  936.                 sqlCmd = sqlCmd & "Telaio, "
  937.             End If
  938.  
  939.             If Produttore.Checked Then
  940.                 sqlCmd = sqlCmd & "Produttore, "
  941.             End If
  942.  
  943.             If Immatricolazione.Checked Then
  944.                 sqlCmd = sqlCmd & "DataImmatricolazione as 'Data di Immatricolazione', "
  945.             End If
  946.  
  947.             If CartaDiCircolazione.Checked Then
  948.                 sqlCmd = sqlCmd & "DataCartaCircolazione as 'Data Aggiornamento Carta di Circolazione', "
  949.             End If
  950.  
  951.             If KW.Checked Then
  952.                 sqlCmd = sqlCmd & "KW, "
  953.             End If
  954.  
  955.             If Classe.Checked Then
  956.                 sqlCmd = sqlCmd & "Classe as 'Classe/Uso', "
  957.             End If
  958.  
  959.             If Cilindrata.Checked Then
  960.                 sqlCmd = sqlCmd & "Cilindrata, "
  961.             End If
  962.  
  963.             If Alimentazione.Checked Then
  964.                 sqlCmd = sqlCmd & "Alimentazione, "
  965.             End If
  966.  
  967.             If Tara.Checked Then
  968.                 sqlCmd = sqlCmd & "Tara, "
  969.             End If
  970.  
  971.             If Posti.Checked Then
  972.                 sqlCmd = sqlCmd & "Posti as 'Posti n.ro', "
  973.             End If
  974.  
  975.             If Assi.Checked Then
  976.                 sqlCmd = sqlCmd & "Assi as 'Assi n.ro', "
  977.             End If
  978.  
  979.             If PrimaFormalita.Checked Then
  980.                 sqlCmd = sqlCmd & "PrimaFormalita as 'Prima Formalità', "
  981.             End If
  982.  
  983.             If DataPrimaFormalita.Checked Then
  984.                 sqlCmd = sqlCmd & "DataPrimaFormalita as 'Data Prima Formalità', "
  985.             End If
  986.  
  987.             If UltimaFormalita.Checked Then
  988.                 sqlCmd = sqlCmd & "UltimaFormalita as 'Ultima Formalità', "
  989.             End If
  990.  
  991.             If DataUltimaFormalita.Checked Then
  992.                 sqlCmd = sqlCmd & "DataUltimaFormalita as 'Data Ultima Formalità', "
  993.  
  994.             End If
  995.  
  996.             If Atto.Checked Then
  997.                 sqlCmd = sqlCmd & "Atto, "
  998.             End If
  999.  
  1000.             If DataAtto.Checked Then
  1001.                 sqlCmd = sqlCmd & "DataAtto as 'Data Atto', "
  1002.             End If
  1003.  
  1004.             If PrezzoVeicolo.Checked Then
  1005.                 sqlCmd = sqlCmd & "PrezzoVeicolo as 'Prezzo del Veicolo', "
  1006.             End If
  1007.  
  1008.             If Proprietario.Checked Then
  1009.                 sqlCmd = sqlCmd & "Proprietario, "
  1010.             End If
  1011.  
  1012.             If SessoTipoSocieta.Checked Then
  1013.                 sqlCmd = sqlCmd & "Tipo as 'Sesso/Tipo Società', "
  1014.             End If
  1015.  
  1016.             If DataNascita.Checked Then
  1017.                 sqlCmd = sqlCmd & "DataNascita as 'Data di Nascita', "
  1018.             End If
  1019.  
  1020.             If Comune.Checked Then
  1021.                 sqlCmd = sqlCmd & "ComuneNascita as 'Comune di Nascita', "
  1022.             End If
  1023.  
  1024.             If Residenza.Checked Then
  1025.                 sqlCmd = sqlCmd & "ComuneResidenza as 'Comune di Residenza', "
  1026.             End If
  1027.  
  1028.             If Indirizzo.Checked Then
  1029.                 sqlCmd = sqlCmd & "Indirizzo as 'Indirizzo di Residenza', "
  1030.             End If
  1031.             sqlCmd = sqlCmd.Substring(0, sqlCmd.Length - 2) & " FROM " & TempTab & ", " & Tab & ", Anagrafica WHERE " & TempTab & ".Targa = " & Tab & ".Targa AND " & Tab & ".Targa = Anagrafica.Targa_FK"
  1032.  
  1033.             Dim RemoteDtTab As New System.Data.DataTable
  1034.             Using myConn As New SqlConnection(connectionString)
  1035.                 myConn.Open()
  1036.                 Using loader As New SqlDataAdapter(sqlCmd, myConn)
  1037.                     loader.Fill(RemoteDtTab)
  1038.                     ProgressBar1.Value = 100
  1039.                     MsgBox("Elaborazione Completata.")
  1040.                     ProgressBar1.Value = 0
  1041.                 End Using
  1042.             End Using
  1043.  
  1044.             'Export to excel
  1045.             MsgBox("Inizio salvataggio file excel.")
  1046.             Dim strFilePath As String = filePath.Replace(Path.GetFileName(filePath), "")
  1047.             ExcelExport(RemoteDtTab, strFilePath)
  1048.  
  1049.         Catch ex As Exception
  1050.             MsgBox("Un errore è stato rilevato in ExcelManagment. Errore:" & vbLf & ex.Message)
  1051.             ProgressBar1.Value = 0
  1052.             Err.Clear()
  1053.             Exit Sub
  1054.         End Try
  1055.     End Sub
  1056.  
  1057.     Private Sub ExcelExport(ByVal ExportDataTable As DataTable, ByVal filePath As String)
  1058.         Try
  1059.             ProgressBar1.Value = 0
  1060.             ProgressBar1.Step = 1
  1061.             ProgressBar1.PerformStep()
  1062.  
  1063.             'Dichiarazione variabili
  1064.             Dim excel As New Microsoft.Office.Interop.Excel.Application
  1065.             Dim wBook As Microsoft.Office.Interop.Excel.Workbook
  1066.             Dim wSheet As Microsoft.Office.Interop.Excel.Worksheet
  1067.             wBook = excel.Workbooks.Add()
  1068.             wSheet = wBook.ActiveSheet()
  1069.             Dim dt As System.Data.DataTable = ExportDataTable
  1070.             Dim dc As System.Data.DataColumn
  1071.             Dim dr As System.Data.DataRow
  1072.             Dim colIndex As Integer = 0
  1073.             Dim rowIndex As Integer = 0
  1074.             Dim Counter As Integer = 0
  1075.             Dim indexProprietario As Integer
  1076.             Dim indexTipo As Integer
  1077.             Dim indexDataN As Integer
  1078.             Dim indexComuneN As Integer
  1079.             Dim indexComuneRes As Integer
  1080.             Dim indexIndirizzo As Integer
  1081.  
  1082.             'Scrittura header e salvataggio index blocco proprietari
  1083.             For Each dc In dt.Columns
  1084.                 colIndex = colIndex + 1
  1085.                 excel.Cells(1, colIndex) = dc.ColumnName
  1086.                 If dc.ColumnName = "Proprietario" Then
  1087.                     indexProprietario = colIndex
  1088.                 End If
  1089.                 If dc.ColumnName = "Sesso/Tipo Società" Then
  1090.                     indexTipo = colIndex
  1091.                 End If
  1092.                 If dc.ColumnName = "Data di Nascita" Then
  1093.                     indexDataN = colIndex
  1094.                 End If
  1095.                 If dc.ColumnName = "Comune di Nascita" Then
  1096.                     indexComuneN = colIndex
  1097.                 End If
  1098.                 If dc.ColumnName = "Comune di Residenza" Then
  1099.                     indexComuneRes = colIndex
  1100.                 End If
  1101.                 If dc.ColumnName = "Indirizzo di Residenza" Then
  1102.                     indexIndirizzo = colIndex
  1103.                 End If
  1104.             Next
  1105.  
  1106.             'Scrittura file excel
  1107.             For Each dr In dt.Rows
  1108.                 rowIndex += 1
  1109.                 colIndex = 0
  1110.                 Counter += 1
  1111.                 If Counter Mod 10 = 0 Then
  1112.                     ProgressBar1.PerformStep()
  1113.                 End If
  1114.                 For Each dc In dt.Columns
  1115.                     colIndex += 1
  1116.                     excel.Cells(rowIndex + 1, colIndex) = dr(dc.ColumnName)
  1117.                 Next
  1118.             Next
  1119.             rowIndex = 0
  1120.  
  1121.  
  1122.             'Unione proprietari multipli
  1123.             For Each dr In dt.Rows
  1124.                 rowIndex += 1
  1125.                 If wSheet.Cells(rowIndex, 1).Value = wSheet.Cells(rowIndex + 1, 1).Value Then
  1126.                     excel.DisplayAlerts = False
  1127.                     For Counter = 1 To indexProprietario - 1
  1128.                         wSheet.Range(wSheet.Cells(rowIndex, Counter), wSheet.Cells(rowIndex + 1, Counter)).Merge()
  1129.                     Next
  1130.                 End If
  1131.  
  1132.             Next
  1133.  
  1134.  
  1135.             Dim range As String = "A1:F1"
  1136.             Dim helper As Integer = 0
  1137.  
  1138.             'Impostazione Range
  1139.             For Each ctrl As Control In Controls
  1140.                 If TypeOf ctrl Is CheckBox AndAlso DirectCast(ctrl, CheckBox).Checked Then
  1141.                     helper += 1
  1142.                 End If
  1143.             Next
  1144.  
  1145.             If chkPgt.Checked Then
  1146.                 helper += 1
  1147.             End If
  1148.  
  1149.             Select Case helper
  1150.                 Case 2
  1151.                     range = "A1:F1"
  1152.                 Case 3
  1153.                     range = "A1:G1"
  1154.                 Case 4
  1155.                     range = "A1:H1"
  1156.                 Case 5
  1157.                     range = "A1:I1"
  1158.                 Case 6
  1159.                     range = "A1:J1"
  1160.                 Case 7
  1161.                     range = "A1:K1"
  1162.                 Case 8
  1163.                     range = "A1:L1"
  1164.                 Case 9
  1165.                     range = "A1:M1"
  1166.                 Case 10
  1167.                     range = "A1:N1"
  1168.                 Case 11
  1169.                     range = "A1:O1"
  1170.                 Case 12
  1171.                     range = "A1:P1"
  1172.                 Case 13
  1173.                     range = "A1:Q1"
  1174.                 Case 14
  1175.                     range = "A1:R1"
  1176.                 Case 15
  1177.                     range = "A1:S1"
  1178.                 Case 16
  1179.                     range = "A1:T1"
  1180.                 Case 17
  1181.                     range = "A1:U1"
  1182.                 Case 18
  1183.                     range = "A1:V1"
  1184.                 Case 19
  1185.                     range = "A1:W1"
  1186.                 Case 20
  1187.                     range = "A1:X1"
  1188.                 Case 21
  1189.                     range = "A1:Y1"
  1190.                 Case 22
  1191.                     range = "A1:Z1"
  1192.                 Case 23
  1193.                     range = "A1:AA1"
  1194.                 Case 24
  1195.                     range = "A1:AB1"
  1196.                 Case 25
  1197.                     range = "A1:AC1"
  1198.                 Case 26
  1199.                     range = "A1:AD1"
  1200.                 Case 27
  1201.                     range = "A1:AE1"
  1202.             End Select
  1203.  
  1204.  
  1205.             'creazione stile
  1206.             Dim style As Microsoft.Office.Interop.Excel.Style = wSheet.Application.ActiveWorkbook.Styles.Add("NewStyle")
  1207.             style.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow)
  1208.             style.HorizontalAlignment = Microsoft.Office.Interop.Excel.XlHAlign.xlHAlignLeft
  1209.  
  1210.  
  1211.  
  1212.             'applicazione stile
  1213.             wSheet.Range(range).Style = "NewStyle"
  1214.             wSheet.Range(range).BorderAround(Microsoft.Office.Interop.Excel.XlBordersIndex.xlEdgeBottom, Microsoft.Office.Interop.Excel.XlBorderWeight.xlMedium)
  1215.             wSheet.Columns.AutoFit()
  1216.             ProgressBar1.Step = 10
  1217.             ProgressBar1.PerformStep()
  1218.  
  1219.  
  1220.  
  1221.             Dim strFileName = filePath & Tab & ".xlsx"
  1222.             If System.IO.File.Exists(strFileName) Then
  1223.                 ProgressBar1.PerformStep()
  1224.                 System.IO.File.Delete(strFileName)
  1225.  
  1226.             End If
  1227.             ProgressBar1.PerformStep()
  1228.             wBook.SaveAs(strFileName)
  1229.             wBook.Close()
  1230.             ProgressBar1.PerformStep()
  1231.             excel.Quit()
  1232.             ProgressBar1.Value = 100
  1233.             MsgBox("File " & Path.GetFileName(strFileName) & " è stato salvato correttamente in " & strFileName)
  1234.             ProgressBar1.Value = 0
  1235.             ProgressBar1.Visible = False
  1236.             Dim result As DialogResult = Windows.Forms.DialogResult.No
  1237.             result = MessageBox.Show("Vuoi continuare con un'altra elaborazione?", "Leggi visure da pdf", MessageBoxButtons.YesNo)
  1238.             If result = Windows.Forms.DialogResult.Yes Then
  1239.                 Excel_Check.Visible = False
  1240.                 Pdf_Check.Visible = False
  1241.                 Excel_TextBox.Text = ""
  1242.                 PDF_TextBox.Text = ""
  1243.                 GC.Collect()
  1244.             Else
  1245.                 Me.Close()
  1246.             End If
  1247.  
  1248.         Catch ex As Exception
  1249.             MsgBox("Un errore è stato rilevato in ExcelEsport. Errore:" & vbLf & ex.Message)
  1250.             ProgressBar1.Value = 0
  1251.             Err.Clear()
  1252.         End Try
  1253.     End Sub
  1254.  
  1255.     Private Sub DataTableToSql(ByVal dt As DataTable)
  1256.         Try
  1257.             ProgressBar1.Step = 1
  1258.             Dim myConn As New SqlConnection(connectionString)
  1259.             Dim myCmd As SqlCommand
  1260.             Dim sqlCmd As String = "INSERT INTO " & TempTab & " VALUES(@Targa, @Modello, @Dealer, @Venduto, @CodiceDealer, @Valore)"
  1261.             If TempTab.Contains("PAI") Then
  1262.                 sqlCmd = "INSERT INTO " & TempTab & " VALUES(@Targa, @DataVendita, @Modello, @Dealer, @Venduto, @CodiceDealer, @Valore)"
  1263.                 myCmd = New SqlCommand(sqlCmd, myConn)
  1264.                 myCmd.Parameters.Add("@DataVendita", SqlDbType.SmallDateTime)
  1265.             Else
  1266.                 myCmd = New SqlCommand(sqlCmd, myConn)
  1267.             End If
  1268.             Dim counter As Integer = 0
  1269.             Dim dataVendita As String
  1270.             myConn.Open()
  1271.             myCmd.Parameters.Add("@Targa", SqlDbType.VarChar)
  1272.             myCmd.Parameters.Add("@Modello", SqlDbType.VarChar)
  1273.             myCmd.Parameters.Add("@Dealer", SqlDbType.VarChar)
  1274.             myCmd.Parameters.Add("@CodiceDealer", SqlDbType.VarChar)
  1275.             myCmd.Parameters.Add("@Venduto", SqlDbType.VarChar)
  1276.             myCmd.Parameters.Add("@Valore", SqlDbType.Float)
  1277.             For Each dr As DataRow In dt.Rows
  1278.                 If chkCit.Checked Then
  1279.                     myCmd.Parameters("@Targa").Value = dr(0).ToString()
  1280.  
  1281.                     myCmd.Parameters("@Modello").Value = dr(1).ToString()
  1282.  
  1283.                     myCmd.Parameters("@Dealer").Value = dr(2).ToString()
  1284.  
  1285.                     myCmd.Parameters("@CodiceDealer").Value = dr(3).ToString()
  1286.  
  1287.                     myCmd.Parameters("@Venduto").Value = dr(4).ToString()
  1288.  
  1289.                     myCmd.Parameters("@Valore").Value = Convert.ToDouble(dr(5).ToString().Replace(",", "."))
  1290.  
  1291.                 Else
  1292.  
  1293.                     myCmd.Parameters("@Targa").Value = dr(0).ToString()
  1294.  
  1295.                     dataVendita = "01/" & dr(2).ToString() & "/" & dr(1).ToString()
  1296.                     myCmd.Parameters("@DataVendita").Value = dataVendita
  1297.  
  1298.                     myCmd.Parameters("@Modello").Value = dr(3).ToString()
  1299.  
  1300.                     myCmd.Parameters("@Dealer").Value = dr(4).ToString()
  1301.  
  1302.                     myCmd.Parameters("@Venduto").Value = dr(5).ToString()
  1303.  
  1304.                     myCmd.Parameters("@CodiceDealer").Value = dr(6).ToString()
  1305.  
  1306.  
  1307.                     myCmd.Parameters("@Valore").Value = Convert.ToDouble(dr(7).ToString().Replace(",", "."))
  1308.  
  1309.                 End If
  1310.                 myCmd.ExecuteNonQuery()
  1311.                 counter += 1
  1312.                 If counter Mod 20 = 0 Then
  1313.                     ProgressBar1.PerformStep()
  1314.                 End If
  1315.  
  1316.             Next
  1317.             ProgressBar1.Value = 100
  1318.             myConn.Close()
  1319.         Catch ex As Exception
  1320.             MsgBox("Un errore è stato rilevato in DataTableToSql. Errore:" & vbLf & ex.Message)
  1321.             ProgressBar1.Value = 0
  1322.             Exit Sub
  1323.         End Try
  1324.     End Sub
  1325.  
  1326.     Private Sub TableCleaner(ByVal tablename As String)
  1327.         Try
  1328.             Using myConn As New SqlConnection(connectionString)
  1329.                 myConn.Open()
  1330.                 Using myCmd As New SqlCommand("DELETE FROM " & tablename, myConn)
  1331.                     myCmd.ExecuteNonQuery()
  1332.                 End Using
  1333.             End Using
  1334.         Catch ex As Exception
  1335.             Err.Clear()
  1336.         End Try
  1337.     End Sub
  1338.  
  1339.     Private Sub TableDeleter(ByVal tablename As String)
  1340.         Try
  1341.             Using myConn As New SqlConnection(connectionString)
  1342.                 myConn.Open()
  1343.                 Using myCmd As New SqlCommand("DROP TABLE " & tablename, myConn)
  1344.                     myCmd.ExecuteNonQuery()
  1345.                 End Using
  1346.             End Using
  1347.         Catch ex As Exception
  1348.             Err.Clear()
  1349.         End Try
  1350.     End Sub
  1351.  
  1352.     Private Sub all_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles all.Click
  1353.         Telaio.Checked = True
  1354.         Produttore.Checked = True
  1355.         Immatricolazione.Checked = True
  1356.         CartaDiCircolazione.Checked = True
  1357.         KW.Checked = True
  1358.         Classe.Checked = True
  1359.         Cilindrata.Checked = True
  1360.         Alimentazione.Checked = True
  1361.         Tara.Checked = True
  1362.         Posti.Checked = True
  1363.         Assi.Checked = True
  1364.         PrimaFormalita.Checked = True
  1365.         DataPrimaFormalita.Checked = True
  1366.         UltimaFormalita.Checked = True
  1367.         DataUltimaFormalita.Checked = True
  1368.         Atto.Checked = True
  1369.         DataAtto.Checked = True
  1370.         PrezzoVeicolo.Checked = True
  1371.         Proprietario.Checked = True
  1372.         SessoTipoSocieta.Checked = True
  1373.         DataNascita.Checked = True
  1374.         Comune.Checked = True
  1375.         Residenza.Checked = True
  1376.         Indirizzo.Checked = True
  1377.     End Sub
  1378.  
  1379.     Private Sub none_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles none.Click
  1380.         Telaio.Checked = False
  1381.         Produttore.Checked = False
  1382.         Immatricolazione.Checked = False
  1383.         CartaDiCircolazione.Checked = False
  1384.         KW.Checked = False
  1385.         Classe.Checked = False
  1386.         Cilindrata.Checked = False
  1387.         Alimentazione.Checked = False
  1388.         Tara.Checked = False
  1389.         Posti.Checked = False
  1390.         Assi.Checked = False
  1391.         PrimaFormalita.Checked = False
  1392.         DataPrimaFormalita.Checked = False
  1393.         UltimaFormalita.Checked = False
  1394.         DataUltimaFormalita.Checked = False
  1395.         Atto.Checked = False
  1396.         DataAtto.Checked = False
  1397.         PrezzoVeicolo.Checked = False
  1398.         Proprietario.Checked = False
  1399.         SessoTipoSocieta.Checked = False
  1400.         DataNascita.Checked = False
  1401.         Comune.Checked = False
  1402.         Residenza.Checked = False
  1403.         Indirizzo.Checked = False
  1404.     End Sub
  1405.  
  1406.     Private Sub DefSel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DefSel.Click
  1407.         Produttore.Checked = False
  1408.         Immatricolazione.Checked = False
  1409.         CartaDiCircolazione.Checked = False
  1410.         KW.Checked = False
  1411.         Classe.Checked = False
  1412.         Cilindrata.Checked = False
  1413.         Alimentazione.Checked = False
  1414.         Tara.Checked = False
  1415.         Posti.Checked = False
  1416.         Assi.Checked = False
  1417.         PrimaFormalita.Checked = False
  1418.         DataPrimaFormalita.Checked = False
  1419.         Atto.Checked = False
  1420.         DataAtto.Checked = False
  1421.         Comune.Checked = False
  1422.         Residenza.Checked = False
  1423.         Indirizzo.Checked = False
  1424.  
  1425.         Telaio.Checked = True
  1426.         UltimaFormalita.Checked = True
  1427.         DataUltimaFormalita.Checked = True
  1428.         PrezzoVeicolo.Checked = True
  1429.         Proprietario.Checked = True
  1430.         SessoTipoSocieta.Checked = True
  1431.         DataNascita.Checked = True
  1432.     End Sub
  1433.  
  1434.     Private Sub CreateTable(ByVal tableName As String)
  1435.         Try
  1436.             Dim sqlStr As String
  1437.             If tableName.Contains("_CAI") Then
  1438.                 sqlStr = "CREATE TABLE TempExcelTable_CAI(" & _
  1439.      "Targa nvarchar(7) PRIMARY KEY," & _
  1440.      "Modello nvarchar(1000) NULL," & _
  1441.      "Dealer nvarchar(1000) NULL," & _
  1442.      "Venduto nvarchar(1000) NULL," & _
  1443.      "CodiceDealer nvarchar(10) NULL," & _
  1444.      "Valore float(17) NULL)"
  1445.             Else
  1446.                 sqlStr = "CREATE TABLE TempExcelTable_PAI(" & _
  1447.      "Targa nvarchar(7) PRIMARY KEY," & _
  1448.      "DataVendita smalldatetime NULL," & _
  1449.      "Modello nvarchar(1000) NULL," & _
  1450.      "Dealer nvarchar(1000) NULL," & _
  1451.      "Venduto nvarchar(1000) NULL," & _
  1452.      "CodiceDealer nvarchar(10) NULL," & _
  1453.      "Valore float(17) NULL)"
  1454.             End If
  1455.             Using myConn As New SqlConnection(connectionString)
  1456.                 Using myCmd As New SqlCommand(sqlStr, myConn)
  1457.                     myCmd.CommandTimeout = 0
  1458.                     myCmd.CommandType = CommandType.Text
  1459.                     myCmd.ExecuteNonQuery()
  1460.                 End Using
  1461.             End Using
  1462.         Catch ex As Exception
  1463.             Err.Clear()
  1464.             Exit Sub
  1465.         End Try
  1466.     End Sub
  1467. End Class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement