This repository was archived by the owner on Nov 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPOExport.cs
More file actions
executable file
·159 lines (130 loc) · 4.43 KB
/
POExport.cs
File metadata and controls
executable file
·159 lines (130 loc) · 4.43 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
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
using System;
using System.IO;
using System.Reflection;
using System.Collections;
using PocketOutlook;
namespace POOMClient
{
// Used to get contacts list using GetDefaultFolder method
/// <summary>
/// Summary description for POExport.
/// </summary>
public class POExport
{
private const int folderContactsConst = 10;
private string separator = ";";
private string lineSeparator = "\n";
public POExport()
{
//
// TODO: Add constructor logic here
//
}
static public void Main()
{
POExport po = new POExport();
po.ExportWithTemplate("\\contacts.csv", "\\csvtemplate.csv");
}
public void ExportWithTemplate(string csvFilename, string templateFilename)
{
PropertyInfo[] fields;
string header = "";
Type cType = Type.GetType("PocketOutlook.Contact"); //pa.GetType();
// open the output file
FileStream fs = new FileStream(csvFilename, FileMode.Create);
/*
// Include all properties
Type cType = Type.GetType("PocketOutlook.Contact");
PropertyInfo[] fields = cType.GetProperties();
// write the header: fields names
{
// add the name of each field in the header record
foreach (PropertyInfo f in fields)
header += ProtectValue(f.Name) + separator;
}
*/
// read a custom mapping of fields
ArrayList fieldsNames = new ArrayList();
StreamReader template = new StreamReader(templateFilename);
for(;;)
{
string line = template.ReadLine();
if (line == null || line.Length == 0) break;
string[] record = line.Split(',');
fieldsNames.Add(UnprotectValue(record[0]));
string label = record.Length > 1 ? record[1] : record[0];
header += /*ProtectValue*/(label) + separator;
}
// Replace the last comma with a CR then save into file
byte[] data = EncodeRecord(header.Substring(0, header.Length - separator.Length) + lineSeparator);
fs.Write(data, 0, data.Length);
fields = new PropertyInfo[fieldsNames.Count];
for(int i=0; i<fieldsNames.Count; ++i)
{
fields[i] = cType.GetProperty((string) fieldsNames[i]);
}
//fields = (PropertyInfo[]) fieldsNames.ToArray(Type.GetType("PropertyInfo"));
Export(fields, fs);
}
private void Export(PropertyInfo[] fields, FileStream fs)
{
try
{
// The Application object is the only object which can be viewed
// by external libraries. Other PocketOutlook objects are created
// by calling various methods on the Application object.
PocketOutlook.Application app = new PocketOutlook.Application();
// Log the user onto a Pocket Outlook session
app.Logon();
// get contacts info
ItemCollection poicContactsCollection = app.GetDefaultFolder(folderContactsConst).Items;
PocketOutlook.Contact pa;
// Add all the Contacts to a ListView.
for (int i = 0; i < poicContactsCollection.Count; i++)
{
pa = (PocketOutlook.Contact) poicContactsCollection.Item(i + 1); // Starting with first item, item 'zero' does not exist
// Add contact information to the list view
//string[] displayInfo = new string[3];
//this.setupListViewItem(pa, out displayInfo[0], out displayInfo[1], out displayInfo[2]);
//this.lvContacts.Items.Add(new ListViewItem(displayInfo));
// Put the data values into a record string
string record = "";
foreach (PropertyInfo f in fields)
record += ProtectValue(f.GetValue(pa, null).ToString()) + separator;
// Replace the last comma with a CR then save into file
byte[] data = EncodeRecord(record.Substring(0, record.Length - separator.Length) + lineSeparator);
fs.Write(data, 0, data.Length);
}
// log the user out
app.Logoff();
}
catch (Exception exception)
{
System.Windows.Forms.MessageBox.Show(exception.ToString());
}
}
private string ProtectValue (string str)
{
if (str.Length > 0)
{
str = str.Replace("\n", "; ");
str = str.Replace("\r", "");
str = str.Replace("\"", "''");
str = "\"" + str + "\"";
}
return str;
}
private string UnprotectValue (string str)
{
if (str.StartsWith("\"") && str.EndsWith("\""))
{
str = str.Substring(1, str.Length-2);
}
return str;
}
private byte[] EncodeRecord (string str)
{
return System.Text.Encoding.Default.GetBytes(str);
}
}
}