• ¡Welcome to Square Theme!
  • This news are in header template.
  • Please ignore this message.
مهمان عزیز خوش‌آمدید. ورود عضــویت


امتیاز موضوع:
  • 7 رای - 2.71 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
Title: ویرایش فایل ورد
حالت موضوعی
#1
سـلام
دوستان چطوری میشه یه فایل ورد را تو برنامه بازش کنم یه ویرایش روش انجام بدم ودوباره ببندمش؟



تو گوگل سرچ کردم چیز بدرد خوری پیدا نکردم متاسفانه
 
پاسخ
#2
درود

سمپل به زبان وی دات نت و سی شارپ


کد:
Create Edit Word Document using C# VB.NET

Here I am going to demonstrate How to Create and Edit MS Word Document using C# and VB.NET





To create or edit MS Word document, first of all we need to add two references to our project.

Microsoft.Office.Core

Microsoft.Office.Interop.Word











Creating document:



Code Behind:



C#





private void btnCreate_Click(object sender, EventArgs e)

{

try

{

//creating object for missing value

object missing = System.Reflection.Missing.Value;

//object for end of file

object endofdoc = "\\endofdoc";



//creating instance of word application

Microsoft.Office.Interop.Word._Application w = new Microsoft.Office.Interop.Word.Application();

//creating instance of word document

Microsoft.Office.Interop.Word._Document doc;

//setting status of application to visible

w.Visible = true;

//creating new document

doc = w.Documents.Add(ref missing, ref missing, ref missing, ref missing);

//adding paragraph to document

Microsoft.Office.Interop.Word.Paragraph para1;

para1 = doc.Content.Paragraphs.Add(ref missing);

object styleHeading1 = "Heading 1";

para1.Range.set_Style(ref styleHeading1);

para1.Range.Text = "Heading One";

para1.Range.Font.Bold = 1;

para1.Format.SpaceAfter = 24;

para1.Range.InsertParagraphAfter();

//creating second paragraph

Microsoft.Office.Interop.Word.Paragraph para2;

para2 = doc.Content.Paragraphs.Add(ref missing);

para2.Range.Text = "Heading OneHeading OneHeading OneHeading OneHeading OneHeading OneHeading" + '\n' + "OneHeading OneHeading OneHeading OneHeading OneHeading OneHeading One";

para2.Range.Font.Bold = 1;

para2.Format.SpaceAfter = 24;

para2.Range.InsertParagraphAfter();

}

catch (Exception ex)

{

ClientScript.RegisterStartupScript(this.GetType(), "error", "javascript:;alert('" + ex.Message + "')");

}

}





VB.NET



Private Sub btnCreate_Click(sender As Object, e As EventArgs)

Try

'creating object for missing value

Dim missing As Object = System.Reflection.Missing.Value

'object for end of file

Dim endofdoc As Object = "\endofdoc"



'creating instance of word application

Dim w As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application()

'creating instance of word document

Dim doc As Microsoft.Office.Interop.Word._Document

'setting status of application to visible

w.Visible = True

'creating new document

doc = w.Documents.Add(missing, missing, missing, missing)

'adding paragraph to document

Dim para1 As Microsoft.Office.Interop.Word.Paragraph

para1 = doc.Content.Paragraphs.Add(missing)

Dim styleHeading1 As Object = "Heading 1"

para1.Range.set_Style(styleHeading1)

para1.Range.Text = "Heading One"

para1.Range.Font.Bold = 1

para1.Format.SpaceAfter = 24

para1.Range.InsertParagraphAfter()

'creating second paragraph

Dim para2 As Microsoft.Office.Interop.Word.Paragraph

para2 = doc.Content.Paragraphs.Add(missing)

para2.Range.Text = "Heading OneHeading OneHeading OneHeading OneHeading OneHeading OneHeading" + ControlChars.Lf + "OneHeading OneHeading OneHeading OneHeading OneHeading OneHeading One"

para2.Range.Font.Bold = 1

para2.Format.SpaceAfter = 24

para2.Range.InsertParagraphAfter()

Catch ex As Exception

ClientScript.RegisterStartupScript(Me.[GetType](), "error", "javascript:;alert('" + ex.Message + "')")

End Try

End Sub





Editing document:



Code Behind:



C#



private void btnEdit_Click(object sender, EventArgs e)

{

//creating instance of word application

Microsoft.Office.Interop.Word._Application w = new Microsoft.Office.Interop.Word.Application();

object path = @"C:\table.doc";

object read = "ReadWrite";

object readOnly = false;

object o = System.Reflection.Missing.Value;

//opening document

Microsoft.Office.Interop.Word._Document oDoc = w.Documents.Open(ref path, ref o, ref readOnly, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o, ref o);



try

{

//loop for each paragraph in document

foreach (Microsoft.Office.Interop.Word.Paragraph p in oDoc.Paragraphs)

{

Microsoft.Office.Interop.Word.Range rng = p.Range;

Microsoft.Office.Interop.Word.Style styl = rng.get_Style() as Microsoft.Office.Interop.Word.Style;

//checking if document containg table

if ((bool)rng.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdWithInTable)

== true)

{

//loop for each cell in table

foreach (Microsoft.Office.Interop.Word.Cell c in rng.Cells)

{

if (rng.Cells.Count > 0)

{

//checking for desired field in table

if (c.Range.Text.ToString().Contains("ID"))

//editing values in tables.

c.Next.Range.Text = "1";

if (c.Range.Text.ToString().Contains("Name"))

c.Next.Range.Text = "Haider";

if (c.Range.Text.ToString().Contains("Address"))

c.Next.Range.Text = "Allahabad";

}

}

//saving document

oDoc.Save();

}

}

//closing document

oDoc.Close(ref o, ref o, ref o);

}

