最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

excel - Getting error code 0x800A03EC in my application - Stack Overflow

programmeradmin3浏览0评论

The behavior of my program is as follows:

  1. The user selects an Excel file to standardize.
  2. Once the columns are ordered, the user clicks the "Export" button, and the column headers are automatically replaced with those from StandardColumnOrder. Then, all the necessary data is copied. The program currently works correctly, but when a file is selected where the column headers are different, I get the error code 0x800A03EC. However, when I open the generated Excel file, I don't see any signs of malfunction.

Can anyone help me understand why this error code appears?

Private Sub BtnExport_Click(sender As Object, e As EventArgs) Handles BtnExport.Click
    Try
        ' Percorso del file di destinazione
        Dim famiglieDiScontoPath As String = iniFile.ReadValue("Percorsi", "FamiglieDiSconto")
        Dim xlNewApp As New Application()
        Dim xlNewWorkbook As Workbook
        Dim xlNewWorksheet As Worksheet
        Dim fileExists As Boolean = System.IO.File.Exists(famiglieDiScontoPath)

        ' Se il file esiste, aprilo; altrimenti creane uno nuovo
        If System.IO.File.Exists(famiglieDiScontoPath) Then
            ' Apri il file esistente SENZA mostare finestre di conferma
            xlNewWorkbook = xlNewApp.Workbooks.Open(famiglieDiScontoPath, [ReadOnly]:=False, [Editable]:=True)
        Else
            ' Crea un nuovo file se non esiste
            xlNewWorkbook = xlNewApp.Workbooks.Add()
        End If

        ' Ottieni il foglio "Famiglie Di Sconto"
        Try
            xlNewWorksheet = xlNewWorkbook.Sheets("Famiglie Di Sconto")
        Catch ex As Exception
            ' Se il foglio non esiste, crealo
            xlNewWorksheet = xlNewWorkbook.Sheets(1)
            xlNewWorksheet.Name = "Famiglie Di Sconto"
        End Try

        ' **1. Recupera combinazioni esistenti nel file di destinazione**
        Dim existingEntries As New HashSet(Of String)
        Dim lastExistingRow As Integer = xlNewWorksheet.Cells(xlNewWorksheet.Rows.Count, 1).End(XlDirection.xlUp).Row
        If lastExistingRow < 2 Then lastExistingRow = 1 ' Assicura che parta da riga 2 in poi

        For rowIndex As Integer = 2 To lastExistingRow
            Dim existingCodiceUnivoco As String = xlNewWorksheet.Cells(rowIndex, 1).Value
            Dim existingSconto As String = xlNewWorksheet.Cells(rowIndex, 2).Value
            Dim existingPrezzo As String = xlNewWorksheet.Cells(rowIndex, 3).Value
            If Not String.IsNullOrEmpty(existingCodiceUnivoco) And Not String.IsNullOrEmpty(existingSconto) Then
                existingEntries.Add(existingCodiceUnivoco & "_" & existingSconto & "_" & existingPrezzo)
            End If
        Next

        ' **2. Trova la prima riga disponibile per i nuovi dati**
        Dim nextRow As Integer = lastExistingRow + 1
        ' **3. Creiamo un HashSet per nuovi dati da copiare**

        Dim uniqueEntries As New HashSet(Of String)
        ' Trova gli indici delle colonne nel file originale

        Dim codiceUnivocoIndex As Integer = columnHeaders.IndexOf("Codice Univoco Azienda") + 1
        Dim scontoIndex As Integer = columnHeaders.IndexOf("Sconto") + 1
        Dim prezzoIndex As Integer = columnHeaders.IndexOf("Prezzo") + 1

        ' **4. Scansiona il file di origine e copia solo nuove combinazioni**
        For rowIndex As Integer = headerRow + 1 To xlWorksheet.UsedRange.Rows.Count
            Dim codiceUnivocoValue As String = xlWorksheet.Cells(rowIndex, codiceUnivocoIndex).Value
            Dim scontoValue As String = xlWorksheet.Cells(rowIndex, scontoIndex).Value
            Dim prezzoValue As String = xlWorksheet.Cells(rowIndex, prezzoIndex).Value
            Dim entryKey As String = codiceUnivocoValue & "_" & scontoValue & "_" & prezzoValue

            ' Se la combinazione non è già presente nel file di destinazione, aggiungila
            If Not existingEntries.Contains(entryKey) And Not uniqueEntries.Contains(entryKey) Then
                uniqueEntries.Add(entryKey)
                xlNewWorksheet.Cells(nextRow, 1).Value = codiceUnivocoValue ' "Codice Univoco Azienda" in colonna A
                xlNewWorksheet.Cells(nextRow, 2).Value = scontoValue ' "Sconto" in colonna B
                xlNewWorksheet.Cells(nextRow, 3).Value = prezzoValue ' "Prezzo" in colonna C
                nextRow += 1
            End If
        Next

        ' **5. Salva e chiudi il file di destinazione**
        xlNewWorkbook.Save()
        xlNewWorkbook.Close(SaveChanges:=True)
        xlNewApp.Quit()

        ' **Rilascia le risorse**
        ReleaseObject(xlNewWorksheet)
        ReleaseObject(xlNewWorkbook)
        ReleaseObject(xlNewApp)

    Catch ex As Exception
        MessageBox.Show($"Errore durante l'esportazione: {ex.Message}", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Error)
    End Try

    Try
        ' Verifica che gli oggetti Excel siano inizializzati
        If xlApp Is Nothing OrElse xlWorkbook Is Nothing OrElse xlWorksheet Is Nothing Then
            MessageBox.Show("Non c'è un file Excel aperto. Apri un file prima di esportare.", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        ' Verifica che un codice sia stato selezionato nella ComboBox
        Dim selectedCodice As String = ComboBoxCodici.SelectedItem?.ToString()

        If String.IsNullOrEmpty(selectedCodice) Then
            MessageBox.Show("Seleziona un codice univoco dalla ComboBox prima di esportare.", "Errore", MessageBoxButtons.OK, MessageBoxIcon.Warning)
            Return
        End If

        ' Esporta i dati in un nuovo file Excel
        SaveFileDialog1.Filter = "Excel Files|*.xls;*.xlsx;*.xlsm"

        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            Dim exportFilePath = SaveFileDialog1.FileName
            Dim newWorkbook = xlApp.Workbooks.Add()
            Dim newWorksheet = CType(newWorkbook.Sheets(1), Worksheet)
            ' Calcola il numero totale di righe da elaborare
            Dim totalRows = xlWorksheet.UsedRange.Rows.Count - headerRow
            Dim currentRow = 0

            ProgressBar1.Visible = True
            ProgressBar1.Value = 0

            ' Cambia i nomi delle colonne secondo l'ordine standard e aggiungi titoli al nuovo file
            For colIndex = 0 To ListBox1.Items.Count - 1
                Dim currentHeader As String = ""

                If colIndex < ListBox1.Items.Count Then
                    currentHeader = ListBox1.Items(colIndex).ToString()
                End If

                If Not String.IsNullOrEmpty(currentHeader) Then
                    newWorksheet.Cells(1, colIndex + 1).Value = currentHeader
                End If

                ' Usa il nome della colonna standard se necessario
                If colIndex < standardColumnOrder.Count AndAlso currentHeader <> standardColumnOrder(colIndex) Then
                    currentHeader = standardColumnOrder(colIndex)
                End If
                ' Imposta il nome della colonna nel nuovo file
                newWorksheet.Cells(1, colIndex + 1).Value = currentHeader
            Next

            ' Copia i dati ordinati dalla ListBox al nuovo foglio Excel

            For rowIndex = headerRow + 1 To xlWorksheet.UsedRange.Rows.Count
                For colIndex = 1 To ListBox1.Items.Count
                    Dim columnName = ListBox1.Items(colIndex - 1).ToString()
                    Dim originalIndex = columnHeaders.IndexOf(columnName) + 1
                    ' Copia il valore dalla colonna originale
                    Dim cellValue = If(originalIndex > 0, xlWorksheet.Cells(rowIndex, originalIndex).Value, "")
                    ' Se la colonna è "Codice Univoco Azienda", usa il valore selezionato
                    If colIndex = 5 Then
                        cellValue = selectedCodice
                    End If

                    ' Scrivi il valore nella nuova cella
                    newWorksheet.Cells(rowIndex - headerRow + 1, colIndex).Value = If(cellValue IsNot Nothing, cellValue, "")
                Next

                ' Aggiorna la barra di caricamento
                currentRow += 1
                ProgressBar1.Value = CInt((currentRow / totalRows) * 100)
                System.Windows.Forms.Application.DoEvents()
            Next

            ' Salva il nuovo file
            MessageBox.Show(exportFilePath)
            newWorkbook.SaveAs(exportFilePath)
            newWorkbook.Close()

            ' Carica i dati esportati nella GridView
            LoadExportedDataIntoGridView(exportFilePath)

            ' Aggiorna lo stato dei pulsanti
            isExported = True
            BtnUploadToDatabase.Enabled = True
            BtnExport.Enabled = False
            ProgressBar1.Visible = False
            MessageBox.Show("File esportato con successo.")
        End If
    Catch ex As Exception
        MessageBox.Show($"Errore durante l'esportazione: {ex.Message}")
        ' Non chiudere gli oggetti Excel per permettere ulteriori tentativi
    Finally
        ' Assicurati che la ProgressBar sia nascosta
        ProgressBar1.Visible = False
    End Try
End Sub

Even though I receive this error code the program works perfectly

发布评论

评论列表(0)

  1. 暂无评论