C# world模板中写信息

            看到有博客园朋友需要往world中写内容相关代码,我就简单写一下。其实往world中写内容分两种情况:纯文本内容、二进制内容(图片),难点在厚着。主要用到ApplicationClass 、Document操作类,

以下是我上网查询和自己总结的一个WordHelper共同类,里面有相关说明。因时间问题就不多说了,如果写的有不对地方,希望大家指正:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Security;
using Microsoft.Office.Interop.Word;
using System.IO;
using System.Data;
using System.Text.RegularExpressions;
using Open.ItemBank.BusinessEntities;
using Open.ItemBank.BusinessLogic;
using Open.Framework;
using System.Web;
namespace Open.ItemBank.WebBase
{
    public class WordHelper
    {
        #region 初始声明和构造函数
        private ApplicationClass WordApp;
        private Document WordDoc;
        private Object oMissing = System.Reflection.Missing.Value;
        private static bool isOpened = false;//判断word模版是否被占用

        public WordHelper()
        {
           //
           // TODO: 在此处添加构造函数逻辑
           //
        }
         #endregion

        #region World对象操作
        public void SaveAs(string strFileName,bool isReplace)
       {
            if (isReplace && File.Exists(strFileName))
            {
                File.Delete(strFileName);
            }
            object missing = Type.Missing;
            object fileName = strFileName;
            WordDoc.SaveAs(ref fileName, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
             ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
        }
        
        /// <summary>
        /// 定义一个Word.Application 对象
        /// </summary>
        public void activeWordApp()
        {
            WordApp = new ApplicationClass();
        }

        /// <summary>
        /// 终止内容
        /// </summary>
        public void Quit()
        {
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application app = WordApp.Application;
            ((_Application)app).Quit(ref missing, ref missing, ref missing);
            isOpened = false;
        }

        /// <summary>
        /// 关闭进程
        /// </summary>
        public void Close()
        {
            ((_Document)WordDoc).Close(ref oMissing, ref oMissing, ref oMissing);
        }

        //基于模版新建Word文件
        public void OpenTempelte(string strTemppath)
        {
            object Missing = Type.Missing;
            activeWordApp();
            WordApp.Visible = false;
            object oTemplate = (object)strTemppath;
            try
            {
                while (isOpened)
                {
                    System.Threading.Thread.Sleep(500);
                }
                WordDoc = WordApp.Documents.Add(ref oTemplate, ref Missing, ref Missing, ref Missing);
                isOpened = true;
                WordDoc.Activate();
            }
            catch (Exception Ex)
            {
                Quit();
                isOpened = false;
                throw new Exception(Ex.Message);
            }
        }

        /// <summary>
        /// 往对应有标签内填充填充内容
        /// </summary>
        /// <param name="LabelId"></param>
        /// <param name="Content"></param>
        public void FillLable(string LabelId,string Content)
        {
            //打开Word模版
            object bkmC = LabelId;
            if (WordApp.ActiveDocument.Bookmarks.Exists(LabelId) == true)
            {
                WordApp.ActiveDocument.Bookmarks.get_Item(ref bkmC).Select();
            }
            WordApp.Selection.TypeText(Content);
        }


        /// <summary>
        /// 往当前行的最后追加内容
        /// </summary>
        /// <param name="LabelId"></param>
        /// <param name="Content"></param>
        public void FillLable(string Content)
        {
            //向对应的标签后追加内容
            WordApp.Selection.TypeText(Content);
        }

        /// <summary>
        /// 换行
        /// </summary>
        /// <param name="currentSelection"></param>
        public void TryParagraph(Selection currentSelection)
        {
            currentSelection.TypeParagraph();//换行
        }
        #endregion

        #region  World位置移动方法
        /// <summary>
        /// 将当前光标定位到当前行的最后一个数字
        /// </summary>
        public void FillSelection()
        {
            Selection currentSelection = WordApp.Selection;
            object a = currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);//得到当前行号
            int oldLine = 0;
            while (oldLine != int.Parse(a.ToString()))//一直按右键,直到光标不再往下了为止
            {
                oldLine++;
                moveRight(currentSelection, WordApp);
                a = currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);
            }
        }

