-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExelExport.cs
133 lines (118 loc) · 3.81 KB
/
ExelExport.cs
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows;
namespace TSP
{
class ResaultAlgorithm
{
public class Resault
{
private double _time;
public double Time
{
get { return _time; }
set { _time = value; }
}
private double _length;
public double Length
{
get { return _length; }
set { _length = value; }
}
public Resault(double time, double length)
{
_time = time;
_length = length;
}
};
private string _name;
public string Name
{
get { return _name; }
}
private int _countCities;
public int CountCities
{
get { return _countCities; }
}
private List<Resault> _data;
public Resault this[int i]
{
get { return _data[i]; }
set { _data[i] = value; }
}
public int CountData
{
get { return _data.Count; }
}
public ResaultAlgorithm (int countCities, string name)
{
_countCities = countCities;
_name = name;
_data =new List<Resault>();
}
public void Add(double time, double length)
{
_data.Add (new Resault(time, length));
}
}
class ExelExport
{
public static void Save( ResaultAlgorithm[] data)
{
try
{
//создаём новое Excel приложение
Excel.Application exApp = new Excel.Application();
//добавляем рабочую книгу
exApp.Workbooks.Add();
//обращаемся к активному листу (по умолчанию он первый)
Worksheet workSheet = (Worksheet)exApp.ActiveSheet;
//добавляем строку в Excel файл
workSheet.Cells[1, 1] = "Вершин";
for (int j = 0; j < data[0].CountData; j ++)
{
workSheet.Cells[j + 2, 1] = data[0].CountCities;
}
for (int i = 0; i < data.Length; i++)
{
int c = (i + 1) * 2;
workSheet.Cells[1, c] = data[i].Name + " Длина";
workSheet.Cells[1, c + 1] = data[i].Name + " Время";
for (int j = 0; j < data[i].CountData; j++)
{
ResaultAlgorithm.Resault res = data[i][j];
workSheet.Cells[j + 2, c] = res.Length;
workSheet.Cells[j + 2, c + 1] = res.Time;
}
}
workSheet.Columns.AutoFit();
object misValue = System.Reflection.Missing.Value;
//Сохранение в Excel файл;
string path = AppDomain.CurrentDomain.BaseDirectory + data[0].CountCities + "_TSP.xlsx";
try
{
workSheet.SaveAs(path, 51);
}
catch (COMException)
{
MessageBox.Show("Не сохранено. Закройте документ " + path);
exApp.Quit();
}
exApp.Quit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}