Jump to content

Recommended Posts

Posted

Boas

Com ajuda, ja consegui fazer o datagrid ir buscar o ficheiro excel que quer.

mas agora ele as vezes da erro.

Aqui:

   

Dim da As New OleDbDataAdapter("SELECT * FROM [Folha1$]", conexao_Excel)

Diz que a folha1 nao esta declarada( ou e sheet1 ou plan1) mas como fasso para meter os 3??

outra duvida, para gravar depois de ter alterado, meti este codigo.

Dim excel As Object
        excel = CreateObject("excel.Application")
        excel.Application.Workbooks.Add(True)
        excel.Save(textbox.text)

ele cria, em vez de criar, vou ver a folha e esta toda em branko.

Agradecia ajuda

dscp o incomodo

Obrigado

Posted
Dim excel As Object
        excel = CreateObject("excel.Application")
        excel.Application.Workbooks.Add(True)
        excel.Save(textbox.text)

Entao ele ali esta a criar um novo ficheiro de excel e grava... nao tens de preencher o ficheiro?

There are two ways to write error-free programs; only the third one works.

Posted

Boas

desde ja agradeço a ajuda 🙂

sim, tenho ke ir buscar um ficheiro excel( ja konseguido) depois se kiser alterar alguma coisa, haja 1 botao para gravar.

Como haverei de meter?

Dim excel As Object
        excel = CreateObject("excel.Application")
        excel.Application.Workbooks.open(textbox.text)
        excel.Save(textbox.text)

e sobre esta duvida.

Dim da As New OleDbDataAdapter("SELECT * FROM [Folha1$]", conexao_Excel)

como posso resolver??

Obrigado pela ajuda

Posted

Testei aqui no meu o open e tb nao da, ele apaga o outro, é como cria-se um novo, tu tens que fazer, para o que esta no datagrid, gravar numa folha de excel, pelo ke presebi.

Em relaçao a outra duvida, nao te consigo ajudar..

Bom resto de trabalho

Posted

Boas

Sim ja consigo, aparecer no datagrid o conteúdo do excel, mas agora queria que quando eu fizesse alguma alteração, haja um botão para gravar as alterações na mesma folha excel( ou outra isso depois o utilizador escolhe)

Ja criei o botão, já consigo gravar este o codigo que meti

 'Dim myStream As Stream
        'Dim saveFileDialog1 As New SaveFileDialog()
        'saveFileDialog1.FileName = "Introduza o Nome"
        'saveFileDialog1.Filter = " Excel 2003 (*.xls) |*.xls"
        'saveFileDialog1.FilterIndex = 2
        'saveFileDialog1.RestoreDirectory = True

        'If saveFileDialog1.ShowDialog() = DialogResult.OK Then
        '    myStream = saveFileDialog1.OpenFile()
        '    If (myStream IsNot Nothing) Then
        '        myStream.Close()

        '        MsgBox(saveFileDialog1.FileName, MsgBoxStyle.Information, "Bem Sucedido")
        '    End If
        'End If

mas quando vou depois ver a folha excel, ela esta em branco, como tivesse criado uma folha nova.

Espero ter sido esclarecedor

Obrigado pela ajuda

Posted

dos que vi, estes 2  foram os melhores, mas mm assim n konsegui meter.

Dim appExcel As New Excel.Application
Dim worExcel As Excel.Workbook
Dim sheExcel As Excel.Worksheet

worExcel = appExcel.Workbooks.Add
sheExcel = appExcel.Sheets(1)
sheExcel.Name = "Faturas.xls"

Dim strString As String

strString = "OLEDB;Provider=SQLOLEDB.1;Password=;Persist Security Info=True;
User ID=sa;Initial Catalog=;Data Source= "

With sheExcel.QueryTables.Add(Connection:=strString, Destination:=sheExcel.Range("A1"))
   .CommandType = Excel.XlCmdType.xlCmdSql
   .CommandText = "Procedure SQL"
   .Name = "+New SQL Server Connection"
   .FieldNames = True
   .RowNumbers = False
   .FillAdjacentFormulas = False
   .PreserveFormatting = True
   .RefreshOnFileOpen = False
   .BackgroundQuery = True
   .RefreshStyle = Excel.XlCellInsertionMode.xlInsertDeleteCells
   .SavePassword = False
   .SaveData = False
   .AdjustColumnWidth = True
   .RefreshPeriod = 0
   .PreserveColumnInfo = True
   .Refresh(BackgroundQuery:=False)

End With
sheExcel.QueryTables(1).Delete()

appExcel.Visible = True

e este

      Public Function DataSetToExcel(ByVal dtSource As System.Data.DataSet, ByVal sFileName As String)

            Dim iRowCount As Integer = dtSource.Tables(0).Rows.Count

            Dim iColCount As Integer = dtSource.Tables(0).Columns.Count

            Dim oData(iRowCount, iColCount) As Object

            Dim iRow As Integer, iCol As Integer

            For iRow = 0 To iRowCount - 1

                For iCol = 0 To iColCount - 1

                    oData(iRow, iCol) = dtSource.Tables(0).Rows(iRow).Item(iCol)

                Next

            Next

            ' Start Excel and get Application object
            Dim oExcel As Excel.Application = New Excel.Application()
            oExcel.Visible = True  ' Make visible

            ' Get a new workbook
            Dim oBook As Excel._Workbook = CType(oExcel.Workbooks.Add(Missing.Value), Excel._Workbook)
            Dim oSheet As Excel._Worksheet = CType(oBook.ActiveSheet, Excel._Worksheet)

            Dim oRange As Excel.Range = oSheet.Range("A1")
            oRange = oRange.Resize(iRowCount, iColCount)
            oRange.Value = oData

            oSheet.SaveAs(sFileName)
            oExcel.Workbooks.Close()
            oExcel.Quit()

            oBook = Nothing
            oSheet = Nothing
            oExcel = Nothing
        End Function



    End Class

Obrigado pela ajuda

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.