        /// <summary>
        /// 将光标后移一个数字
        /// </summary>
        private void moveRight(Selection currentSelection, Microsoft.Office.Interop.Word.Application WordApp)
        {
            if (currentSelection == null || currentSelection != WordApp.Selection)
                currentSelection = WordApp.Selection;
            object dummy = System.Reflection.Missing.Value;
            object count = 1;
            object Unit = Microsoft.Office.Interop.Word.WdUnits.wdCharacter;
            currentSelection.MoveRight(ref Unit, ref count, ref dummy);
        }


        /// <summary>
        /// 定位到第一个字符
        /// </summary>
        private void gotoFirstCharacter()
        {
            Selection selection = WordApp.Selection;
            if (selection == null || selection != WordApp.Selection)
                selection = WordApp.Selection;
            int oldLine = 0;
            gotoAbsolutLine(1);
            object a = selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);//得到当前行号
            while (oldLine != int.Parse(a.ToString()))//一直按右键,直到光标不再往下了为止
            {
                oldLine++;
                moveRight(selection, WordApp);
                a = selection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);
            }
            gotoAbsolutLine(int.Parse(a.ToString()));
        }

        /// <summary>
        /// 根据行号定位到具体行
        /// </summary>
        /// <param name="lineNum"></param>
        private void gotoAbsolutLine(int lineNum)
        {
            Selection selection = WordApp.Selection;
            if(selection == null || selection != WordApp.Selection)
                selection = WordApp.Selection;
            object dummy = System.Reflection.Missing.Value;
            object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToLine;
            object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToFirst;
            object count = lineNum;
            selection.GoTo(ref what, ref which, ref count, ref dummy);
        }


        /// <summary>
        /// 取得行、列、页信息
        /// </summary>
        /// <param name="row_cout">行</param>
        /// <param name="cell_count">列</param>
        /// <param name="page_count">页</param>
        public void WordGetRCP(ref object row_cout, ref object cell_count, ref object page_count)
        {
            Selection currentSelection = WordApp.Selection;  
            row_cout=currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);      //a.ToString()行
            cell_count=currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterColumnNumber);   //b.ToString()列
            page_count=currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber);  //c.ToString()页
        }

        #endregion

        #region   文件模板创建、下载、删除
        /// <summary>
        /// 根据模板创建文件
        /// </summary>
        /// <returns></returns>
        public string CreateTemplate()
        {
            string mFileTimes = string.Empty;
            try
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("~/Template");
                mFileTimes = string.Format("{0}", Convert.ToDateTime(DateTime.Now).ToString("yyyymmddhhmmss"));
                string templatePath = string.Format("{0}/Template.docx", path);
                string mStrFileRoot = string.Format("{0}/{1}savetest.docx", path, mFileTimes);
                if (File.Exists(mStrFileRoot))
                    DeletTemplate(mStrFileRoot);
                File.Copy(templatePath, mStrFileRoot);
                mFileTimes = string.Format("{0}savetest.docx", mFileTimes);
            }
            catch (Exception)
            {
                throw;
            }
            return mFileTimes;
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="mStrFileRoot"></param>
        public void DeletTemplate(string mStrFileName)
        {
            try
            {
                string path = System.Web.HttpContext.Current.Server.MapPath("~/Template");
                string mStrFileRoot = string.Format("{0}\\{1}", path, mStrFileName);
                if (File.Exists(mStrFileRoot))
                     File.Delete(mStrFileRoot);
            }
            catch (Exception)
            {
                throw;
            }
        }

        /// <summary>
        /// 下载文件
        /// </summary>
        public void LoadPaperTemplate(string mStrFileName)
        {
            FileStream fs = null;
            BinaryReader br = null;
            BinaryWriter brnew = null;
            try
            {
                //给内容赋值   
                string path = System.Web.HttpContext.Current.Server.MapPath("~/Template");
                string mStrFileRoot = string.Format("{0}\\{1}", path, mStrFileName);
                if (File.Exists(mStrFileRoot))
                {
                    fs = new System.IO.FileStream(mStrFileRoot, System.IO.FileMode.Open);
                    br = new BinaryReader((Stream)fs);
                    byte[] bytes = br.ReadBytes((Int32)fs.Length);
                    brnew = new BinaryWriter(fs);
                    brnew.Write(bytes, 0, bytes.Length);
                    System.Web.HttpContext.Current.Response.Clear();
                    System.Web.HttpContext.Current.Response.Buffer = true;
                    System.Web.HttpContext.Current.Response.Charset = "GB2312";
                    System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;  filename=" + HttpUtility.UrlEncode(mStrFileRoot.Substring(mStrFileRoot.LastIndexOf('\\') + 1)));
                    System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");
                    System.Web.HttpContext.Current.Response.ContentType = "application/ms-word";
                    System.Web.HttpContext.Current.Response.BinaryWrite(bytes);
                    System.Web.HttpContext.Current.Response.Flush();
                    System.Web.HttpContext.Current.Response.End();
                }
            }
            catch (Exception)
            {
                //throw;
            }
            finally
            {
                br.Close();
                brnew.Close();
                fs.Close();
            }
        }
        #endregion

        #region  扩展操作
        /// <summary>
        ///导出对应试卷完整信息
        /// </summary>
        /// <param name="mStrPaperId">试卷编号</param>
        /// <param name="file">保存文件</param>
        /// <param name="fileTemplate">模板文件</param>
        public void WriteDoc(string mStrPaperId)
        {
            string mStrFileName = string.Empty;   //文件名称
            try
            {
                #region 获取新的模板对象
                mStrFileName = CreateTemplate();
                string fileTemplate = "~/Template/Template.docx";
                string file = string.Format("~/Template/{0}", mStrFileName);
                #endregion

                #region  初始声明文档操作对象
                object strFileName = System.Web.HttpContext.Current.Server.MapPath(file);
                object fileName = System.Web.HttpContext.Current.Server.MapPath(fileTemplate);
                OpenTempelte(fileName.ToString());
                //WordApp.Visible = true; 用于调试显示模板信息
                #endregion

                #region  获取对应模板,并将试题填充到对应文档中
                ///试卷模板中包含一个标签"content" 
                ///"content" 试卷的内容部分 
                ///**获取对应试卷的试题信息
                DraftPaper draftPaper = new DraftPaper();
                draftPaper = (new DraftPaperLogic()).GetById(DataAdpater.ToInt(mStrPaperId));
                foreach (Bookmark bm in WordDoc.Bookmarks)
                {
                    if (bm.Name == "title") //对应写试卷名称
                    {
                        bm.Select();
                        Selection currentSelections = WordApp.Selection;
                        //设置当前行对应的字体样式
                        currentSelections.Range.Font.Name = "新宋体";
                        currentSelections.Range.Font.Bold = 1;
                        currentSelections.Range.Font.Size = 18;
                        currentSelections.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;//设置居中对齐 
                        //@@得到光标的位置
                        currentSelections.TypeText(draftPaper.PaperName);
                        TryParagraph(currentSelections);//换行
                    }
                    if (bm.Name == "content")//获取当前标签对象是否正确
                    {
                        //将光标定位到书签内。 
                        bm.Select();
                        //获取当前光标的位置。 
                        Selection currentSelection = WordApp.Selection;
                        //设置当前行对应的字体样式
                        currentSelection.Range.Font.Name = "新宋体";
                        currentSelection.Range.Font.Bold = 1;
                        currentSelection.Range.Font.Size = 16;
                        currentSelection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//设置左对齐 
                        ///**获取该试卷对应大题信息
                        List<DraftPaperSection> draftPaperSectionList = (new DraftPaperSectionLogic()).GetListAllByDraftPaperId(DataAdpater.ToInt(mStrPaperId));
                        foreach (DraftPaperSection DpSection in draftPaperSectionList)
                        {
                            //@@填充对应大题名称
                            currentSelection.TypeText(string.Format("{0} {1}", DpSection.SectionIndex, DpSection.Title));
                            currentSelection.TypeParagraph();//换行
                            ///**获取该试卷对应大题下的所有试题关系集合
                            List<DraftPaperQuestionRef> draftPaperQuestionRefList = (new DraftPaperQuestionRefLogic()).GetAllDraftPaperQuestionRefById(DpSection.DraftPaperSectionId, DpSection.DraftPaperId);
                            DraftQuestion _dQ = null;
                            foreach (DraftPaperQuestionRef DPQRef in draftPaperQuestionRefList)
                            {
                                ///** 获取该试卷对应的试题
                                _dQ = (new DraftQuestionLogic()).GetById(DPQRef.DraftItemId);
                                if (_dQ != null)
                                {
                                    //@@填充对应试题名称
                                    currentSelection.TypeText(DPQRef.ItemIndex.ToString()+"、");
                                    //TryParagraph(currentSelection);//换行
                                    ///** 获取该试题内容信息
                                    DraftQuestionContent draftQuestionContent = (new DraftQuestionContentLogic()).GetById(_dQ.DraftQuestionId, _dQ.VersionId);
                                    string questionContentText = string.Empty;
                                    List<string> questionImages = null;
                                    (new DraftQuestionLogic()).ContentImageAnalysis(draftQuestionContent, out questionContentText, out questionImages);
                                    int materialPoint = 0;  //用于记录材料中图片所占的个数
                                    int index = 0;         //用于取出对应材料图片信息的索引
                                    //获取对应的内容和材料数组
                                    string[] mStrDraftQuestionContens = questionContentText.Split('□');
                                    //判断是否含有阅读材料信息
                                    if (mStrDraftQuestionContens != null && !string.IsNullOrEmpty(mStrDraftQuestionContens[0]))
                                    {
                                        WriteDocImages(mStrDraftQuestionContens[0], currentSelection, questionImages, index, out materialPoint, WordDoc);
                                        TryParagraph(currentSelection);//换行
                                    }
                                    index = materialPoint;  //重新将材料信息赋值
                                    //填充对应试题内容信息
                                    if (mStrDraftQuestionContens != null && !string.IsNullOrEmpty(mStrDraftQuestionContens[1]))
                                    {
                                        WriteDocImages(mStrDraftQuestionContens[1], currentSelection, questionImages, index, out materialPoint, WordDoc);
                                        TryParagraph(currentSelection);//换行
                                    }
                                }
                            }
                        }
                    }
                }
                #endregion

                #region 将Word文档保存并结束进程
                SaveAs(strFileName.ToString(), false);
                Close();
                Quit();
                #endregion

                #region 下载文件内容信息
                LoadPaperTemplate(mStrFileName);
                #endregion
            }
            catch (Exception)
            {
                throw;
                #region 结束进程
                Close();
                Quit();
                #endregion
            }
            finally
            {
                #region 删除文件内容信息
                DeletTemplate(mStrFileName);
                #endregion
            }
        }

        /// <summary>
        /// 获取填充对应图片信息
        /// </summary>
        /// <param name="mStrDraftQuestionContens">对应填充试题内容信息</param>
        /// <param name="currentSelection">文本插入对象</param>
        /// <param name="questionImages">图片集合信息</param>
        /// <param name="index">取图片的初始索引</param>
        /// <param name="materialPoint"></param>
        public void WriteDocImages(string mStrDraftQuestionContens, Selection currentSelection, List<string> questionImages, int index, out int materialPoint, Document WordDoc)
        {
            materialPoint = 0;//初始给材料赋值
            if (!mStrDraftQuestionContens.Contains('■'))
            {
                //@@填充对应试题的材料信息
                mStrDraftQuestionContens = mStrDraftQuestionContens.Replace("\\r", "");
                mStrDraftQuestionContens = mStrDraftQuestionContens.Replace("\\n", "");
                currentSelection.TypeText(mStrDraftQuestionContens);
            }
            else
            {
                string[] mStrDQMaterial = mStrDraftQuestionContens.Split('■');
                materialPoint = mStrDQMaterial.Length - 1;//得到内容的图片索引开始位置
                foreach (string mStrMaterial in mStrDQMaterial)
                {
                    ///** 写对应试题材料信息
                    currentSelection.TypeText(mStrMaterial);
                    if (materialPoint > 0)
                    {
                        Regex reg  =new Regex(@"(?i)<IMG[^>]*?\ssrc\s*=\s*(['""]?)(?<src>[^'""\s>]+)\1[^>]*>");
                        string mStrImgMaterial = string.Empty; //过滤后的图片信息
                        MatchCollection mc = reg.Matches(questionImages[index]);
                        foreach (Match m in mc)
                        {
                            mStrImgMaterial=m.Groups["src"].Value ;
                            break;
                        }
                        string FileName = System.Web.HttpContext.Current.Server.MapPath(mStrImgMaterial.Contains("..") ? ("~" + mStrImgMaterial.Substring(2)) : ("~" + mStrImgMaterial));//图片所在路径 

                        //object LinkToFile = false;
                        //object SaveWithDocument = true;
                        //object Anchor = WordDoc.Application.Selection.Range;
                        //InlineShape li = currentSelection.InlineShapes.AddPicture(imgPath, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                        //li.Width = 20f;//图片宽度
                        //li.Height = 20f;//图片高度
                        //Shape s = li.ConvertToShape();
                        //插入图片
                        if (File.Exists(FileName))
                        {
                            //string FileName = System.Web.HttpContext.Current.Server.MapPath("~/Attachments/DraftQuestionContent/20120308102235.jpg");//图片所在路径
                            object LinkToFile = false;
                            object SaveWithDocument = true;
                            object Anchor = WordDoc.Application.Selection.Range;
                            WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                            //WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 40f;//图片宽度
                            //WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 40f;//图片高度
                        }
                        FillSelection();
                        //将图片设置为四周环绕型
                        //Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                        //s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;
                        //WordDoc.Application.ActiveDocument.InlineShapes[1].Width = 100f;//图片宽度 
                        //将图片设置为四周环绕型 
                        materialPoint--;
                    }
                    index++;
                }
            }
        }

        /// <summary>
        /// 参考操作
        /// </summary>
        /// <returns></returns>
        public string CreateWordFile()
        {
            string message = "";
            try
            {
                Object oMissing = System.Reflection.Missing.Value;
                string dir = System.Web.HttpContext.Current.Server.MapPath("");//首先在类库添加using System.web的引用
                if (!Directory.Exists(dir + "\\file"))
                {
                    Directory.CreateDirectory(dir + "\\file");  //创建文件所在目录
                }
                string name = DateTime.Now.ToLongDateString() + ".doc";
                object filename = dir + "\\file\\" + name;  //文件保存路径

                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                WordApp.Visible = true;
                object unit;
                object count; //移动数
                unit = Microsoft.Office.Interop.Word.WdUnits.wdCell;
                Selection currentSelection = WordApp.Selection;
                count = 1;
                currentSelection.TypeText("11111:");
                //object c = currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdActiveEndAdjustedPageNumber);
                WordApp.Selection.Move(ref unit, ref count);
                //插入图片
                if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/Attachments/DraftQuestionContent/20120308102235.jpg")))
                {
                    string FileName = System.Web.HttpContext.Current.Server.MapPath("~/Attachments/DraftQuestionContent/20120308102235.jpg");//图片所在路径
                    object LinkToFile = false;
                    object SaveWithDocument = true;
                    object Anchor = currentSelection.Range;
                    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                }

                object a = currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);//得到当前行号
                int oldLine = 0;
                while (oldLine != int.Parse(a.ToString()))//一直按右键,直到光标不再往下了为止
                {
                    oldLine++;
                    moveRight(currentSelection, WordApp);
                    a = currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);
                }


                //将图片设置为四周环绕型
                // Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                // s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapInline;
                WordApp.Selection.Move(ref unit, ref count);
                currentSelection.TypeText("yangzhichaojiuasdfasdkf;ajsldkjfa;lskdjfa;sdkjfa;sdkfalsdfka;sldfkjasldkfja;sldkfjshiwojiu");
                //插入图片
                if (File.Exists(System.Web.HttpContext.Current.Server.MapPath("~/Attachments/DraftQuestionContent/20120308102235.jpg")))
                {
                    string FileName = System.Web.HttpContext.Current.Server.MapPath("~/Attachments/DraftQuestionContent/20120308102235.jpg");//图片所在路径
                    object LinkToFile = false;
                    object SaveWithDocument = true;
                    object Anchor = currentSelection.Range;
                    WordDoc.Application.ActiveDocument.InlineShapes.AddPicture(FileName, ref LinkToFile, ref SaveWithDocument, ref Anchor);
                }
                oldLine = 0;
                a = currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);//得到当前行号
                while (oldLine != int.Parse(a.ToString()))//一直按右键,直到光标不再往下了为止
                {
                    oldLine++;
                    moveRight(currentSelection, WordApp);
                    a = currentSelection.get_Information(Microsoft.Office.Interop.Word.WdInformation.wdFirstCharacterLineNumber);
                }

                //文件保存
                WordDoc.SaveAs(ref filename, 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);
                WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
                WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
                message = name + "文档生成成功";
            }
            catch
            {
            }
            return message;
        }
       #endregion
   }
}

  

           有不明白地方可以给我留言,我会及时给大家回复,希望对大家有所帮助!!!

