网站不会发布使用openxml

时间:2018-04-27 14:26:10

标签: asp.net vb.net file openxml

情况如下:

Asp.Net Web Forms网站使用Open XML读取(通过流)word文档(docx)。然后我在文档中插入一些文本,然后将文件写回到不同的位置。然后通过电子邮件将其发送给最终用户。所有这些都很有效。

我遇到的问题是我无法通过网站编写新文件。我收到以下错误: “该进程无法访问该文件(此处为文件名),因为它正在另一个进程中使用”

我已确认是该网站(或IIS)正在保留该文件。

以下是读取原始文件并生成新文件的代码:

    Private Function GetDocument(worddoc As String) As Integer
    Dim byteArray As Byte() = File.ReadAllBytes("\\WEB-DEV-1\HR_Documents\" & worddoc)
    Using Stream As New MemoryStream()
        Stream.Write(byteArray, 0, CInt(byteArray.Length))
        Try
            'Set Row & Cell variables
            Dim rowNum As Integer = 0
            Dim cellNum As Integer = 0
            'Set File Stream
            Using doc As WordprocessingDocument = WordprocessingDocument.Open(Stream, True)
                'Employee Name Insert
                'Find first table in document
                Dim tbl1 As Table = doc.MainDocumentPart.Document.Body.Elements(Of Table).First()
                'First Row in tbl
                Dim row As TableRow = tbl1.Elements(Of TableRow)().ElementAt(0)
                'Find first cell in row
                Dim cell As TableCell = row.Elements(Of TableCell)().ElementAt(0)
                'Insert selected Employee Name
                Dim p As Paragraph = cell.Elements(Of Paragraph)().First()
                Dim r As Run = p.Elements(Of Run)().First()
                Dim txt As Text = r.Elements(Of Text)().First()
                txt.Text = "Employee Name: " & ddlEmployeeList.SelectedItem.Text

                'Supervisor Name Insert
                'Check for form
                If ddlFormChoice.SelectedIndex <> 2 Then
                    'Reset row to supervisors row in table
                    row = tbl1.Elements(Of TableRow)().ElementAt(1)
                ElseIf ddlFormChoice.SelectedIndex = 2 Then
                    'Reset row to supervisors row in table
                    row = tbl1.Elements(Of TableRow)().ElementAt(2)
                End If
                If ddlFormChoice.SelectedIndex <> 2 Then
                    'Reset cell to supervisor cell in row
                    cell = row.Elements(Of TableCell)().ElementAt(1)
                ElseIf ddlFormChoice.SelectedIndex = 2 Then
                    'Reset cell to supervisor cell in row
                    cell = row.Elements(Of TableCell)().ElementAt(0)
                End If

                'Insert selected Employee Name
                p = cell.Elements(Of Paragraph)().First()
                r = p.Elements(Of Run)().First()
                txt = r.Elements(Of Text)().First()
                If ddlFormChoice.SelectedIndex <> 2 Then
                    txt.Text = "Supervisor: " & ddlSupervisorList.SelectedItem.Text
                ElseIf ddlFormChoice.SelectedIndex = 2 Then
                    txt.Text = "Manager/Supervisor: " & ddlSupervisorList.SelectedItem.Text
                End If
                doc.Close()
            End Using
            'Save File to temp location
            File.WriteAllBytes("\\WEB-DEV-1\HR_Documents\TempDocs\" & worddoc, Stream.ToArray())
            Stream.Close()
            Stream.Dispose()
            Return 1
        Catch ex As Exception
            Return Nothing
        End Try
    End Using
End Function

我关闭了OpenXML文档和流以及处理流但是当我尝试从调用该函数的主要子文件中删除文件时,我得到了上面列出的错误。

我错过了什么?我关闭了文档,流,并处理了流。为什么网站仍然保留文件?

请注意尝试删除文件的代码行;

File.Delete("\\Web-Dev-1\HR_Documents\TempDocs\" & fileAttach)

1 个答案:

答案 0 :(得分:0)

所以在一天中的大部分时间后我终于找到了问题所在。文档创建,保存和通过电子邮件发送后,它通过电子邮件方法保存。出于某种原因,我认为当方法完成时它处理了邮件消息,但事实并非如此。

一旦我添加了处置线,它一切正常。

仅谷歌搜索了将近两天。 :|