c# word 操作系列之Word代码实现

参考资料:

http://www.cnblogs.com/wngwz/archive/2004/08/19/34678.html

https://www.cnblogs.com/xh6300/p/5915717.html

http://www.cnblogs.com/xhp5678/archive/2005/08/13/214126.html

https://www.cnblogs.com/fellowcheng/articles/1274276.html

https://www.cnblogs.com/wngwz/archive/2004/08/19/34677.html

https://wenku.baidu.com/view/2c4bf7313968011ca300917b.html

https://wenku.baidu.com/view/95ed9a410640be1e650e52ea551810a6f424c861.html



using Microsoft.Office.Interop.Word;
using MSWord = Microsoft.Office.Interop.Word;
using System.Reflection;

private void button1_Click(object sender, System.EventArgs e)
 {
            //Word文档保护密码
            string Pass = "ITIS@997168";
            object PassWord = Pass;
            MSWord.Application wordApp;  //Word应用程序变量
            MSWord.Document wordDoc;    //Word文档变量
            try
            {
                object Nothing = Missing.Value;  //初始化
                wordApp = new MSWord.ApplicationClass();

                // 打开已存在的Word
                object FileName = @"E:\archive\CMPLatest_2117_230614-1053.Rtf";
                object readOnly = false;
                object isVisible = true;
                object objFalse = false;

                wordDoc = wordApp.Documents.Open(ref FileName, ref Nothing, ref readOnly, ref Nothing, ref PassWord, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                //激活Word文档
                wordDoc.Activate();
                //判断是否有密码
                if (wordDoc.HasPassword)
                {
                    wordDoc.Password = null;
                }

                //检查是否为Word文档设置保护功能,没有设置保护功能,就解除密码保护
                if (wordDoc.ProtectionType != WdProtectionType.wdNoProtection)
                {
                    wordDoc.Unprotect(ref PassWord);
                }

                //跳转到指定书签
                object toMark = MSWord.WdGoToItem.wdGoToBookmark;
                //分页符
                object oPageBreak = Microsoft.Office.Interop.Word.WdBreakType.wdPageBreak;  

                //定义书签名称  PartB
                object BookMarkName_b = "bmf_b";
                wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_b);
                //插入分页符
                wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);
                
                //定义书签名称  PartC1
                object BookMarkName_c1 = "bmf_c1";
                wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c1);
                //插入分页符
                wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);

                //定义书签名称  PartC2
                object BookMarkName_c2 = "bmf_c2";
                wordDoc.ActiveWindow.Selection.GoTo(ref toMark, ref Nothing, ref Nothing, ref BookMarkName_c2);
                //插入分页符
                wordDoc.ActiveWindow.Selection.InsertBreak(ref oPageBreak);

                //对Word文档进行加密保护
                if(PassWord.ToString() != null)
                {
                    wordDoc.Protect(WdProtectionType.wdAllowOnlyReading, ref objFalse, ref PassWord, ref Nothing, ref Nothing);
                }
                

                //将插入分页符后的Word文档保存一下
                wordDoc.SaveAs(ref FileName, ref Nothing, ref Nothing, ref Nothing, ref objFalse, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref isVisible, ref Nothing, ref Nothing, ref Nothing, ref Nothing);

                //标记为最终状态,禁止弹出对话框
                //wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;
                //标记为最终状态
                //wordDoc.Final = true;

                //关闭Word文档
                wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
            }
            catch(Exception ex)
            {
                
            }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using Microsoft.Office.Interop.Word;
using com.enkj.job.WebUI.App_Code;
using System.Data;
using System.Reflection;
using JobCommon;

namespace com.enkj.job.WebUI.HttpHandler
{
    /// <summary>
    /// LoadWordResume 的摘要说明
    /// </summary>
    public class LoadWordResume :  PersonUserBase,IHttpHandler
    {
        public DataSet ds = null;
        public List<Model.Person> perList = null;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (context.Request["action"] != null && context.Request["action"].ToString() == "LoadResume")
            {
                GetDataSet();
                context.Response.Write(CreateWord());
                context.Response.End();
            }
        }

        DataSet GetDataSet()
        {
            ds = new Module.PersonResume().GetResumeInfo(sessionID, ResumeId);
            if (ds != null)
            {
                return ds;
            }
            throw new NoNullAllowedException();
        }