转载于:https://www.cnblogs.com/BeyondWJsel/archive/2012/06/28/2567134.html

1.C#在Console、Windows Forms模板下编写Hello World。 C#有多种模板,现在以Console、WPF、Windows Forms三种模板为例,介绍各模板下Hello World写法。 阅读详情

相关推荐

C# gRPC 示例项目:Hello World 入门指南

gRPC作为一种现代RPC框架,特别适合于微服务架构中不同服务之间的通信。它使用HTTP/2作为传输层协议,这意味着gRPC能够利用HTTP/2提供的多路复用、头部压缩以及服务器推送等特性,大幅度提高通信效率。gRPC服务以定义良好的接口进行通信,这些接口使用一种名为Protocol Buffers的二进制序列化协议进行定义。这种协议不仅小巧高效,而且语言无关,支持跨语言的服务调用。

weixin_35752645的博客 1100

C# 批量修改Word模板

1制作Word模板将需药批量修改的字段加上占位符 2通过程序控制 批量修改占位符的字符 达到批量处理目的 修改Excel文档 修改列头与Word模板占位符保持 一直 且不重复 运行程序导入考生名单,批量生成Word文档 ...

danxiaogui0513的专栏 845

F5高可用HA配置详情

F5 HA详细配置

weixin_45843944的博客 3032

