بروز رسانی بهمن ۱ام, ۱۳۹۹ ۳:۳۴ بعد از ظهر
فروردین ۱۷, ۱۳۹۵ admin مقالات 0
خواندن محتوای فایـل word با مصرف از ASP.Net
گاهي از مواقع نیاز داریم که محتوای فایـل Word آپلود شده را در textbox نمایش دهیم، پس تصمیم گرفتیم که در این متن نحوه خواندن فایـل word را از طریق ASP.Net روش دهیم. پس با ما همراه باشید تا گام به گام نشان دهیم که چگونه می توان فایـل Word را خواند محتوای آن را در یک TextBox نمایش داد.
فایل Word
موارد مورجهان ز:
موارد مورجهان ز زیر احتمالا نتیجه شرایط مختلف باشند، برخي از شرایط رایج به صورت زیر هستند :
برای آپلود رزومه نمایش محتوای آن در TextBox به عنوان خلاصه
ذخیره این رزومه در دیتابیس که بعدها جهت CV یا پارس کردن(parsing) رزومه مفید است .
در بلاگ یا فروم ها جهت آپلود مستقیم محتوای فایـل در ویرایشگر که ویرایش ذخیره محتوای فایل را سريع تر می نماید .
هم اکنون مجوز دهید با ایجاد یک Web Application آسان به صورت زیر، مراحل موردنظر را دنبال کنیم.
Start >> All Programs>> Microsoft Visual Studio 2012
File>> New WebSite>> C#>> Empty WebSite(برای پیشگیری از اضافه شدن Master Page)
نام مناسبی جهت پروژه انتخاب می کنیم (“ReadWordFilesInFillTextBox”) مکان ذخیره سازی آن را معین می کنیم.
در Solution Explorer راست کلیک کرده Add New Item و پس Add Web Form را گزینش می کنیم.
دو Button، Fileuploader یک TextBox به بخش
صفحه Default.aspx اضافه می کنیم.
خصوصیت TextMode مربوط به TextBox را روی multiline قرار می دهیم.
حال سورس کد صفحه Default.aspx به صورت زیر خواهد بود:
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
<%@ Page Language=”C#” AutoEventWireup=”true” CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
هم اکنون جهت مدیریت فرایند مربوط به فایـل های Word گفته شده با راست کلیک کردن در Solution Explorer رفرنس Microsoft.Office.Interop را اضافه می کنیم. فضای نام های زیر برای کار با فایل های Word مورد نیاز است :
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text;
۱
۲
۳
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text;
هم اکنون روی دکمه upload دابل کلیک کرده کد زیر را می نویسیم:
protected void btnUpload_Click(object sender, EventArgs e)
//createting the object of application class
Application Objword = new Application();
//creating the object of document class
Document objdoc = new Document();
//get the uploaded file full path
dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
//pass the optional (missing) parameter to API
dynamic NA = System.Type.Missing;
//open Word file document
objdoc = Objword.Documents.Open
(ref FilePath, ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA
);
//creating the object of string builder class
StringBuilder sb = new StringBuilder();
for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();
if (Filedata != string.Empty)
//Append word files data to stringbuilder
sb.AppendLine(Filedata);
//closing document object
((_Document)objdoc).Close();
//Quit application object to end process
((_Application)Objword).Quit();
//assign stringbuilder object to show text in textbox
TextBox1.Text =Convert.ToString(sb);
protected void btnUpload_Click(object sender, EventArgs e)
//createting the object of application class
Application Objword = new Application();
//creating the object of document class
Document objdoc = new Document();
//get the uploaded file full path
dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
//pass the optional (missing) parameter to API
dynamic NA = System.Type.Missing;
//open Word file document
objdoc = Objword.Documents.Open
(ref FilePath, ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA
);
//creating the object of string builder class
StringBuilder sb = new StringBuilder();
for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();
if (Filedata != string.Empty)
//Append word files data to stringbuilder
sb.AppendLine(Filedata);
//closing document object
((_Document)objdoc).Close();
//Quit application object to end process
((_Application)Objword).Quit();
//assign stringbuilder object to show text in textbox
TextBox1.Text =Convert.ToString(sb);
روی دکمه reset دابل کلیک کرده کد زیر را می نویسیم:
protected void Button1_Click(object sender, EventArgs e)
TextBox1.Text =string.Empty;
۱
۲
۳
۴
protected void Button1_Click(object sender, EventArgs e)
TextBox1.Text =string.Empty;
کد مربوط به صفحه Default.aspx.cs به طور کامل به شکل زیر خواهد بود:
using System;
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text;
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnUpload_Click(object sender, EventArgs e)
//createting the object of application class
Application Objword = new Application();
//creating the object of document class
Document objdoc = new Document();
//get the uploaded file full path
dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
//pass the optional (missing) parameter to API
dynamic NA = System.Type.Missing;
//open Word file document
objdoc = Objword.Documents.Open
(ref FilePath, ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA
);
//creating the object of string builder class
StringBuilder sb = new StringBuilder();
for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();
if (Filedata != string.Empty)
//Append word files data to stringbuilder
sb.AppendLine(Filedata);
//closing document object
((_Document)objdoc).Close();
//Quit application object to end process
((_Application)Objword).Quit();
//assign stringbuilder object to show text in textbox
TextBox1.Text =Convert.ToString(sb);
protected void Button1_Click(object sender, EventArgs e)
TextBox1.Text =string.Empty;
using System;
using System.IO;
using Microsoft.Office.Interop.Word;
using System.Text;
public partial class _Default : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
protected void btnUpload_Click(object sender, EventArgs e)
//createting the object of application class
Application Objword = new Application();
//creating the object of document class
Document objdoc = new Document();
//get the uploaded file full path
dynamic FilePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
//pass the optional (missing) parameter to API
dynamic NA = System.Type.Missing;
//open Word file document
objdoc = Objword.Documents.Open
(ref FilePath, ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA, ref NA,
ref NA, ref NA, ref NA
);
//creating the object of string builder class
StringBuilder sb = new StringBuilder();
for (int Line = 0; Line < objdoc.Paragraphs.Count; Line++)
string Filedata = objdoc.Paragraphs[Line + 1].Range.Text.Trim();
if (Filedata != string.Empty)
//Append word files data to stringbuilder
sb.AppendLine(Filedata);
//closing document object
((_Document)objdoc).Close();
//Quit application object to end process
((_Application)Objword).Quit();
//assign stringbuilder object to show text in textbox
TextBox1.Text =Convert.ToString(sb);
protected void Button1_Click(object sender, EventArgs e)
TextBox1.Text =string.Empty;
چنانچه از پروگرام اجرا بگیرید، خروجی آن به شکل زیر است :
فایل Word
در UI بالا، کنترل Browse جهت گزینش فایل ها از محل فیزیکی آن ها مصرف می شود. در رویداد کلیک دکمه upload فایل Word آپلود شده خوانده در TextBox نمایش داده می شود. دکمه Clear محتوای TextBox را خالی می نماید .
هم اکنون فایـل word را گزینش کرده روی دکمه upload کلیک می کنیم و محتوای آن به صورت زیر نمایش داده می شود:
فایل Word
به این ترتیب مشاهده نمودید که چگونه می توان محتوای فایـل Word را خوانده در TextBox قرار داد.
نکته:
اعتبارسنجی مناسب برای کنترل File Upload را در وقت پیاده سازی فراهم نمایید که تحلیل نماید فایـل وجود دارد یا خیر!
خوب باشید !
آذر ۱۲, ۱۳۹۹ 0
مرداد ۲۲, ۱۳۹۹ 0
تیر ۲۳, ۱۳۹۸ 0
تیر ۲۰, ۱۳۹۸ 0