MAdKulhas
Power Member
Boas tardes.
Hoje a programar para uma disciplina onde vamos Interacção de Aplicações, começamos hoje a dar a interoperabilidade com o Excel.
Na disciplina damos VB mas eu prefiro trabalhar em C# e estou com um problema a iniciar um novo documento em Excel. Coloco de seguida o meu código:
Não sei também se estou a fazer da melhor maneira, mas gostaria de saber a vossa opinião e como resolver o facto de estar ali a aparecer uma excepção estranha, visto que até já adicionei as referências ao Excel.
Cumprimentos.
Hoje a programar para uma disciplina onde vamos Interacção de Aplicações, começamos hoje a dar a interoperabilidade com o Excel.
Na disciplina damos VB mas eu prefiro trabalhar em C# e estou com um problema a iniciar um novo documento em Excel. Coloco de seguida o meu código:
Código:
[SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]System;[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]System.Collections.Generic;[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]System.ComponentModel;[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]System.Data;[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]System.Drawing;[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]System.Text;[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]System.Windows.Forms;[/COLOR]
[/SIZE][SIZE=2][COLOR=#0000ff]using [/COLOR][/SIZE][SIZE=2][COLOR=cyan]Excel;[/COLOR]
[/SIZE]
[SIZE=2][COLOR=#0000ff]namespace [/COLOR][/SIZE][SIZE=2][COLOR=cyan]WindowsApplication1[/COLOR]
{
[/SIZE][SIZE=2][COLOR=#0000ff] public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]partial[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]class [/COLOR][/SIZE][SIZE=2][COLOR=#008080]WinExcel[/COLOR][/SIZE][SIZE=2] : [/SIZE][SIZE=2][COLOR=#008080]Form
[/COLOR][/SIZE][SIZE=2] {
[/SIZE][SIZE=2][COLOR=#0000ff] private[/COLOR][/SIZE][SIZE=2] Excel.[/SIZE][SIZE=2][COLOR=#008080]Application[/COLOR][/SIZE][SIZE=2] excel;
[/SIZE][SIZE=2][COLOR=#0000ff] public[/COLOR][/SIZE][SIZE=2] WinExcel()
{
InitializeComponent();
}
[/SIZE]
[SIZE=2][COLOR=#0000ff] private [/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]void[/COLOR][/SIZE][SIZE=2] button1_Click([/SIZE][SIZE=2][COLOR=#0000ff]object[/COLOR][/SIZE][SIZE=2] sender, [/SIZE][SIZE=2][COLOR=#008080]EventArgs[/COLOR][/SIZE][SIZE=2] e)
{
excel = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] Excel.[/SIZE][SIZE=2][COLOR=#008080]Application[/COLOR][/SIZE][SIZE=2]();
excel.Visible = [/SIZE][SIZE=2][COLOR=#0000ff]true[/COLOR][/SIZE][SIZE=2];
Excel.[/SIZE][SIZE=2][COLOR=#008080]Workbook[/COLOR][/SIZE][SIZE=2] wrk = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2][COLOR=#008080]Workbook[/COLOR][/SIZE][SIZE=2](); //ocorre uma excepção aqui nesta linha
wrk = excel.Workbooks.Add([/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2]);
Excel.[/SIZE][SIZE=2][COLOR=#008080]Worksheet[/COLOR][/SIZE][SIZE=2] sheet = [/SIZE][SIZE=2][COLOR=#0000ff]new[/COLOR][/SIZE][SIZE=2] Excel.[/SIZE][SIZE=2][COLOR=#008080]Worksheet[/COLOR][/SIZE][SIZE=2]();
sheet = (Excel.[/SIZE][SIZE=2][COLOR=#008080]Worksheet[/COLOR][/SIZE][SIZE=2])wrk.Worksheets[1];
sheet.Columns[[/SIZE][SIZE=2][COLOR=#800000]"1"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"A"[/COLOR][/SIZE][SIZE=2]] = [/SIZE][SIZE=2][COLOR=#800000]"Ol "[/COLOR][/SIZE][SIZE=2];
wrk.SaveAs([/SIZE][SIZE=2][COLOR=#800000]"C:\ficheiro.xls"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]"xls"[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#800000]""[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], Excel.[/SIZE][SIZE=2][COLOR=#008080]XlSaveAsAccessMode[/COLOR][/SIZE][SIZE=2].xlNoChange, [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2]);
wrk.Close([/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]null[/COLOR][/SIZE][SIZE=2]);
excel.Quit();
System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#008080]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(sheet);
System.Runtime.InteropServices.[/SIZE][SIZE=2][COLOR=#008080]Marshal[/COLOR][/SIZE][SIZE=2].ReleaseComObject(wrk);
}
}
}
[/SIZE]
Não sei também se estou a fazer da melhor maneira, mas gostaria de saber a vossa opinião e como resolver o facto de estar ali a aparecer uma excepção estranha, visto que até já adicionei as referências ao Excel.
Cumprimentos.