C#作业miniword

基于C#编写的miniword,C#程序设计实践,C#作业

影小薯的博客 1302

如何在OBS中添加实时时间戳:Lua脚本配置教程(附效果展示)

本文详细介绍了如何在OBS中通过Lua脚本配置实时时间戳功能,包括环境准备、脚本获取、安装配置及高级定制。适用于直播和视频录制场景,提供精准回溯和风格自定义,支持多平台适配和性能优化,是专业制作的必备技巧。

day7的博客 1049

S7-1200PLC 与温控仪表Modbus RTU通信

硬件:cpu 1214C 目的:通过Modbus RTU协议,实现对温控仪表的控制 软件:Portal V14SP1

weixin_43922025的博客 3万+

c# 操作word get_information

expression.information(type) expression 必需。该表达式返回一个 range 或 selection 对象。 type long 类型,必需。需要返回的信息。可取下列 wdinformation 常量之一: wdactiveendadjustedpagenumber 返回页码,在该页中包含指定的所选内容或区域的活动结尾。如果设置了一个起...

weixin_30835933的博客 1646

EDA课程实验:《数字逻辑电路》实验四___基于Minisys 平台 VIVADO工具与Verilog语言

(4)使用 Verilog HDL 实现一个分频器 clock_div,输入的是系统提供的 100MHz 频率,输出的是 1Hz 频率,占空比是 1:2,(实际上可以用一个加 1 计数器来实现降频,输入的是一个 100MHz 频率的方波,对这 个方波计数 1 亿次刚好到 1 秒(1Hz),题目要求输出频率占空比是 1:2,也就是这个 1Hz 的 方波高电平和低电平各 500ms,可以采用计数到中间值的时候输出电平翻转。(1)1) 拨动 SW1~0 为 01,按下 P4 按钮,GLD1~0 为 01;

