Jump to content
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

footboyedit

Guardar XML em Excel

Recommended Posts

footboyedit

boas, estou a fazer um programa para guardar tags XML em Excel. Na primeira vez resulta só que na segunda pede para substituir ficheiro Excel. Como faço para adicionar no mesmo ficheiro em vez de substituir? Obrigado :)

Dim xlApp As Excel.Application
	Dim xlWorkBook As Excel.Workbook
	Dim xlWorkSheet As Excel.Worksheet
	Dim misValue As Object = System.Reflection.Missing.Value

	Dim ds As New DataSet
	Dim xmlFile As XmlReader
	Dim i, j As Integer

	xlApp = New Excel.ApplicationClass
	xlWorkBook = xlApp.Workbooks.Add(misValue)
	xlWorkSheet = xlWorkBook.Sheets("sheet1")

	xmlFile = XmlReader.Create("C:\qwerty.xml", New XmlReaderSettings())
	ds.ReadXml(xmlFile)

	For i = 0 To ds.Tables(0).Rows.Count - 1
		For j = 0 To ds.Tables(0).Columns.Count - 1
			xlWorkSheet.Cells(i + 1, j + 1) = ds.Tables(1).Rows(i).Item(j)
		Next
	Next

	xlWorkSheet.SaveAs("C:\qwerty.xls")
	xlWorkBook.Close()
	xlApp.Quit()

	releaseObject(xlApp)
	releaseObject(xlWorkBook)
	releaseObject(xlWorkSheet)

Share this post


Link to post
Share on other sites
tiago.f

Oi, em vez de criares um novo workbook:

xlWorkBook = xlApp.Workbooks.Add(misValue)
Tens que abrir o existente:
xlWorkBook  = Workbooks.Open("C:\querty.xls")

Valida tb se existe claro, senão tens mesmo que o criar

Share this post


Link to post
Share on other sites
footboyedit

Oi, em vez de criares um novo workbook:

xlWorkBook = xlApp.Workbooks.Add(misValue)
Tens que abrir o existente:
xlWorkBook  = Workbooks.Open("C:\querty.xls")

Valida tb se existe claro, senão tens mesmo que o criar

obrigado, fiz isso e agora dá erro dizendo que o qwerty.xls é só de leitura

Share this post


Link to post
Share on other sites
tiago.f

as permissões estão bem?

já agora:

xlWorkSheet.SaveAs("C:\qwerty.xls")

devia ser xlWorkBook.SaveAs(....

certo?

Share this post


Link to post
Share on other sites

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

×

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.