catch (Exception ex)

{

oDoc.Close(ref o, ref o, ref o);

ClientScript.RegisterStartupScript(this.GetType(), "error", "javascript:;alert('" + ex.Message + "')");

}



}





VB.NET



Private Sub btnEdit_Click(sender As Object, e As EventArgs)

'creating instance of word application

Dim w As Microsoft.Office.Interop.Word._Application = New Microsoft.Office.Interop.Word.Application()

Dim path As Object = "C:\table.doc"

Dim read As Object = "ReadWrite"

Dim [readOnly] As Object = False

Dim o As Object = System.Reflection.Missing.Value

'opening document

Dim oDoc As Microsoft.Office.Interop.Word._Document = w.Documents.Open(path, o, [readOnly], o, o, o, _

o, o, o, o, o, o, _

o, o, o, o)



Try

'loop for each paragraph in document

For Each p As Microsoft.Office.Interop.Word.Paragraph In oDoc.Paragraphs

Dim rng As Microsoft.Office.Interop.Word.Range = p.Range

Dim styl As Microsoft.Office.Interop.Word.Style = TryCast(rng.get_Style(), Microsoft.Office.Interop.Word.Style)

'checking if document containg table

If CBool(rng.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdWithInTable)) = True Then

'loop for each cell in table

For Each c As Microsoft.Office.Interop.Word.Cell In rng.Cells

If rng.Cells.Count > 0 Then

'checking for desired field in table

If c.Range.Text.ToString().Contains("ID") Then

'editing values in tables.

c.[Next].Range.Text = "1"

End If

If c.Range.Text.ToString().Contains("Name") Then

c.[Next].Range.Text = "Haider"

End If

If c.Range.Text.ToString().Contains("Address") Then

c.[Next].Range.Text = "Allahabad"

End If

End If

Next

'saving document

oDoc.Save()

End If

Next

'closing document

oDoc.Close(o, o, o)

Catch ex As Exception

oDoc.Close(o, o, o)

ClientScript.RegisterStartupScript(Me.[GetType](), "error", "javascript:;alert('" + ex.Message + "')")

End Try



End Sub
گروه دور همی پارسی کدرز
https://t.me/joinchat/GxVRww3ykLynHFsdCvb7eg
 
پاسخ
  


موضوعات مشابه ...
موضوع نویسنده پاسخ بازدید آخرین ارسال
  سورس کد ساخت فایل pdf (سی شارپ) Amin_Mansouri 3 12,081 07-01-2017، 10:05 AM
آخرین ارسال: 7seo
  سورس کد اضافه ,ویرایش,حذف,جستجو در دیتابیس(سی شارپ) Amin_Mansouri 6 24,099 12-09-2015، 08:17 AM
آخرین ارسال: Amin_Mansouri
  سورس کد دانلود فایل به زبان سی شارپ Amin_Mansouri 3 11,476 07-06-2013، 09:19 AM
آخرین ارسال: mitranasimy
  جستجو یک رشته در یک فایل متنی Ghoghnus 1 7,742 01-10-2013، 12:30 AM
آخرین ارسال: cgss
  سورس کد پاک کردن تمپ فایل اینترنت اکسپلور (سی شارپ) Amin_Mansouri 0 3,615 05-02-2012، 12:57 PM
آخرین ارسال: Amin_Mansouri
  سورس کد جست و جوی رشته در فایل پی دی اف (سی شارپ) Amin_Mansouri 0 5,324 05-02-2012، 12:45 PM
آخرین ارسال: Amin_Mansouri
  سورس کد پاک کردن فایل های داخل سطل زباله (سی شارپ) Amin_Mansouri 0 5,607 12-30-2011، 04:13 PM
آخرین ارسال: Amin_Mansouri
  بدست اوردن حجم فایل و تعداد فایل های پاک شده سطل زباله سیستم عامل (سی شارپ) Amin_Mansouri 0 5,253 12-30-2011، 03:59 PM
آخرین ارسال: Amin_Mansouri
  سورس پاک کردن فایل بصورتی که ریکاوری نشه Amin_Mansouri 0 3,567 11-27-2011، 01:37 PM
آخرین ارسال: Amin_Mansouri
  نوشتن اطلاعات یک جدول به صورت فایل xml در یک فایل xml Ghoghnus 0 3,262 10-31-2011، 03:23 PM
آخرین ارسال: Ghoghnus

پرش به انجمن:


Browsing: 1 مهمان