ssjy_的博客 1303

WdInformation 枚举

指定返回的涉及指定所选内容或区域的信息的类型。 字段 字段 wdActiveEndAdjustedPageNumber 1 返回包含指定的所选内容或区域的活动结尾的页面数。如果设置一个起始页码或进行其他手动调整,则返回经调整的页码(与wdActiveEndPageNumber不同)。 wdActiveEndPageNumber 3 返回包含指定的选定内容或范围,从文档的开头开始计数的活动端的页面数。对页码编号的任何手动调整都将被忽略(与计数不...

lvmingzhou的专栏 733

【ComfyUI】QwenImageEdit + Z-Image 面部融合图生图

本文介绍了一种基于Stable Video Diffusion(SVD)模型的ComfyUI图生视频工作流,能够将静态图片转换为动态视频。工作流包含模型加载、图像导入、条件生成、视频采样、解码合成等8个核心步骤,通过SVD_img2vid_Conditioning节点实现视觉语义转换。该方案适用于创意视频制作、产品演示等场景,支持设计师和AI爱好者快速实现"以图生动"效果。文中详细说明了工作流架构、关键节点功能、操作流程及应用案例,并提供了模型参数和使用注意事项。

Mr数据杨 1834

非洲秃鹫优化算法(AVOA)详解:从原理到实战

非洲秃鹫优化算法(AVOA)是一种模拟秃鹫觅食行为的元启发式算法。该算法通过数学建模秃鹫的群体分工、饥饿感知和食物竞争等行为,将优化问题转化为智能觅食过程。AVOA包含四个关键阶段:(1)种群初始化与最佳秃鹫选择;(2)饥饿率控制探索与开发平衡;(3)探索阶段模拟远距离飞行搜索;(4)开发阶段模拟食物源附近的竞争行为。算法采用饥饿率函数实现自适应搜索,初期偏向全局探索,后期转向局部开发。实验表明AVOA在标准测试函数和工程设计问题上表现出优越性能,其创新性在于将秃鹫独特的生物学特性转化为高效的优化机制。

qq_42212808的博客 231

C#获取目录中word问文件列表

1.c#获取目录中word问文件列表的源码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; using System.IO; using

biyusr的专栏 589

DBeaver配置本地mysql-connector驱动文件

本文摘要:介绍了数据库驱动管理的基本操作步骤:1)新建驱动管理器,填写MySQL驱动名称、类名和类型;2)添加本地MySQL驱动文件;3)创建数据库连接并测试。适用于MySQL5.x和8.x版本,帮助用户快速配置数据库连接。

