نمایش سند Word در صفحه وب با ASP.Net
با سلام در این مقاله با مثال توضیح خواهیم داد که چگونه فایلهای Word (DOC و DOCX) را در صفحه وب در ASP.Net با استفاده از C # و VB.Net نمایش دهید.
سند Word آپلود میکنیم و سپس با استفاده از Microsoft Office Interop Library به HTML تبدیل می شود و در نهایت HTML تبدیل شده آن را در صفحه وب ASP.Net با استفاده از C # و VB.Net نمایش می دهد .
اضافه کردن مرجع کتابخانه Microsoft Office Interop
برای اضافه کردن مرجع کتابخانه Microsoft Office Interop ، روی Project در Solution Explorer کلیک راست کرده و Add Reference را کلیک کنید.
سپس از پنجره زیر، Microsoft Office Interop.Word را انتخاب کنید.

کد HTML
کد HTML زیر شامل کنترل ASP.Net FileUpload، دکمه و یک DIV HTML است.
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnUpload" runat="server" Text="submit" OnClick="Upload" />
<hr />
<div id="dvWord" runat="server"></div>
فضاهای نام
شما باید فضای نامهای زیر را وارد کنید.
C #
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text.RegularExpressions;
VB.Net
Imports System.IO
Imports Microsoft.Office.Interop.Word
Imports System.Text.RegularExpressions
نمایش سند Word در صفحه وب با ASP.Net
رویداد handler زیر هنگامی که دکمه Submit کلیک می شود، فراخوانی می شود.
فایل Word Document آپلود شده برای اولین بار در پوشه Temp در پوشه Project ذخیره می شود و سپس فایل Document Word به رشته HTML با استفاده از Microsoft Office Interop Library تبدیل می شود .
در نهایت رشته HTML به ویژگی InnerHtml از DIV HTML اختصاص خواهد یافت.
C #
protected void Upload(object sender, EventArgs e)
{
object documentFormat = 8;
string randomName = DateTime.Now.Ticks.ToString();
object htmlFilePath = Server.MapPath("~/Temp/") + randomName + ".htm";
string directoryPath = Server.MapPath("~/Temp/") + randomName + "_files";
object fileSavePath = Server.MapPath("~/Temp/") + Path.GetFileName(FileUpload1.PostedFile.FileName);
//If Directory not present, create it.
if (!Directory.Exists(Server.MapPath("~/Temp/")))
{
Directory.CreateDirectory(Server.MapPath("~/Temp/"));
}
//Upload the word document and save to Temp folder.
FileUpload1.PostedFile.SaveAs(fileSavePath.ToString());
//Open the word document in background.
_Application applicationclass = new Application();
applicationclass.Documents.Open(ref fileSavePath);
applicationclass.Visible = false;
Document document = applicationclass.ActiveDocument;
//Save the word document as HTML file.
document.SaveAs(ref htmlFilePath, ref documentFormat);
//Close the word document.
document.Close();
//Read the saved Html File.
string wordHTML = System.IO.File.ReadAllText(htmlFilePath.ToString());
//Loop and replace the Image Path.
foreach (Match match in Regex.Matches(wordHTML, "<v:imagedata.+?src=[\"'](.+?)[\"'].*?>", RegexOptions.IgnoreCase))
{
wordHTML = Regex.Replace(wordHTML, match.Groups[1].Value, "Temp/" + match.Groups[1].Value);
}
//Delete the Uploaded Word File.
System.IO.File.Delete(fileSavePath.ToString());
dvWord.InnerHtml = wordHTML;
}
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim documentFormat As Object = 8
Dim randomName As String = DateTime.Now.Ticks.ToString
Dim htmlFilePath As Object = Server.MapPath("~/Temp/") & randomName + ".htm"
Dim directoryPath As String = Server.MapPath("~/Temp/") & randomName + "_files"
Dim fileSavePath As Object = Server.MapPath("~/Temp/") & Path.GetFileName(FileUpload1.PostedFile.FileName)
'If Directory not present, create it.
If Not Directory.Exists(Server.MapPath("~/Temp/")) Then
Directory.CreateDirectory(Server.MapPath("~/Temp/"))
End If
'Upload the word document and save to Temp folder.
FileUpload1.PostedFile.SaveAs(fileSavePath.ToString)
'Open the word document in background.
Dim applicationclass As _Application = New Application
applicationclass.Documents.Open(fileSavePath)
applicationclass.Visible = False
Dim document As Document = applicationclass.ActiveDocument
'Save the word document as HTML file.
document.SaveAs(htmlFilePath, documentFormat)
'Close the word document.
document.Close()
'Read the saved Html File.
Dim wordHTML As String = System.IO.File.ReadAllText(htmlFilePath.ToString)
'Loop and replace the Image Path.
For Each match As Match In Regex.Matches(wordHTML, "<v:imagedata.+?src=[""'](.+?)[""'].*?>", RegexOptions.IgnoreCase)
wordHTML = Regex.Replace(wordHTML, match.Groups(1).Value, ("Temp/" + match.Groups(1).Value))
Next
'Delete the Uploaded Word File.
System.IO.File.Delete(fileSavePath.ToString)
dvWord.InnerHtml = wordHTML
End Sub