Autor Beitrag
DiaryOfDreams
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Mo 01.03.10 17:12 
Ich habe eine .xlsx_Datei erstellt. Diese soll jetzt geöffnet werden. Dann wird etwas reingeschrieben und die Datei wieder geschlossen. Allerdings bekomme ich immer eine Fehlermeldung:
COMException wurde nicht behandelt
"Zugriff auf das schreibgeschützte Dokument 'Test.xlsx' nicht möglich."

Woran liegt das?
Das ganze ist bis jetzt nur experimentiererei, also nicht wundern falls der Code an der einen oder anderen Stelle komisch wirkt.

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
using System;
using System.Windows.Forms;
using System.Reflection;
using Excel = Microsoft.Office.Interop.Excel;
using System.Runtime.InteropServices;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private const string EXCEL_FILE = @"C:\Dokumente und Einstellungen\daniel.gorski\Desktop\Test.xlsx";

        Excel.ApplicationClass exApp;
        Excel.Workbook exWb;
        Excel.Worksheet exWs;

        object missing = Missing.Value;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                initExcelApplication();
                initExcelWorkbook();
                initExcelWorksheet();

                exApp.DisplayAlerts = false;

                exWb = openExcelWorkbook(EXCEL_FILE);

                writeExcelCell(11"Name:");
                writeExcelCell(12"Vorname:");

                writeExcelCell(31"Presley");
                writeExcelCell(32"Elvis");

                saveExcelWorkbookAs(exWb, EXCEL_FILE);

                closeAllExcelWorkbooks();
            }
            finally
            {
                exApp.Quit();
                freeExcelRam();
            }
        }

        //write to a cell of current workbook
        private void writeExcelCell(int row, int column, string value)
        {
            if (exWs != null)
            {
                exWs.Cells[row, column] = value;
            }
        }

        //removes all Excel-Elements from RAM
        private void freeExcelRam()
        {
            foreach (Excel.Workbook workbook in exApp.Workbooks)
            {
                Marshal.ReleaseComObject(workbook);
            }
            Marshal.FinalReleaseComObject(exApp);
        }

        private void closeAllExcelWorkbooks()
        {
            foreach (Excel.Workbook workbook in exApp.Workbooks)
            {
                workbook.Close(true, EXCEL_FILE, missing);
            }
        }

        private void saveExcelWorkbookAs(Excel.Workbook workbook, string filename)
        {
            workbook.SaveAs(filename, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
        }

        private Excel.Workbook openExcelWorkbook(string filename)
        {
            return exApp.Workbooks.Open(filename, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing, missing);
        }

        //initializes an worksheet in the workbook
        private void initExcelWorksheet()
        {
            if (exWb != null)
            {
                exWs = (Excel.Worksheet)exWb.ActiveSheet;
            }
        }

        //initializes an workbook in Excel
        private void initExcelWorkbook()
        {
            if (exApp != null)
            {
                exWb = exApp.Workbooks.Add(Missing.Value);
            }
        }

        //initializes an Excel-Application
        private void initExcelApplication()
        {
            exApp = new Excel.ApplicationClass();
        }
    }
}
DiaryOfDreams Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 22



BeitragVerfasst: Di 02.03.10 09:55 
Okay, habe den Code jetzt umgeschrieben und es scheint nun alles problemlos zu funktionieren.

Die beiden wichtigsten Änderungen waren wohl folgende Methoden.

ausblenden C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
private void closeAllExcelWorkbooks()
{
    foreach (Excel.Workbook workbook in exApp.Workbooks)
    {
        workbook.Close(false, missing, missing);
    }
}

private void saveExcelWorkbookAs(Excel.Workbook workbook, string filename)
{
    foreach (Excel.Worksheet worksheet in workbook.Worksheets)
    {
        worksheet.SaveAs(EXCEL_FILE, missing, missing, missing, missing, missing, missing, missing, missing, missing);
    }

    workbook.SaveAs(filename, missing, missing, missing, missing, missing, Excel.XlSaveAsAccessMode.xlNoChange, missing, missing, missing, missing, missing);
}