litlit023的专栏 1714

C# 实现 “Hello World” 教程

C# 作为一种广泛应用于.NET 开发的编程语言,以其简洁、高效和类型安全等特性,深受开发者喜爱。在踏入 C# 编程领域时,编写经典的 “Hello World” 程序是重要的起点,它能帮助我们快速熟悉 C# 的开发流程与基本语法。下面,就让我们一同开启 C# 的 “Hello World” 之旅。

zsnandzmn的博客 1345

i.MX6ULL驱动开发 | 31 - Linux内核网络设备驱动框架

net_device结构体在内核中指代一个网络设备,定义在,是一个非常大的结构体,包含网络设备的属性描述和操作接口,其中一些关键的成员如下。Linux在轮询处理机制和中断处理机制的基础上,提出了一种高效的网络处理技术NAPI。NAPI的核心思想是不全部采用中断来读取网络数据,而是采用中断来唤醒数据接收服务程序,在接收服务程序中采用poll的方法来轮询处理数据。提高短数据包的接收效率,减少中断处理的时间。...

Mculover666的博客(嵌入式) 1107

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

C#输出Hello world!

C# C#是一个面向对象的编程语言。 在这里我使用的工具是:Microsoft Visual Studio 2010 一、首先我们新建一个项目   二、然后选择Visual C#下的Windows的控制台应用程序        设置好位置之后,点击右下角的确定。   三、此时会生成一个c#小程序的大体        我们需要加一点内容。   四、输出 “Hello...

