Hello,

je suis en train de préparer une appli web qui génère des documents Word à la volée.

J'ai beaucoup de difficultés à trouver un moyen propre de faire ceci car il y a tant de méthodes décrites dans Google que je m'y perd.

Néanmoins, j'ai tout de même trouvé ce code qui fonctionne bien :

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
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Xml
Imports System.Xml.Linq
Imports DocumentFormat.OpenXml.Packaging
Partial Public Class Default2
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(sender As Object, e As EventArgs)
    End Sub
    Protected Sub btnUpload_Click(sender As Object, e As EventArgs)
        If FileUploadControl.HasFile Then
            Try
                Dim fileNameFromUser As String = FileUploadControl.FileName
                Session("FileNameFromUser") = fileNameFromUser
                Using memoryStream As New MemoryStream()
                    memoryStream.Write(FileUploadControl.FileBytes, 0, FileUploadControl.FileBytes.Length)
                    Using wDoc As WordprocessingDocument = WordprocessingDocument.Open(memoryStream, True)
                        Dim w As XNamespace = "http://schemas.openxmlformats.org/wordprocessingml/2006/main"
                        Dim body As XElement = wDoc.MainDocumentPart.GetXDocument().Descendants(w + "body").FirstOrDefault()
                        body.AddFirst(New XElement(w + "p", New XElement(w + "r", New XElement(w + "t", "Hello world"))))
                        wDoc.MainDocumentPart.PutXDocument()
                    End Using
                    Session("ByteArray") = memoryStream.ToArray()
                End Using
            Catch ex As Exception
                lblMessage.Text = "ERROR: " + ex.Message.ToString()
            End Try
        Else
            lblMessage.Text = "You have not specified a file."
        End If
    End Sub
    Protected Sub btnDownload_Click(sender As Object, e As EventArgs)
        Dim byteArray As Byte() = CType((Session("ByteArray")), Byte())
        Response.Clear()
        Response.ContentType = "application/octet-stream"
        Dim fileName As String = CType((Session("FileNameFromUser")), String)
        Response.AddHeader("Content-Disposition", [String].Format("attachment; filename={0}", fileName))
        Response.BinaryWrite(byteArray)
        Response.Flush()
        Response.[End]()
    End Sub
End Class
Public Module LocalExtensions
    <System.Runtime.CompilerServices.Extension()> _
    Public Function GetXDocument(part As OpenXmlPart) As XDocument
        Dim partXDocument As XDocument = part.Annotation(Of XDocument)()
        If partXDocument IsNot Nothing Then
            Return partXDocument
        End If
        Using partStream As Stream = part.GetStream()
            If partStream.Length = 0 Then
                partXDocument = New XDocument()
                partXDocument.Declaration = New XDeclaration("1.0", "UTF-8", "yes")
            Else
                Using partXmlReader As XmlReader = XmlReader.Create(partStream)
                    partXDocument = XDocument.Load(partXmlReader)
                End Using
            End If
        End Using
        part.AddAnnotation(partXDocument)
        Return partXDocument
    End Function
    <System.Runtime.CompilerServices.Extension()> _
    Public Sub PutXDocument(part As OpenXmlPart)
        Dim partXDocument As XDocument = part.GetXDocument()
        If partXDocument IsNot Nothing Then
            Using partStream As Stream = part.GetStream(FileMode.Create, FileAccess.Write)
                Using partXmlWriter As XmlWriter = XmlWriter.Create(partStream)
                    partXDocument.Save(partXmlWriter)
                End Using
            End Using
        End If
    End Sub
End Module
Le principe de ce code est simple, on upload un document word existant (c'est très commode car cela permet de créer des lettres types très facilement). On va ensuite intervenir directement dans le fichier pour ajouter des choses ou en remplacer. Une fois modifié, on renvoie le tout comme un fichier Word.

Outre le fait que je ne maitrise pas du tout Linq, ce que je n'arrive pas à trouver, c'est une page web qui me donne des références pour aller un peu plus loin, ajouter des notes de bas de page par exemple.

Pouvez-vous me dire si je suis sur la bonne route et m'indiquer le chemin ?

merci