Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate Word in C #

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "C#how to operate Word". In daily operation, I believe many people have doubts about how to operate Word in C#. Xiaobian consulted all kinds of materials and sorted out simple and easy operation methods. I hope to help you answer the doubts about "C#how to operate Word"! Next, please follow the small series to learn together!

C#operation Word learning examples: first introduce class libraries, Microsoft.Office.Interop.Word, and then programming. The code is as follows:

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using Microsoft.Office.Interop.Word; namespace WordTest { public partial class Form1 : Form { object strFileName; Object Nothing; Microsoft.Office.Interop.Word.Application myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); Document myWordDoc; string strContent = ""; public Form1() { InitializeComponent(); } //C#operation Word learning example analysis private void button1_Click(object sender, EventArgs e) { createWord(); //openWord(); } private void createWord() { strFileName = System.Windows.Forms.Application.StartupPath + "test.doc"; if (System.IO.File.Exists((string)strFileName)) System.IO.File.Delete((string)strFileName); Object Nothing = System.Reflection.Missing.Value; myWordDoc = myWordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing); #region writes the data read from the database to the word file strContent = "Hello nnr"; myWordDoc.Paragraphs.Last.Range.Text = strContent; strContent = "This is a test program"; myWordDoc.Paragraphs.Last.Range.Text = strContent; //C#Operation Word Learning Example Analysis #endregion //Save the contents of the WordDoc document object as DOC document myWordDoc.SaveAs(ref strFileName, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //Close WordDoc Document Object myWordDoc.Close(ref Nothing, ref Nothing, ref Nothing); //Close WordApp Component Object myWordApp.Quit(ref Nothing, ref Nothing, ref Nothing); this.richTextBox1.Text = strFileName + "rn" + "Created successfully"; } private void openWord() { The requested URL//fontDialog () was not found on this server. System.Drawing.Font font = fontDialog1.Font; object filepath = "D:asp.docx"; object oMissing = System.Reflection.Missing.Value; myWordDoc = myWordApp.Documents.Open(ref filepath, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); myWordDoc.Content.Font.Size = font.Size; myWordDoc.Content.Font.Name = font.Name; myWordDoc.Save(); richTextBox1.Text = myWordDoc.Content.Text; //C#Operation Word Learning Example Analysis myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing); myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing); } } At this point, the study of "C#how to operate Word" is over, hoping to solve everyone's doubts. Theory and practice can better match to help everyone learn, go and try it! If you want to continue learning more relevant knowledge, please continue to pay attention to the website, Xiaobian will continue to strive to bring more practical articles for everyone!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report