A_mmmm的博客 1万+

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

C# Hello World 实例

C#程序结构 在我们学习 C# 编程语言的基础构件块之前,让我们先看一下 C# 的最小的程序结构,以便作为接下来章节的参考。 C# Hello World 实例 一个 C# 程序主要包括以下部分: 命名空间声明(Namespace declaration) 一个 class Class 方法 Class 属性 一个 Main 方法 语句(Statements)&am...

weixin_30587927的博客 619

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

用VS2013的C#写hello world

(1)C# 即C Sharp,C#编程语言基于微软.net Framework框架下开发。net Framework是一个平台,也是一个技术。Visual studio(简称VS)是一个集成化开发平台环境,也叫IDE工具。 (2) a.新建项目,如图“文件 -> 新建 -> 项目” b.选择语言,如Visual C#;选择创建的应用程序类型;如WPF应用程序或者控制台应用程序,这里选择...

RichardoMu的博客 673

物联网远程监测系统设计:STM32+WIFI+GPS+震动传感器整合实践

物联网远程监测系统通过传感器网络实时采集环境数据,利用无线通信技术将数据传输至云端平台。其技术原理基于嵌入式系统架构,结合多种传感器模块实现数据感知,通过WIFI或移动网络完成数据传输,最终在云平台进行存储与分析。这类系统在工业设备监控、车辆追踪、环境监测等领域具有重要价值,能够实现设备状态的实时感知和预警功能。在实际应用中,STM32微控制器常作为核心处理器,配合GPS模块提供精准定位,震动传感器检测设备振动状态。本文以WIFI+GPS+震动监测为具体案例,详细解析多传感器系统的硬件选型、数据采集策略和云

