-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexcelClass.cs
More file actions
105 lines (89 loc) · 3.41 KB
/
excelClass.cs
File metadata and controls
105 lines (89 loc) · 3.41 KB
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
using System.Collections.Generic;
using System.Linq;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.IO;
namespace RecolectorEmail
{
static class excelClass
{
private static string path = "clientes.xlsx";
// TODO: Introduce método para encontrar el nombre de la ficha
private static string sheetName = "Hoja1";
public static void SetPath(string newPath)
{
path = newPath;
}
public static string GetPath()
{
return path;
}
// Gets the list of emails the excel has
public static List<string> GetExcelMails()
{
List<string> actualMails = new List<string>();
if (File.Exists(path))
{
FileStream contactosFile = File.OpenRead(path);
IWorkbook readWB = new XSSFWorkbook(contactosFile);
contactosFile.Close();
ISheet sheet = readWB.GetSheet(sheetName);
int row = 1;
while (sheet.GetRow(row) != null)
{
actualMails.Add(sheet.GetRow(row).GetCell(1).StringCellValue.Trim().ToLower());
row++;
}
}
return actualMails;
}
// Gets the value pair that is not contained in the excel
public static SortedList<string,string> GetNotContainedMails
(SortedList<string,string> toChekMails)
{
List<string> keyNewMails = new List<string>();
SortedList<string, string> newMails = new SortedList<string, string>();
List<string> currentSaved = GetExcelMails();
// Compares the inbox mails with the excel mails
keyNewMails = toChekMails.Keys.Except(currentSaved).ToList();
// Deletes the blacklisted mails
keyNewMails = keyNewMails.Except(BlackListMails.blacklistedMails).ToList();
foreach (var email in toChekMails)
{
foreach (var mail in keyNewMails)
{
if (email.Key == mail)
{
newMails.Add(email.Key, email.Value);
}
}
}
return newMails;
}
// Writes mails to the excel
public static void WriteNewMails(List<string> newMails)
{
FileStream leido = File.OpenRead(path);
IWorkbook writeWB = new XSSFWorkbook(leido);
leido.Close();
ISheet page = writeWB.GetSheet("Hoja1");
foreach (string mail in newMails)
{
// Insert name
page.CreateRow(page.LastRowNum + 1).CreateCell(0).
SetCellValue(mail.Split('>')[0].Trim());
// Insert mail
page.GetRow(page.LastRowNum).CreateCell(1).
SetCellValue("mailto:" + mail.Split('>','(')[1].Trim());
// Insert telephone number
if (mail.Contains('('))
{
page.GetRow(page.LastRowNum).CreateCell(2).SetCellValue(mail.Split('(', ')')[1]);
}
}
FileStream modificado = File.Create(path);
writeWB.Write(modificado);
modificado.Close();
}
}
}