Je cherche à réaliser un alerte qui a besoin de lire et écrire des informations dans un fichier excel stocké dans une liste.
Savez vous comment réaliser cette lecture et écriture dans une case définie?
Je cherche à réaliser un alerte qui a besoin de lire et écrire des informations dans un fichier excel stocké dans une liste.
Savez vous comment réaliser cette lecture et écriture dans une case définie?
Si tu cherches un peu sur le net, tu trouveras tout un tas de snippets qui feront ton bonheur.
Voici par exemple 2 methodes, l'une pour écrire une chaine de caractères, et l'autre pour écrire une valeur entière.
Sur codeplex, il y'a également un projet qui facilite la lecture/écriture dans les fichiers Excel 2007.
Bref, en cherchan un peu, tu devrais tout trouver.
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303 private bool XLInsertNumberIntoCell(string fileName, string sheetName, string addressName, string value) { const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/" + "relationships/officeDocument"; const string worksheetSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; PackagePart documentPart = null; Uri documentUri = null; bool returnValue = false; using (Package xlPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { foreach (System.IO.Packaging.PackageRelationship relationship in xlPackage.GetRelationshipsByType(documentRelationshipType)) { documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri); documentPart = xlPackage.GetPart(documentUri); break; } if (documentPart != null) { XmlDocument doc = new XmlDocument(); doc.Load(documentPart.GetStream()); XmlNamespaceManager nsManager = new XmlNamespaceManager(doc.NameTable); nsManager.AddNamespace("d", doc.DocumentElement.NamespaceURI); string searchString = string.Format("//d:sheet[@name='{0}']", sheetName); XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager); if (sheetNode != null) { XmlAttribute relationAttribute = sheetNode.Attributes["r:id"]; if (relationAttribute != null) { string relId = relationAttribute.Value; PackageRelationship sheetRelation = documentPart.GetRelationship(relId); Uri sheetUri = PackUriHelper.ResolvePartUri(documentUri, sheetRelation.TargetUri); PackagePart sheetPart = xlPackage.GetPart(sheetUri); XmlDocument xDoc = new XmlDocument(); xDoc.Load(sheetPart.GetStream()); System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"^(?<col>\D+)(?<row>\d+)"); string rowNumber = r.Match(addressName).Result("${row}"); XmlNode cellnode = xDoc.SelectSingleNode( string.Format("//d:sheetData/d:row/d:c[@r='{0}']", addressName), nsManager); if (cellnode == null) { XmlElement cellElement = xDoc.CreateElement("c", worksheetSchema); cellElement.Attributes.Append(xDoc.CreateAttribute("r")); cellElement.Attributes["r"].Value = addressName; XmlElement valueElement = xDoc.CreateElement("v", worksheetSchema); valueElement.InnerText = value.ToString(); cellElement.AppendChild(valueElement); cellElement.Attributes.Append(xDoc.CreateAttribute("s")); cellElement.Attributes["s"].Value = "0"; XmlNode rowNode = xDoc.SelectSingleNode(string.Format( "//d:sheetData/d:row[@r='{0}']", rowNumber), nsManager); if (rowNode == null) { XmlNode sheetDataNode = xDoc.SelectSingleNode("//d:sheetData", nsManager); if (sheetDataNode != null) { XmlElement rowElement = xDoc.CreateElement("row", worksheetSchema); rowElement.Attributes.Append(xDoc.CreateAttribute("r")); rowElement.Attributes["r"].Value = rowNumber; rowElement.AppendChild(cellElement); sheetDataNode.AppendChild(rowElement); returnValue = true; } } else { XmlAttribute styleAttr = ((XmlAttribute)(rowNode.Attributes.GetNamedItem("s"))); if (styleAttr != null) { cellElement.Attributes["s"].Value = styleAttr.Value; } XmlNode biggerNode = null; XmlNodeList cellNodes = rowNode.SelectNodes("./d:c", nsManager); if (cellNodes != null) { foreach (XmlNode node in cellNodes) { if (String.Compare(node.Attributes["r"].Value, addressName) > 0) { biggerNode = node; break; } } } if (biggerNode == null) { rowNode.AppendChild(cellElement); } else { rowNode.InsertBefore(cellElement, biggerNode); } returnValue = true; } } else { cellnode.Attributes.RemoveNamedItem("t"); XmlNode valueNode = cellnode.SelectSingleNode("d:v", nsManager); if (valueNode == null) { valueNode = xDoc.CreateElement("v", worksheetSchema); cellnode.AppendChild(valueNode); } valueNode.InnerText = value.ToString(); returnValue = true; } xDoc.Save(sheetPart.GetStream(FileMode.Create, FileAccess.Write)); } } } } return returnValue; } private bool XLInsertStringIntoCell(string fileName, string sheetName, string addressName, string value) { const string documentRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument"; const string worksheetSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; const string sharedStringsRelationshipType = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/sharedStrings"; const string sharedStringSchema = "http://schemas.openxmlformats.org/spreadsheetml/2006/main"; PackagePart documentPart = null; Uri documentUri = null; bool returnValue = false; using (Package xlPackage = Package.Open(fileName, FileMode.Open, FileAccess.ReadWrite)) { foreach (System.IO.Packaging.PackageRelationship relationship in xlPackage.GetRelationshipsByType(documentRelationshipType)) { documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri); documentPart = xlPackage.GetPart(documentUri); break; } if (documentPart != null) { XmlDocument doc = new XmlDocument(); doc.Load(documentPart.GetStream()); NameTable nt = new NameTable(); XmlNamespaceManager nsManager = new XmlNamespaceManager(nt); nsManager.AddNamespace("d", worksheetSchema); nsManager.AddNamespace("s", sharedStringSchema); string searchString = string.Format("//d:sheet[@name='{0}']", sheetName); XmlNode sheetNode = doc.SelectSingleNode(searchString, nsManager); if (sheetNode != null) { XmlAttribute relationAttribute = sheetNode.Attributes["r:id"]; if (relationAttribute != null) { string relId = relationAttribute.Value; PackageRelationship sheetRelation = documentPart.GetRelationship(relId); Uri sheetUri = PackUriHelper.ResolvePartUri(documentUri, sheetRelation.TargetUri); PackagePart sheetPart = xlPackage.GetPart(sheetUri); XmlDocument xDoc = new XmlDocument(nt); xDoc.Load(sheetPart.GetStream()); foreach (System.IO.Packaging.PackageRelationship stringRelationship in documentPart.GetRelationshipsByType(sharedStringsRelationshipType)) { Uri sharedStringsUri = PackUriHelper.ResolvePartUri(documentUri, stringRelationship.TargetUri); PackagePart stringPart = xlPackage.GetPart(sharedStringsUri); if (stringPart != null) { XmlDocument stringDoc = new XmlDocument(nt); stringDoc.Load(stringPart.GetStream()); int index = -1; XmlNode stringNode = stringDoc.SelectSingleNode(string.Format("//s:si[s:t='{0}']", value), nsManager); if (stringNode == null) { // You didn't find the string in the table, so add it now. stringNode = stringDoc.CreateElement("si", sharedStringSchema); XmlElement textNode = stringDoc.CreateElement("t", sharedStringSchema); textNode.InnerText = value; stringNode.AppendChild(textNode); stringDoc.DocumentElement.AppendChild(stringNode); } if (stringNode != null) { XmlNodeList nodes = stringNode.SelectNodes("preceding-sibling::d:si", nsManager); index = nodes.Count; System.Text.RegularExpressions.Regex r = new System.Text.RegularExpressions.Regex(@"^(?<col>\D+)(?<row>\d+)"); string rowNumber = r.Match(addressName).Result("${row}"); XmlNode cellNode = xDoc.SelectSingleNode(string.Format("//d:sheetData/d:row/d:c[@r='{0}']", addressName), nsManager); if (cellNode == null) { XmlElement cellElement = xDoc.CreateElement("c", worksheetSchema); cellElement.Attributes.Append(xDoc.CreateAttribute("r")); cellElement.Attributes["r"].Value = addressName; cellElement.Attributes.Append(xDoc.CreateAttribute("t")); cellElement.Attributes["t"].Value = "s"; cellElement.Attributes.Append(xDoc.CreateAttribute("s")); cellElement.Attributes["s"].Value = "0"; XmlElement valueElement = xDoc.CreateElement("v", worksheetSchema); valueElement.InnerText = index.ToString(); cellElement.AppendChild(valueElement); XmlNode rowNode = xDoc.SelectSingleNode(string.Format("//d:sheetData/d:row[@r='{0}']", rowNumber), nsManager); if (rowNode == null) { XmlNode sheetDataNode = xDoc.SelectSingleNode("//d:sheetData", nsManager); if (sheetDataNode != null) { XmlElement rowElement = xDoc.CreateElement("row", worksheetSchema); rowElement.Attributes.Append(xDoc.CreateAttribute("r")); rowElement.Attributes["r"].Value = rowNumber; rowElement.AppendChild(cellElement); sheetDataNode.AppendChild(rowElement); returnValue = true; } } else { XmlAttribute styleAttr = ((XmlAttribute)(rowNode.Attributes.GetNamedItem("s"))); if (styleAttr != null) { cellElement.Attributes["s"].Value = styleAttr.Value; } XmlNode biggerNode = null; XmlNodeList cellNodes = rowNode.SelectNodes("./d:c", nsManager); if (cellNodes != null) { foreach (XmlNode node in cellNodes) { if (String.Compare(node.Attributes["r"].Value, addressName) > 0) { biggerNode = node; break; } } } if (biggerNode == null) { rowNode.AppendChild(cellElement); } else { rowNode.InsertBefore(cellElement, biggerNode); } returnValue = true; } } else { if (cellNode.Attributes["t"] == null) { cellNode.Attributes.Append(xDoc.CreateAttribute("t")); } cellNode.Attributes["t"].Value = "s"; XmlNode valueNode = cellNode.SelectSingleNode("d:v", nsManager); if (valueNode == null) { valueNode = xDoc.CreateElement("v", worksheetSchema); cellNode.AppendChild(valueNode); } valueNode.InnerText = index.ToString(); returnValue = true; } stringDoc.Save(stringPart.GetStream(FileMode.Create, FileAccess.Write)); xDoc.Save(sheetPart.GetStream(FileMode.Create, FileAccess.Write)); } } break; } } } } } return returnValue; }
A ouai les fonctions sont quand même balaises...
Je pensais que c'était beaucoup plus simple que cela.
J'avais trouvé ce tutoriel:
http://stephaneey.developpez.com/tut...ervices/#LIV-A
Mais comme un boulet, je n'arrivais pas à utiliser la classe ExcelService à cause de l'oubli d'ajout de la référence web.
Vous avez un bloqueur de publicités installé.
Le Club Developpez.com n'affiche que des publicités IT, discrètes et non intrusives.
Afin que nous puissions continuer à vous fournir gratuitement du contenu de qualité, merci de nous soutenir en désactivant votre bloqueur de publicités sur Developpez.com.
Partager