weixin_34060299的博客 879

打开C# 大门:Hallo, World!

本文算是了解了 C# 的基础知识,所有语言都从Hallo World,其实大致语言都差不多的,后续可以继续学习更多高级概念和技术。C# 是一门功能强大的编程语言,可以用于创建各种类型的应用程序,如桌面应用、Web 应用、移动应用等。

qi_ming88的博客 888

[C#] 第一个Hello World程序

在这一节中,我们通过Microsoft Visual Studio 2005以及AutoCAD Managed C# Project Application Wizard来创建一个.NET程序。首先,启动Microsoft Visual Studio 2005,选择“文件>新建>项目”,在新建项目对话中选择项目类型为“Visual C#”,在模板中选择“AutoCAD Managed C# P

cadguider的专栏 2718

黄聪:C#代码生成工具:文本模板初体验 Hello,World!

C#代码生成工具:文本模板初体验 Hello World 在VS中的文本模板(也称T4),它给我有点CodeSmith的感觉,也是通过模板加逻辑代码混编批量生成代码的方法,但是关于T4的资料不多,而且对T4模板编写代码的时候智能感知也没有,不过还是介绍给一些可能用到它的朋友吧,下面就给几个简单的示例体验一下吧,当然要先从最简单的Hello,World!开始啦~ 一. Hello,World...

weixin_33877092的博客 227

(转)C#代码生成工具:文本模板初体验 Hello World

原文地址:http://www.cnblogs.com/huangcong/archive/2011/01/08/1930553.html C#代码生成工具:文本模板初体验 Hello World 在VS中的文本模板(也称T4),它给我有点CodeSmith的感觉,也是通过模板加逻辑代码混编批量生成代码的方法,但是关于T4的资料不多,而且对T4模板编写代码的时候智能感知也没有,不过...

weixin_30260399的博客 115

C#入门:在JetBrains Rider中创建.Net Framework控制台应用程序,输出“Hello, World!”

本篇博客将引导您使用JetBrains Rider这一功能强大且用户友好的集成开发环境(IDE),基于传统的.NET Framework来构建并运行您的第一个C#控制台应用程序。通过以上步骤,您已成功利用JetBrains Rider和.NET Framework创建了一个基础的C#控制台应用程序,完成了输出“Hello, World!当程序运行时,一个新的控制台窗口将会打开,并显示“Hello, World!输入你的项目名称,比如 “HelloWorldApp”,并指定项目的存储路径。

一起coding,一起嗨。 6148

C#入门第一步:从零搭建环境到运行Hello World程序

编程语言是人与计算机沟通的桥梁,通过特定的语法规则将逻辑转化为可执行的指令。C#作为一种面向对象的现代编程语言,其设计兼顾了表达力与运行效率,在.NET运行时环境中编译执行。对于开发者而言,掌握一门新语言的首要技术价值在于快速验证开发环境与理解基础编程范式,这为后续构建复杂应用奠定坚实基础。在众多应用场景中,环境配置、基础语法实践和简单程序调试是初学者必经之路。本文聚焦于C#开发环境的搭建与配置,详细介绍了.NET SDK的安装步骤,并引导读者完成第一个控制台应用程序的创建与运行,帮助新手顺利迈出编程学习的

weixin_33674976的博客 342
上一篇: Juery 动态移除js、动态添加js
下一篇: 动态DNS负载均衡
dingba0214
博客等级 码龄12年 0粉丝 0原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值