Forum: PC-Programmierung Excel-Tabelle mit C# Forms anzeigen und bearbeiten


von Tim (Gast)


Lesenswert?

Hallo

Ich möchte mit einer C# Form Anwendung eine EXCEL Tabelle anzeigen und 
dann jeweils die einzelne Spalten miteinander verrechnen. Dafür ist es 
z.B. auch erforderlich neue Spalten hinzuzufügen.

Die Anzeige der EXCEL-Tabelle habe ich wie folgt umgesetzt:
1
using System;
2
using System.Collections.Generic;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Linq;
7
using System.Text;
8
using System.Threading.Tasks;
9
using System.Windows.Forms;
10
using Excel = Microsoft.Office.Interop.Excel;
11
using Microsoft.Office.Core;
12
13
namespace Messwert
14
{
15
    public partial class Form1 : Form
16
    {
17
        private Excel.Application ExcelObj = null;
18
        public Form1()
19
        {
20
            InitializeComponent();
21
            ExcelObj = new Excel.Application();
22
            Open();
23
        }
24
25
        private void Open()
26
        {
27
            if(ExcelObj == null)
28
            { 
29
                MessageBox.Show("ERROR");
30
                System.Windows.Forms.Application.Exit();
31
32
            }else{
33
34
                ExcelObj.Visible = true;
35
36
            }
37
38
        }
39
40
        private void Form1_Load(object sender, EventArgs e)
41
        {
42
43
        }
44
45
        string[] ConvertToStringArray(System.Array values)
46
        {
47
            // create a new string array
48
            string[] theArray = new string[values.Length];
49
            // loop through the 2-D System.Array and populate the 1-D String Array
50
            for (int i = 1; i <= values.Length; i++)
51
            {
52
                if (values.GetValue(1, i) == null)
53
                    theArray[i - 1] = "";
54
                else
55
                    theArray[i - 1] = (string)values.GetValue(1, i).ToString();
56
            }
57
            return theArray;
58
59
60
        }
61
62
        private void button1_Click(object sender, EventArgs e)
63
        {
64
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
65
            //ListView listView1 = new ListView();
66
            
67
            // prepare open file dialog to only search for excel files (had trouble setting this in design view)
68
            openFileDialog1.FileName = "*.xls"; if (openFileDialog1.ShowDialog() == DialogResult.OK)
69
            {
70
                // Here is the call to Open a Workbook in Excel 
71
                // It uses most of the default values (except for the read-only which we set to true)
72
                Excel.Workbook theWorkbook = ExcelObj.Workbooks.Open(openFileDialog1.FileName, 0, true, 5,
73
                "", "", true, Excel.XlPlatform.xlWindows, "\t", false, false, 0, true);
74
                // get the collection of sheets in the workbook
75
                Excel.Sheets sheets = theWorkbook.Worksheets;
76
                // get the first and only worksheet from the collection of worksheets
77
                Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1);
78
                // loop through 10 rows of the spreadsheet and place each row in the list view
79
                for (int i = 1; i <= 20; i++)
80
                {
81
                    
82
                    //Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
83
                    //Excel.Range range = worksheet.get_Range(i.ToString(), i.ToString());
84
                    
85
                    Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
86
                    System.Array myvalues = (System.Array)range.Cells.Value;
87
                    string[] strArray = ConvertToStringArray(myvalues);
88
                    listView1.Items.Add(new ListViewItem(strArray));
89
                }
90
            }
91
92
93
94
           
95
96
        }
97
98
        
99
100
        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
101
        {
102
103
        }
104
    }
105
}

Hier ergeben sich bei mir nun folgende Fragen:

Warum gibt das Programm erst 10 leer Zeilen aus und fängt erste dann an 
die Daten aus der Excel-Tabelle darzustellen?
Wie kann ich es erreichen, dass nur eine Reihe (z.B. A) in der ListView 
angezeigt wird.
1
 Excel.Range range = worksheet.get_Range("A" + i.ToString(), "J" + i.ToString());
 wenn ich statt "J" "A" eingebe wird das Programm mit Fehler beendet. 
Warum?
Gibt es die Möglichkeit, dass sich MS Excel nicht im Hintergrund öffnet, 
wenn die Excel-Datei geöffnet wird?

Danke

von Tim (Gast)


Angehängte Dateien:

Lesenswert?

Hallo Hier noch eine Beispiel EXCEL-Tabelle die z.B. zum Test verwende.

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.