        public string CreateWord()
        {
            string message = "";
            try
            {
                Object Nothing = System.Reflection.Missing.Value;
                Directory.CreateDirectory("D:/JobResume");  //创建文件所在目录
                
                string name = "JobResume_" + DateTime.Now.ToString("yyyyMMddhhmmssfff") + ".doc";//文件名
                object filename = "D://JobResume//" + name;  //文件保存路径
                //创建Word文档
                Microsoft.Office.Interop.Word.Application WordApp = new Microsoft.Office.Interop.Word.ApplicationClass();
                Microsoft.Office.Interop.Word.Document WordDoc = WordApp.Documents.Add(ref Nothing, ref Nothing, ref Nothing, ref Nothing);
                WordApp.Selection.PageSetup.LeftMargin = 85f;
                WordApp.Selection.PageSetup.RightMargin = 85f;
                WordApp.Selection.PageSetup.PageWidth = 650f;  //页面宽度
                //添加页眉
                WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("蓝领招聘网模板生成");
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;//设置右对齐
                WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;//跳出页眉设置
                WordApp.Selection.ParagraphFormat.LineSpacing = 15f;//设置文档的行间距     
                //移动焦点并换行
                object count = 14;
                object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;
                WordApp.Selection.Text = "个人简历";
                WordApp.Selection.ParagraphFormat.LineSpacing = 30f;
                WordApp.Selection.Range.Bold = 2;
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                WordApp.Selection.MoveDown(ref WdLine, ref count, ref Nothing);//移动焦点
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                WordApp.Selection.TypeParagraph();//插入段落
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;
                WordApp.Selection.ParagraphFormat.LineSpacing = 16;
                WordApp.Selection.Font.Size = 10f;
                WordApp.Selection.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorGray40;
                //文档中创建表格
                Microsoft.Office.Interop.Word.Table newTable = WordDoc.Tables.Add(WordApp.Selection.Range,8, 3, ref Nothing, ref Nothing);
                //设置表格样式
                newTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;                
                newTable.Columns[1].Width = 175f;
                newTable.Columns[2].Width = 170f;
                newTable.Columns[3].Width = 135f;
                //填充表格内容
                newTable.Cell(1, 1).Range.Text = "个人信息";
                newTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                newTable.Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;              
                //合并单元格
                newTable.Cell(1, 1).Merge(newTable.Cell(1, 3));
                WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//垂直居下
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居左
                WordApp.Selection.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGray25;
                newTable.Rows[1].Range.ParagraphFormat.LineSpacing = 20f;
                //填充表格内容
                newTable.Cell(2, 1).Range.Text = "姓名:"+ds.Tables[0].Rows[0]["name"];
                newTable.Cell(2, 2).Range.Text = "性别:" + Common.DoEnum.EnumManager<JobCommon.ApplicationEnum.Sex>.GetEnumDesc(JobTool.Toint(ds.Tables[0].Rows[0]["sex"]));
                //纵向合并单元格
                newTable.Cell(2, 3).Select();//选中一行
                object moveUnit = Microsoft.Office.Interop.Word.WdUnits.wdLine;
                object moveCount = 5;
                object moveExtend = Microsoft.Office.Interop.Word.WdMovementType.wdExtend;
                WordApp.Selection.MoveDown(ref moveUnit, ref moveCount, ref moveExtend);
                WordApp.Selection.Cells.Merge();             
                //插入图片
                string FileName = Server.MapPath(ds.Tables[0].Rows[0]["Photo"]!=null&&ds.Tables[0].Rows[0]["Photo"].ToString().Length>0?ds.Tables[0].Rows[0]["Photo"].ToString():"/sys_admin/images/noImg.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 = 80f;//图片宽度
                WordDoc.Application.ActiveDocument.InlineShapes[1].Height = 100f;//图片高
                WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                //将图片设置为四周环绕型
                Microsoft.Office.Interop.Word.Shape s = WordDoc.Application.ActiveDocument.InlineShapes[1].ConvertToShape();
                s.WrapFormat.Type = Microsoft.Office.Interop.Word.WdWrapType.wdWrapSquare;

                newTable.Cell(3, 1).Range.Text = "出生日期: "+Convert.ToDateTime(ds.Tables[0].Rows[0]["birthday"]).ToString("yyyy年MM月dd日");
                newTable.Cell(3, 2).Range.Text = "居住地 :" + GetCity();
                newTable.Cell(4, 1).Range.Text = "工作年限: " + GetWorkYears();
                newTable.Cell(5, 1).Range.Text = ds.Tables[0].Rows[0]["MobileNumber"] == null ? "手机号码: "+ds.Tables[0].Rows[0]["MobileNumber"] :  "联系电话: "+ds.Tables[0].Rows[0]["PhoneNumber"];
                newTable.Cell(6, 1).Range.Text = "邮编:" + ds.Tables[0].Rows[0]["PostCode"] == null ? "稍后填写" : ds.Tables[0].Rows[0]["PostCode"].ToString();
                newTable.Cell(6, 1).Range.Text = "身高:" + ds.Tables[0].Rows[0]["Height"] +" cm";
                newTable.Cell(7, 1).Range.Text = "户口 :" + GetHomeCity();
                newTable.Cell(8, 1).Range.Text = "QQ :" + (ds.Tables[0].Rows[0]["QQ"] == null ? "稍后填写" : ds.Tables[0].Rows[0]["QQ"].ToString());
                newTable.Cell(8,1).Range.Text = "电子邮箱:" + (ds.Tables[0].Rows[0]["Email"] == null ? "稍后填写" : ds.Tables[0].Rows[0]["Email"].ToString());
            

                //求职意向          
                object missing = System.Reflection.Missing.Value;
                object count2 = 2;
                object WdLine2 = Microsoft.Office.Interop.Word.WdUnits.wdLine;//换一行;  
                WordApp.Selection.MoveDown(ref WdLine2, ref count2, ref missing);//光标向下移1行  
                WordApp.Selection.TypeParagraph();//在表格外回车 

                if (ds.Tables[1].Rows.Count > 0)
                {

                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table QzyxTable = WordDoc.Tables.Add(WordApp.Selection.Range, 6, 1, ref Nothing, ref Nothing);
                    //设置表格样式
                    QzyxTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                    QzyxTable.Columns[1].Width = 480f;
                    //填充表格内容
                    QzyxTable.Cell(1, 1).Range.Text = "求职意向";
                    QzyxTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                    QzyxTable.Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
                    //合并单元格
                    WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//垂直居下
                    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居左
                    WordApp.Selection.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGray25;
                    QzyxTable.Rows[1].Range.ParagraphFormat.LineSpacing = 20f;
                    QzyxTable.Cell(2, 1).Range.Text = "工作性质: " + GetJobType();
                    QzyxTable.Cell(3, 1).Range.Text = "期望月薪: " + GetMouthSalary();
                    QzyxTable.Cell(4, 1).Range.Text = "工作地点: " + ds.Tables[1].Rows[0]["Workplace"];
                    QzyxTable.Cell(5, 1).Range.Text = "到岗时间: " + ds.Tables[1].Rows[0]["WorkTime"];
                    QzyxTable.Cell(6, 1).Range.Text = "自我评价: " + ds.Tables[1].Rows[0]["MyEvaluate"];
                }


                //工作经验
                object count3 = 6;
                WordApp.Selection.MoveDown(ref WdLine2, ref count3, ref missing);//光标向下移1行  
                WordApp.Selection.TypeParagraph();//在表格外回车 
                int j = 2;
                if (ds.Tables[3].Rows.Count > 0)
                {
                    //工作经验个数
                    int i = ds.Tables[3].Rows.Count;
                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table GzjyTable = WordDoc.Tables.Add(WordApp.Selection.Range, i*5, 1, ref Nothing, ref Nothing);
                    //设置表格样式
                    GzjyTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                    GzjyTable.Columns[1].Width = 480f;
                    //填充表格内容
                    GzjyTable.Cell(1, 1).Range.Text = "工作经验";
                    GzjyTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                    GzjyTable.Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
                    //合并单元格
                    WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//垂直居下
                    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居左
                    WordApp.Selection.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGray25;
                    GzjyTable.Rows[1].Range.ParagraphFormat.LineSpacing = 20f;                   
                    
                    for (int k = 0; k < i; k++)
                    {

                        GzjyTable.Cell(j, 1).Range.Text = JobTool.ToDateTimeNow(ds.Tables[3].Rows[k]["BeginTime"]).ToString("yyyy-MM") + "--" + JobTool.ToDateTimeNow(ds.Tables[3].Rows[k]["EndTime"]).ToString("yyyy-MM") + ":" + " " + ds.Tables[3].Rows[k]["WorkCompany"] + "(" + GetComRange(ds.Tables[3].Rows[k]["Scale"]) + ")" + " [ " + GetInWorkYears(ds.Tables[3].Rows[k]["BeginTime"], ds.Tables[3].Rows[k]["EndTime"]) + "]";

                        GzjyTable.Cell(j + 1, 1).Range.Text = "所属行业:        " + GetIndustryType(ds.Tables[3].Rows[k]["Industry"]);
                        GzjyTable.Cell(j + 2, 1).Range.Text = ds.Tables[3].Rows[k]["Department"] + "              " + ds.Tables[3].Rows[k]["Post"];
                        GzjyTable.Cell(j + 3, 1).Range.Text = "" + ds.Tables[3].Rows[k]["WorkDescription"];
                        if (i * 5 + 1 != j + 4)
                        {
                            GzjyTable.Cell(j + 4, 1).Range.InlineShapes.AddHorizontalLineStandard(ref Nothing);
                        }
                        j = j + 5;
                    }
                }

                //教育经历
                object count4 = j;
                WordApp.Selection.MoveDown(ref WdLine2, ref count4, ref missing);//光标向下移1行  
                WordApp.Selection.TypeParagraph();//在表格外回车 
                if (ds.Tables[2].Rows.Count > 0)
                {
                    //教育经历个数
                    int i = ds.Tables[2].Rows.Count;
                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table jyjlTable = WordDoc.Tables.Add(WordApp.Selection.Range, i * 4, 1, ref Nothing, ref Nothing);
                    //设置表格样式
                    jyjlTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                    jyjlTable.Columns[1].Width = 480f;
                    //填充表格内容
                    jyjlTable.Cell(1, 1).Range.Text = "教育经历";
                    jyjlTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                    jyjlTable.Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
                    //合并单元格
                    WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//垂直居下
                    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居左
                    WordApp.Selection.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGray25;
                    jyjlTable.Rows[1].Range.ParagraphFormat.LineSpacing = 20f;
                    j = 2;
                    for (int k = 0; k < i; k++)
                    {
                        jyjlTable.Cell(j, 1).Range.Text = JobTool.ToDateTimeNow(ds.Tables[2].Rows[k]["BeginTime"]).ToString("yyyy-MM") + "--" + JobTool.ToDateTimeNow(ds.Tables[2].Rows[k]["EndTime"]).ToString("yyyy-MM") + ":" + " " + ds.Tables[2].Rows[k]["school"] + "         (" + ds.Tables[2].Rows[k]["EduBackground"] + ")" ;
                        jyjlTable.Cell(j + 1, 1).Range.Text = "专业:        " + ds.Tables[2].Rows[k]["Specialty"];
                        jyjlTable.Cell(j + 2, 1).Range.Text = ""+ds.Tables[2].Rows[k]["SpecialtyDescription"];
                        if (i * 4 + 1 != j + 3)
                        {
                            jyjlTable.Cell(j + 3, 1).Range.InlineShapes.AddHorizontalLineStandard(ref Nothing);
                        }
                        j = j + 4;
                    }
                }


                //培训经历
                object count5 = j+8;
                WordApp.Selection.MoveDown(ref WdLine2, ref count5, ref missing);//光标向下移1行  
                WordApp.Selection.TypeParagraph();//在表格外回车 
                if (ds.Tables[4].Rows.Count > 0)
                {
                    //培训经历个数
                    int i = ds.Tables[4].Rows.Count;
                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table pxjlTable = WordDoc.Tables.Add(WordApp.Selection.Range, i * 5, 1, ref Nothing, ref Nothing);
                    //设置表格样式
                    pxjlTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                    pxjlTable.Columns[1].Width = 480f;
                    //填充表格内容
                    pxjlTable.Cell(1, 1).Range.Text = "培训经历";
                    pxjlTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                    pxjlTable.Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
                    //合并单元格
                    WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//垂直居下
                    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居左
                    WordApp.Selection.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGray25;
                    pxjlTable.Rows[1].Range.ParagraphFormat.LineSpacing = 20f;
                    j = 2;
                    for (int k = 0; k < i; k++)
                    {
                        pxjlTable.Cell(j, 1).Range.Text = "时  间:    " + JobTool.ToDateTimeNow(ds.Tables[4].Rows[k]["BeginTime"]).ToString("yyyy-MM") + "--" + JobTool.ToDateTimeNow(ds.Tables[4].Rows[k]["EndTime"]).ToString("yyyy-MM") + "" + "           培训机构:  " + ds.Tables[4].Rows[k]["CultivateCompany"] ;
                        pxjlTable.Cell(j + 1, 1).Range.Text = "培训地点:        " + ds.Tables[4].Rows[k]["Address"] + "                 培训课程:  " + ds.Tables[4].Rows[k]["Subject"];
                        pxjlTable.Cell(j + 2, 1).Range.Text = "获得证书:" + ds.Tables[4].Rows[k]["Certificate"];
                        pxjlTable.Cell(j + 3, 1).Range.Text = "自我评价:" + ds.Tables[4].Rows[k]["Description"];
                        if (i * 5 + 1 != j + 4)
                        {
                            pxjlTable.Cell(j + 4, 1).Range.InlineShapes.AddHorizontalLineStandard(ref Nothing);
                        }
                        j = j + 5;
                    }
                }
                //语言能力
                object count6 = j + 3;
                WordApp.Selection.MoveDown(ref WdLine2, ref count6, ref missing);//光标向下移1行  
                WordApp.Selection.TypeParagraph();//在表格外回车 
                if (ds.Tables[5].Rows.Count > 0)
                {
                    //语言能力个数
                    int i = ds.Tables[5].Rows.Count;
                    //文档中创建表格
                    Microsoft.Office.Interop.Word.Table yynlTable = WordDoc.Tables.Add(WordApp.Selection.Range, i * 5, 1, ref Nothing, ref Nothing);
                    //设置表格样式
                    yynlTable.Borders.OutsideLineStyle = Microsoft.Office.Interop.Word.WdLineStyle.wdLineStyleNone;
                    yynlTable.Columns[1].Width = 480f;
                    //填充表格内容
                    yynlTable.Cell(1, 1).Range.Text = "语言能力";
                    yynlTable.Cell(1, 1).Range.Bold = 2;//设置单元格中字体为粗体
                    yynlTable.Cell(1, 1).Range.Font.Color = Microsoft.Office.Interop.Word.WdColor.wdColorBlack;
                    //合并单元格
                    WordApp.Selection.Cells.VerticalAlignment = Microsoft.Office.Interop.Word.WdCellVerticalAlignment.wdCellAlignVerticalBottom;//垂直居下
                    WordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphLeft;//水平居左
                    WordApp.Selection.Shading.BackgroundPatternColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdGray25;
                    yynlTable.Rows[1].Range.ParagraphFormat.LineSpacing = 20f;
                    j = 2;
                    for (int k = 0; k < i; k++)
                    {
                        yynlTable.Cell(j, 1).Range.Text = "语言类别: " + ds.Tables[5].Rows[k]["language"];
                        yynlTable.Cell(j + 1, 1).Range.Text = "掌握程度:" + ds.Tables[5].Rows[k]["level"];
                        yynlTable.Cell(j + 2, 1).Range.Text = "读写能力:" + ds.Tables[5].Rows[k]["ReadWrite"];
                        yynlTable.Cell(j + 3, 1).Range.Text = "听说能力:" + ds.Tables[5].Rows[k]["ListenSpeak"];
                        if (i * 5 + 1 != j + 4)
                        {
                            yynlTable.Cell(j + 4, 1).Range.InlineShapes.AddHorizontalLineStandard(ref Nothing);
                        }
                        j = j + 5;
                    }
                }
                //文件保存
                WordDoc.SaveAs(ref filename, 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);
                WordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
                WordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
                message = name + "文档生成成功,以保存到 D:\\JobResume 下";
            }
            catch
            {
                message = "文件导出异常!";
            }
            return message;
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}


发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。