/
Form2.cs
287 lines (255 loc) · 11.7 KB
/
Form2.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;
using Microsoft.Office.Core;
using System.Reflection;
using System.IO;
using System.Diagnostics;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
using Microsoft.Office.Interop.Excel;
using Excel = Microsoft.Office.Interop.Excel;
namespace Rapor
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
// Find and Replace:
private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
{
object matchCase = true;
object matchWholeWord = true;
object matchWildCards = false;
object matchSoundLike = false;
object nmatchAllForms = false;
object forward = true;
object format = false;
object matchKashida = false;
object matchDiactitics = false;
object matchAlefHamza = false;
object matchControl = false;
object read_only = false;
object visible = true;
object replace = 2;
object wrap = 1;
wordApp.Selection.Find.Execute(ref findText,
ref matchCase, ref matchWholeWord,
ref matchWildCards, ref matchSoundLike,
ref nmatchAllForms, ref forward,
ref wrap, ref format, ref replaceWithText,
ref replace, ref matchKashida,
ref matchDiactitics, ref matchAlefHamza,
ref matchControl);
}
string pathResim = null;
// Create The Document:
private void CreateWordDocument(object filename, object saveAs, object image)
{
List<int> processesbeforegen = getRunningProcesses();
object missing = Missing.Value;
Word.Application wordApp = new Word.Application();
Document aDoc;
if (File.Exists((string)filename))
{
object readOnly = false;
wordApp.Visible = false;
aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
object replaceAll = WdReplace.wdReplaceAll;
foreach (Section section in aDoc.Sections)
{
Word.Range headerRange = section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
headerRange.Find.Text = "proje_dokuman_no";
headerRange.Find.Replacement.Text = proje_dokuman_no.Text;
headerRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref replaceAll, ref missing,
ref missing, ref missing, ref missing);
headerRange.Find.Text = "firma_dokuman_no";
headerRange.Find.Replacement.Text = firma_dokuman_no.Text;
headerRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref replaceAll, ref missing,
ref missing, ref missing, ref missing);
headerRange.Find.Text = "ekipman_ad";
headerRange.Find.Replacement.Text = textBox5.Text;
headerRange.Find.Execute(ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref replaceAll, ref missing,
ref missing, ref missing, ref missing);
}
aDoc.Activate();
// find and replace commands:
FindAndReplace(wordApp, "siparis_no", siparis_no.Text);
FindAndReplace(wordApp, "mm-yy", tarih.Text);
FindAndReplace(wordApp, "musteri_adi", musteri_adi.SelectedItem);
FindAndReplace(wordApp, "ekipman_adi", textBox5.Text);
FindAndReplace(wordApp, "gign_no", textBox6.Text);
FindAndReplace(wordApp, "uretici_firma", uretici_firma.Text);
FindAndReplace(wordApp, "test_tarihi", maskedTextBox1.Text);
FindAndReplace(wordApp, "rapor_tarihi", maskedTextBox2.Text);
FindAndReplace(wordApp, "olcumu_gerceklestiren", comboBox2.SelectedItem);
FindAndReplace(wordApp, "telefon_no", telefon_no.Text);
FindAndReplace(wordApp, "e_posta", e_posta.Text);
FindAndReplace(wordApp, "gemi_adi", gemi_adi.Text);
FindAndReplace(wordApp, "olcum_yeri", comboBox1.SelectedItem);
FindAndReplace(wordApp, "mahal", textBox7.Text);
FindAndReplace(wordApp, "ekipman_model", textBox8.Text);
FindAndReplace(wordApp, "ekipman_guc", textBox10.Text);
FindAndReplace(wordApp, "ekipman_devir", textBox9.Text);
FindAndReplace(wordApp, "proje_dokuman_no", proje_dokuman_no.Text);
FindAndReplace(wordApp, "firma_dokuman_no", firma_dokuman_no.Text);
FindAndReplace(wordApp, "olcum_cihazi", olcum_cihazi.Text);
FindAndReplace(wordApp, "analizator", analizator.Text);
FindAndReplace(wordApp, "ivme_olcer", ivme_olcer.Text);
FindAndReplace(wordApp, "fft_size", average.Text);
FindAndReplace(wordApp, "average_quantity", average.Text);
FindAndReplace(wordApp, "windowing", windowing.Text);
FindAndReplace(wordApp, "resolution", resolution.Text);
}
else
{
MessageBox.Show("file dose not exist.");
return;
}
int i = 1;
while (aDoc.Content.Bookmarks.Exists("a_" + i))
{
object oMissed = aDoc.Bookmarks["a_" + i].Range;
object oLinktoFile = false;
object oSaveWithDocument = true;
aDoc.InlineShapes.AddPicture(pathResim + @"\" + "Ortam " + i + ".png", ref oLinktoFile, ref oSaveWithDocument, ref oMissed);
i++;
}
// Save as the document:
aDoc.SaveAs2(ref saveAs, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing);
MessageBox.Show("The report was created successfully.");
List<int> processesaftergen = getRunningProcesses();
killProcesses(processesbeforegen, processesaftergen);
}
public List<int> getRunningProcesses()
{
List<int> ProcessIDs = new List<int>();
foreach (Process clsProcess in Process.GetProcesses())
{
if (Process.GetCurrentProcess().Id == clsProcess.Id)
continue;
if (clsProcess.ProcessName.Contains("WINWORD") || clsProcess.ProcessName.Contains("EXCEL"))
{
ProcessIDs.Add(clsProcess.Id);
}
}
return ProcessIDs;
}
public void killProcesses(List<int> processesbeforegen, List<int> processesaftergen)
{
foreach (int pidafter in processesaftergen)
{
bool processfound = false;
foreach (int pidbefore in processesbeforegen)
{
if (pidafter == pidbefore)
{
processfound = true;
}
}
if (processfound == false)
{
Process clsProcess = Process.GetProcessById(pidafter);
clsProcess.Kill();
}
}
}
private void Import_Image_Click_1(object sender, EventArgs e)
{
if (graphs.ShowDialog() == DialogResult.OK)
{
pathResim = graphs.SelectedPath;
textBox2.Text = graphs.SelectedPath;
button1.Enabled = true;
progressBar1.Value = 60;
}
}
// Load The Current Document:
private void Button2_Click_1(object sender, EventArgs e)
{
if (LoadDoc.ShowDialog() == DialogResult.OK)
{
textBox1.Text = LoadDoc.FileName;
progressBar1.Value = 80;
}
}
// Create The New Document:
private void Button1_Click_1(object sender, EventArgs e)
{
if (SaveDoc.ShowDialog() == DialogResult.OK)
{
CreateWordDocument(textBox1.Text, SaveDoc.FileName, pathResim);
progressBar1.Value = 100;
}
}
private void Export_Click_1(object sender, EventArgs e)
{
if (exportpath.ShowDialog() == DialogResult.OK)
{
textBox4.Text = exportpath.SelectedPath;
progressBar1.Value = 20;
}
}
private void Openexcel_Click_1(object sender, EventArgs e)
{
List<int> processesbeforegen = getRunningProcesses();
if (excelac.ShowDialog() == DialogResult.OK)
{
textBox3.Text = excelac.FileName;
Excel.Application exc = new Excel.Application();
{
exc.Visible = false;
}
exc.Workbooks.Open(textBox3.Text);
Worksheet sheet = exc.Worksheets[1];
sheet.Activate();
foreach (Worksheet ws in exc.Worksheets)
{
ChartObjects chartobjects = ws.ChartObjects();
foreach (ChartObject co in chartobjects)
{
co.Select();
Excel.Chart chart = co.Chart;
chart.Export(exportpath.SelectedPath + @"\" + chart.Name + ".png", "PNG", false);
}
}
progressBar1.Value = 40;
MessageBox.Show("The graphs are imported successfully.");
List<int> processesaftergen = getRunningProcesses();
killProcesses(processesbeforegen, processesaftergen);
}
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
LinearGradientBrush brushx = new LinearGradientBrush(this.ClientRectangle, Color.SkyBlue, Color.Gray, 90F);
e.Graphics.FillRectangle(brushx, this.ClientRectangle);
}
}
}