آموزش ساخت فایل ورد و باز کردن آن در مایکروسافت ورد در سی شارپ
سلام دوستان عزیز . در این تاپیک ،آموزش ساخت فایل ورد و باز کردن آن در مایکروسافت ورد در سی شارپ را برای شما آماده کردم.انشااله که این آموزش سی شارپ به دردتون می خوره.
خب روی پروژه تون راست کلیک بکنین و Add Reference رو بزنین و این اسمبلی ها رو اضافه کنین:
Microsoft.Office.Core
Microsoft.Vbe.Interop
Microsoft.Office.Interop.Word
Microsoft.Vbe.Interop
Microsoft.Office.Interop.Word
حالا میرسیم به کد برنامه:
using Word = Microsoft.Office.Interop.Word;
private static object n = Type.Missing;
static void Main(string[] args)
{
// Start Word in the background.
Word.ApplicationClass app = new Word.ApplicationClass();
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
// Create a new document (this is not visible to the user).
Word.Document doc = app.Documents.Add(ref n, ref n, ref n,
ref n);
// Add a heading and two lines of text.
Word.Range range = doc.Paragraphs.Add(ref n).Range;
range.InsertBefore("Test Document");
string style = "Heading 1";
object objStyle = style;
range.set_Style(ref objStyle);
range = doc.Paragraphs.Add(ref n).Range;
range.InsertBefore("Line one.\nLine two.");
range.Font.Bold = 1;
// Show a print preview, and make Word visible.
doc.PrintPreview();
app.Visible = true;
}
private static object n = Type.Missing;
static void Main(string[] args)
{
// Start Word in the background.
Word.ApplicationClass app = new Word.ApplicationClass();
app.DisplayAlerts = Word.WdAlertLevel.wdAlertsNone;
// Create a new document (this is not visible to the user).
Word.Document doc = app.Documents.Add(ref n, ref n, ref n,
ref n);
// Add a heading and two lines of text.
Word.Range range = doc.Paragraphs.Add(ref n).Range;
range.InsertBefore("Test Document");
string style = "Heading 1";
object objStyle = style;
range.set_Style(ref objStyle);
range = doc.Paragraphs.Add(ref n).Range;
range.InsertBefore("Line one.\nLine two.");
range.Font.Bold = 1;
// Show a print preview, and make Word visible.
doc.PrintPreview();
app.Visible = true;
}
در پناه حق پیروز و موفق باشید