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 | }
|