static void Main(string[] args)
        {
            Word._Application MyWord = (Word._Application)Marshal.GetActiveObject("Word.Application");

            MyWord.PrintPreview = true;
            System.Windows.Forms.Application.Run();
        }
        /// <summary>
        /// 导出后备干部考察材料表
        /// </summary>
        /// <param name="idlist">需要导出后备干部的id号集合</param>
        public void exportword(ArrayList idlist)
        {
            //选择保存路径
            #region
            string         savepath = "";
            SaveFileDialog sa       = new SaveFileDialog();
            sa.Filter   = "Document(*.doc)|*.doc";
            sa.FileName = "考察材料表";
            if (sa.ShowDialog() == DialogResult.OK)
            {
                savepath = sa.FileName;
            }
            else
            {
                return;
            }
            #endregion

            //创建word应用程序
            wordappliction = new Word.Application();
            object        filepath = System.Windows.Forms.Application.StartupPath + "\\wordModel" + "\\考察材料1.doc";
            Word.Document mydoc    = wordappliction.Documents.Open(ref filepath, ref missing, ref readOnly,
                                                                   ref missing, ref missing, ref missing, ref missing, ref missing,
                                                                   ref missing, ref missing, ref missing, ref isVisible, ref missing,
                                                                   ref missing, ref missing, ref missing);
            //wordappliction.Visible = true;
            mydoc.ActiveWindow.Selection.WholeStory();
            mydoc.ActiveWindow.Selection.Tables[1].Select();
            mydoc.ActiveWindow.Selection.Copy();
            for (int i = 1; i < idlist.Count; i++)
            {
                //插入分页符
                #region
                Word.Paragraph para1;
                para1 = mydoc.Content.Paragraphs.Add(ref missing);
                object pBreak = (int)WdBreakType.wdSectionBreakNextPage;
                //控制位置
                para1.Range.InsertBreak(ref pBreak);
                #endregion
                //粘贴剪贴板中的内容,同时加上加上备注。
                para1.Range.Paste();
            }

            string selectid = "";
            for (int i = 0; i < idlist.Count; i++)
            {
                if (i == idlist.Count - 1)
                {
                    selectid = selectid + "'" + idlist[i] + "'";
                }
                else
                {
                    selectid = selectid + "'" + idlist[i] + "',";
                }
            }
            string        sql        = "select  name,material,cid from TB_CommonInfo where isdelete=0 order by rank,joinTeam desc";
            string        cond       = "cid in (" + selectid + ")";
            DataOperation dataOp     = new DataOperation();
            DataTable     datatableT = dataOp.GetOneDataTable_sql(sql);
            DataTable     dt         = new DataTable();
            dt = datatableT.Clone();
            DataView dv = datatableT.AsDataView();
            dv.RowFilter = cond;
            dt           = dv.ToTable();

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                mydoc.Tables[i + 1].Cell(1, 1).Range.Text = dt.Rows[i][0].ToString() + "同志考察材料";
                mydoc.Tables[i + 1].Cell(2, 1).Range.Text = dt.Rows[i][1].ToString();
            }

            #region
            object path = savepath;
            //wordappliction.Documents.Save(path);
            object myobj = System.Reflection.Missing.Value;;
            mydoc.SaveAs(ref path, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj);
            #endregion
            //关闭退出文档
            #region
            //关闭文档
            mydoc.Close(ref myobj, ref myobj, ref myobj);
            //退出应用程序。
            wordappliction.Quit();
            #endregion
            MessageBox.Show("导出成功!");
        }
        /// <summary>
        /// 导出所选后备干部简要情况登记表
        /// </summary>
        /// <param name="idlist">所选后备干部id号集合</param>
        public void exportword(ArrayList idlist)  //建议把Datatable类型的参数传过来,建议读视图。
        {
            //弹出对话框,选择保存的路径
            #region
            string         savepath = "";
            SaveFileDialog sa       = new SaveFileDialog();
            sa.Filter   = "Document(*.doc)|*.doc";
            sa.FileName = "简要情况登记表";
            if (sa.ShowDialog() == DialogResult.OK)
            {
                savepath = sa.FileName;
            }
            else
            {
                return;
            }
            #endregion

            //创建word应用程序
            wordappliction = new Word.Application();

            //打开指定路径的内容
            #region
            object filepath = System.Windows.Forms.Application.StartupPath + "\\wordModel" + "\\简要情况.doc";
            mydoc = wordappliction.Documents.Open(ref filepath, ref missing, ref readOnly,
                                                  ref missing, ref missing, ref missing, ref missing, ref missing,
                                                  ref missing, ref missing, ref missing, ref isVisible, ref missing,
                                                  ref missing, ref missing, ref missing);
            #endregion
            ////设置word创建的word程序的可见性
            wordappliction.Visible = true;

            //将整个活动区域全部复制
            #region
            mydoc.ActiveWindow.Selection.WholeStory();
            mydoc.ActiveWindow.Selection.Copy();
            #endregion

            string selectid = "";

            for (int i = 0; i < idlist.Count; i++)
            {
                if (i == idlist.Count - 1)
                {
                    selectid = selectid + "'" + idlist[i] + "'";
                }
                else
                {
                    selectid = selectid + "'" + idlist[i] + "',";
                }
            }

            ImageCollection(selectid);

            //读取后备干部的相关信息
            string        sql        = "select * from TB_CommonInfo order by rank, joinTeam desc";
            string        cond       = "cid in (" + selectid + ")";
            DataOperation dataOp     = new DataOperation();
            DataTable     datatableT = dataOp.GetOneDataTable_sql(sql);
            DataTable     datatable  = new DataTable();
            datatable = datatableT.Clone();
            DataView dv = datatableT.AsDataView();
            dv.RowFilter = cond;
            datatable    = dv.ToTable();

            Object myobj = Missing.Value;
            for (int i = 0; i < datatable.Rows.Count - 1; i++)
            {
                //加入两段避免了最后一行文本被弄到下面
                Word.Paragraph para1;
                Word.Paragraph para2;
                para1 = mydoc.Content.Paragraphs.Add(ref myobj);
                para2 = mydoc.Content.Paragraphs.Add(ref myobj);
                object pBreak = (int)WdBreakType.wdSectionBreakNextPage;
                para2.Range.InsertBreak(ref pBreak);
                //调用粘贴方法即可粘贴剪贴板中的内容。
                para2.Range.Paste();
            }
            Shape s = null;
            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                mydoc.ActiveWindow.Selection.WholeStory();
                wordappliction.Selection.Tables[2 * i + 1].Cell(1, 2).Range.Text = datatable.Rows[i]["name"].ToString(); //姓名
                wordappliction.Selection.Tables[2 * i + 1].Cell(1, 4).Range.Text = datatable.Rows[i]["sex"].ToString();  //性别;
                string age = datatable.Rows[i]["birthday"].ToString().Replace("年", ".");

                int nowAge = 0;
                int temp1  = Convert.ToInt32(datatable.Rows[i]["birthday"].ToString().Substring(5, 2));
                int now1   = Convert.ToInt32(DateTime.Now.ToString("MM"));
                int temp2  = Convert.ToInt32(datatable.Rows[i]["birthday"].ToString().Substring(0, 4));
                int now2   = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
                if (temp1 <= now1)
                {
                    nowAge = now2 - temp2;
                }
                else
                {
                    nowAge = now2 - temp2 - 1;
                }
                wordappliction.Selection.Tables[2 * i + 1].Cell(1, 6).Range.Text = age.Replace("月", "") + "\n(" + nowAge + "岁)"; //出生年月

                string filepath1 = System.Windows.Forms.Application.StartupPath + "\\图片夹\\" + datatable.Rows[i]["cid"].ToString() + ".jpg";

                if (System.IO.File.Exists(filepath1))
                {
                    double height    = 0.0;
                    double width     = 0.0;
                    Image  pic       = Image.FromFile(filepath1); //strFilePath是该图片的绝对路径
                    int    intWidth  = pic.Width;                 //长度像素值
                    int    intHeight = pic.Height;                //高度像素值
                    pic.Dispose();
                    if ((double)intHeight / intWidth > 118 / 91.0)
                    {
                        height = 118;
                        width  = 118 * (double)intWidth / intHeight;
                    }
                    else
                    {
                        width  = 91;
                        height = 91 * (double)intHeight / intWidth;
                    }
                    object LinkToFile       = false;
                    object SaveWithDocument = true;
                    object Anchor           = wordappliction.Selection.Tables[2 * i + 1].Cell(1, 7).Range;

                    wordappliction.ActiveDocument.InlineShapes.AddPicture(filepath1, ref LinkToFile, ref SaveWithDocument, ref Anchor);

                    wordappliction.ActiveDocument.InlineShapes[1].Select();
                    wordappliction.ActiveDocument.InlineShapes[1].Width = (int)width;   //图片宽度

                    wordappliction.ActiveDocument.InlineShapes[1].Height = (int)height; //图片高度
                    try
                    {
                        s = wordappliction.ActiveDocument.InlineShapes[1].ConvertToShape();
                        s.WrapFormat.Type = Word.WdWrapType.wdWrapNone;
                        s = null;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message);
                    }
                    mydoc.ActiveWindow.Selection.WholeStory();
                }

                wordappliction.Selection.Tables[2 * i + 1].Cell(2, 2).Range.Text = datatable.Rows[i]["nation"].ToString();                                                      //民族
                wordappliction.Selection.Tables[2 * i + 1].Cell(2, 4).Range.Text = datatable.Rows[i]["native"].ToString();                                                      //籍贯
                wordappliction.Selection.Tables[2 * i + 1].Cell(2, 6).Range.Text = datatable.Rows[i]["birthplace"].ToString();                                                  //出生地
                string partytime = datatable.Rows[i]["partyTime"].ToString().Replace("年", ".");
                wordappliction.Selection.Tables[2 * i + 1].Cell(3, 2).Range.Text = partytime.Replace("月", "");                                                                  //入党时间
                string worktime = datatable.Rows[i]["workTime"].ToString().Replace("年", ".");
                wordappliction.Selection.Tables[2 * i + 1].Cell(3, 4).Range.Text  = worktime.Replace("月", "");                                                                  //参加工作时间
                wordappliction.Selection.Tables[2 * i + 1].Cell(3, 6).Range.Text  = datatable.Rows[i]["health"].ToString();                                                     //健康状况
                wordappliction.Selection.Tables[2 * i + 1].Cell(4, 2).Range.Text  = datatable.Rows[i]["technicalPost"].ToString();                                              //专业技术职务
                wordappliction.Selection.Tables[2 * i + 1].Cell(4, 4).Range.Text  = datatable.Rows[i]["specialtySkill"].ToString();                                             //熟悉专业有何专长
                wordappliction.Selection.Tables[2 * i + 1].Cell(5, 3).Range.Text  = datatable.Rows[i]["fullEducation"].ToString() + datatable.Rows[i]["fullDegree"].ToString(); //全日制教育
                wordappliction.Selection.Tables[2 * i + 1].Cell(5, 5).Range.Text  = datatable.Rows[i]["fullSchool"].ToString() + datatable.Rows[i]["fullSpecialty"].ToString(); //毕业院校及专业
                wordappliction.Selection.Tables[2 * i + 1].Cell(6, 3).Range.Text  = datatable.Rows[i]["workEducation"].ToString() + datatable.Rows[i]["workDegree"].ToString(); //在职教育
                wordappliction.Selection.Tables[2 * i + 1].Cell(6, 5).Range.Text  = datatable.Rows[i]["workGraduate"].ToString() + datatable.Rows[i]["workSpecialty"].ToString();
                wordappliction.Selection.Tables[2 * i + 1].Cell(7, 2).Range.Text  = datatable.Rows[i]["position"].ToString();
                wordappliction.Selection.Tables[2 * i + 1].Cell(8, 2).Range.Text  = datatable.Rows[i]["knowField"].ToString();
                wordappliction.Selection.Tables[2 * i + 1].Cell(9, 2).Range.Text  = datatable.Rows[i]["trainDirection"].ToString();       //"培养方向";
                wordappliction.Selection.Tables[2 * i + 1].Cell(10, 2).Range.Text = datatable.Rows[i]["trainMeasure"].ToString();         //"培养措施";
                wordappliction.Selection.Tables[2 * i + 1].Cell(11, 2).Range.Text = "\n" + HBresume(datatable.Rows[i]["CID"].ToString()); //"简历";

                //第二页:奖惩情况,年度考核,家庭成员关系
                mydoc.ActiveWindow.Selection.WholeStory();                                                                                      //表示在选中的范围内
                wordappliction.Selection.Tables[2 * i + 2].Cell(1, 2).Range.Text = HBrewardsAndpunishment(datatable.Rows[i]["CID"].ToString()); //"奖惩情况";
                wordappliction.Selection.Tables[2 * i + 2].Cell(2, 2).Range.Text = HByearcheck(datatable.Rows[i]["CID"].ToString());            // "年度考核结果";\

                //得到该干部的家庭信息
                DataTable famliy_dt = dataOp.GetOneDataTable_sql("select * from TB_Family where cid ='" + datatable.Rows[i]["CID"].ToString() + "'");

                int n = famliy_dt.Rows.Count; //家庭人员的个数
                for (int j = 0; j < n; j++)
                {
                    wordappliction.Selection.Tables[2 * i + 2].Cell(j + 4, 2).Range.Text = famliy_dt.Rows[j]["relationship"].ToString(); //"称谓";
                    wordappliction.Selection.Tables[2 * i + 2].Cell(j + 4, 3).Range.Text = famliy_dt.Rows[j]["name"].ToString();         //"姓名";
                    if (famliy_dt.Rows[j]["age"].ToString() == "0")
                    {
                        wordappliction.Selection.Tables[2 * i + 2].Cell(j + 4, 4).Range.Text = "";
                    }
                    else
                    {
                        wordappliction.Selection.Tables[2 * i + 2].Cell(j + 4, 4).Range.Text = famliy_dt.Rows[j]["age"].ToString(); //"年龄";
                    }
                    wordappliction.Selection.Tables[2 * i + 2].Cell(j + 4, 5).Range.Text = famliy_dt.Rows[j]["party"].ToString();   //"政治面貌";
                    if (famliy_dt.Rows[j]["remark"].ToString().Trim() == "已故")
                    {
                        wordappliction.Selection.Tables[2 * i + 2].Cell(j + 4, 6).Range.Text = famliy_dt.Rows[j]["deptJob"].ToString() + "(已故)";//"工作单位及职务";
                    }
                    else
                    {
                        wordappliction.Selection.Tables[2 * i + 2].Cell(j + 4, 6).Range.Text = famliy_dt.Rows[j]["deptJob"].ToString();//"工作单位及职务";
                    }
                }
            }
            //把文件保存在选定的保存路径上
            #region
            object path = savepath;
            //wordappliction.Documents.Save(path);
            mydoc.SaveAs(ref path, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj);
            #endregion

            //关闭退出文档
            #region
            //关闭文档
            mydoc.Close(ref myobj, ref myobj, ref myobj);

            //退出应用程序。
            wordappliction.Quit();
            #endregion
            MessageBox.Show("导出成功!");
            return;
        }
Example #4
0
        private static int pagecount = 5;//记录花名册一页显示多少条记录

        /// <summary>
        /// 给初步人选名册表填值并导出
        /// </summary>
        /// <param name="idlist">传递所选干部的所有id</param>
        public void exportword(ArrayList idlist)
        {
            #region
            //选择保存路径
            #region
            string         savepath = "";
            SaveFileDialog sa       = new SaveFileDialog();
            sa.Filter   = "Document(*.doc)|*.doc";
            sa.FileName = "初步人选名册";
            if (sa.ShowDialog() == DialogResult.OK)
            {
                savepath = sa.FileName;
            }
            else
            {
                return;
            }
            #endregion

            //创建word应用程序
            wordappliction = new Word.Application();
            //打开模板

            object filepath = System.Windows.Forms.Application.StartupPath + "\\wordModel" + "\\花名册.doc";

            Word.Document mydoc = wordappliction.Documents.Open(ref filepath, ref missing, ref readOnly,
                                                                ref missing, ref missing, ref missing, ref missing, ref missing,
                                                                ref missing, ref missing, ref missing, ref isVisible, ref missing,
                                                                ref missing, ref missing, ref missing);
            //wordappliction.Visible = true;
            //打开模板复制其中的表

            #endregion


            string selectid = "";

            for (int i = 0; i < idlist.Count; i++)
            {
                if (i == idlist.Count - 1)
                {
                    selectid = selectid + "'" + idlist[i] + "'";
                }
                else
                {
                    selectid = selectid + "'" + idlist[i] + "',";
                }
            }

            //读取干部的相关信息
            string sql = "select name,sex,nation,department,position,native,birthday,age,partyTime,workTime,fullEducation,fullDegree,fullSchool,fullSpecialty,workEducation,workDegree,workGraduate,workSpecialty,technicalPost,experiencePost,knowField,trainDirection,trainMeasure,partyClass from TB_CommonInfo where cid in (" + selectid + ")";

            DataOperation         dataOp    = new DataOperation();
            System.Data.DataTable datatable = dataOp.GetOneDataTable_sql(sql);

            //确定表头的单位信息
            string title = null;

            mydoc.Tables[1].Cell(1, 1).Range.Text = title;

            mydoc.Tables[3].Cell(1, 1).Range.Text = title + "初步人选名册";
            #region
            mydoc.ActiveWindow.Selection.WholeStory();
            mydoc.ActiveWindow.Selection.Tables[4].Select();
            mydoc.ActiveWindow.Selection.Copy();
            #endregion

            //添加前面的说明信息
            //以下几个变量用于干部信息的统计
            int allcount       = 0; //统计总人数
            int mancount       = 0; //记录男总人数
            int womencoumt     = 0; //记录女总人数
            int unpartyount    = 0; //记录非中共党员人数
            int fewnationcount = 0; //记录少数民族人数
            int age40          = 0; //记录40岁以下人数
            int age41_45       = 0; //记录41到45岁人数
            int age46_50       = 0; //记录46到50岁人数
            int age51          = 0; //记录51岁以上人数
            int doctorgraduate = 0; //记录博士研究生人数
            int mastergraduate = 0; //记录硕士研究生人数
            int graduate       = 0; //记录大学生人数
            int aveage         = 0; //记录平均年龄

            //所用到的一切信息统计结果
            #region
            //总人数
            allcount = datatable.Rows.Count;
            //男人数
            DataTable datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and sex = '男'");
            mancount = datatable1.Rows.Count;
            //女人数
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and sex = '女'");
            womencoumt = datatable1.Rows.Count;
            //非中共
            datatable1  = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and partyClass = '中共'");
            unpartyount = allcount - datatable1.Rows.Count;
            //少数民族
            datatable1     = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and nation like '%汉%'");
            fewnationcount = allcount - datatable1.Rows.Count;


            //年龄段统计


            int sum_age = 0;
            datatable1 = dataOp.GetOneDataTable_sql("select * from TB_CommonInfo where cid in (" + selectid + ") ");

            for (int i = 0; i < datatable1.Rows.Count; i++)
            {
                //获取当前年份
                int year = System.DateTime.Now.Year;
                //获取当前月份
                int month = System.DateTime.Now.Month;
                //获取出生年份
                string str2 = datatable1.Rows[i]["birthday"].ToString().Substring(0, 4);
                //出生月份
                string str3 = datatable1.Rows[i]["birthday"].ToString().Substring(5, 2);
                sum_age = sum_age + (year - Convert.ToInt32(str2));

                if (Convert.ToInt32(str3) < month)
                {
                    if ((year - Convert.ToInt32(str2) - 1) <= 40)
                    {
                        age40++;
                    }
                    if ((year - Convert.ToInt32(str2) - 1) > 40 && (year - Convert.ToInt32(str2)) <= 45)
                    {
                        age41_45++;
                    }
                    if ((year - Convert.ToInt32(str2) - 1) > 45 && (year - Convert.ToInt32(str2)) <= 50)
                    {
                        age46_50++;
                    }
                    if ((year - Convert.ToInt32(str2) - 1) > 50)
                    {
                        age51++;
                    }
                }
                else
                {
                    if ((year - Convert.ToInt32(str2)) <= 40)
                    {
                        age40++;
                    }
                    if ((year - Convert.ToInt32(str2)) > 40 && (year - Convert.ToInt32(str2)) <= 45)
                    {
                        age41_45++;
                    }
                    if ((year - Convert.ToInt32(str2)) > 45 && (year - Convert.ToInt32(str2)) <= 50)
                    {
                        age46_50++;
                    }
                    if ((year - Convert.ToInt32(str2)) > 50)
                    {
                        age51++;
                    }
                }
            }

            aveage = (int)sum_age / datatable1.Rows.Count;

            //博士
            datatable1     = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and fullDegree = '博士'");
            doctorgraduate = datatable1.Rows.Count;
            //硕士
            datatable1     = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and fullDegree = '硕士'");
            mastergraduate = datatable1.Rows.Count;
            //学士
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and fullDegree = '学士'");
            graduate   = datatable1.Rows.Count;
            #endregion

            //表头和说明信息
            #region
            string explain = "    " + "初步人选共" + allcount + "人,其中:男" + mancount + "人,女" + womencoumt + "人,非中共党员干部" + unpartyount + "人,少数民族干部" + fewnationcount + "人,40岁及以下" + age40 + "人,41-45岁" + age41_45 + "人,46-50岁" + age46_50 + "人,51岁以上" + age51 + "人,平均年龄" + Math.Round(Convert.ToDouble(aveage), 2) + "岁,博士研究生" + doctorgraduate + "人,硕士研究生" + mastergraduate + "人,大学" + graduate + "人。";

            #endregion
            mydoc.Tables[2].Range.Text = explain;

            int tablecount1 = 0;
            if (datatable.Rows.Count % pagecount == 0)
            {
                tablecount1 = datatable.Rows.Count / pagecount - 1;
            }
            else
            {
                tablecount1 = datatable.Rows.Count / pagecount;
            }

            #region

            for (int i = 0; i < tablecount1; i++)
            {
                //第二页的前两行
                Word.Paragraph para1 = mydoc.Content.Paragraphs.Add(ref missing);
                Word.Paragraph para2 = mydoc.Content.Paragraphs.Add(ref missing);
                Word.Paragraph para3 = mydoc.Content.Paragraphs.Add(ref missing);
                //在此粘贴复制的表格
                //控制位置
                object pBreak = ( int )WdBreakType.wdSectionBreakNextPage;
                para1.Range.InsertBreak(ref pBreak);
                para3.Range.Paste();
                if (i == tablecount1 - 1)
                {
                    para3.Range.Text = " 注:干部人选是民主党派成员的,在“入党时间”栏填写党派名称及加入时间;无党派人士,此栏不填。";
                }
            }

            if (tablecount1 == 0)
            {
                Word.Paragraph para1 = mydoc.Content.Paragraphs.Add(ref missing);
                para1.Range.Text = " 注:干部人选是民主党派成员的,在“入党时间”栏填写党派名称及加入时间;无党派人士,此栏不填。";
            }
            #endregion

            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                int    tableindex = i / pagecount + 4;
                int    rowindex = i % pagecount + 3;
                string strS = "", strN = "";
                string Remarks = "";
                if (datatable.Rows[i]["sex"].ToString().Trim() != "男")
                {
                    strS = datatable.Rows[i]["sex"].ToString().Trim();
                }
                if (datatable.Rows[i]["nation"].ToString().Trim() != "汉族")
                {
                    strN = datatable.Rows[i]["nation"].ToString().Trim();
                }
                if (strS != "" && strN == "")
                {
                    Remarks = "(" + strS + ")";
                }
                else
                if (strS != "" && strN != "")
                {
                    Remarks = "(" + strS + "、" + strN + ")";
                }
                else
                if (strS == "" && strN != "")
                {
                    Remarks = "(" + strN + ")";
                }
                mydoc.Tables[tableindex].Cell(rowindex, 1).Select();
                mydoc.Tables[tableindex].Cell(rowindex, 1).Range.Text = Remarks;
                //在选中的位置添加文本
                string strna = datatable.Rows[i]["name"].ToString() + "\n";
                wordappliction.Selection.Font.Name = "宋体 ";
                wordappliction.Selection.Font.Size = 10.5f;
                wordappliction.Selection.TypeText(strna);
                try
                {
                    mydoc.Tables[tableindex].Cell(rowindex, 2).Range.Text = datatable.Rows[i]["department"].ToString() + "\n" + datatable.Rows[i]["position"].ToString(); //"工作单位及职务";
                    mydoc.Tables[tableindex].Cell(rowindex, 3).Range.Text = datatable.Rows[i]["native"].ToString();                                                       //"籍贯";
                    string age = datatable.Rows[i]["birthday"].ToString().Replace("年", ".");
                    mydoc.Tables[tableindex].Cell(rowindex, 4).Range.Text = age.Replace("月", "");                                                                         //"出生年月";
                    string partytime = datatable.Rows[i]["partyTime"].ToString().Replace("年", ".");
                    mydoc.Tables[tableindex].Cell(rowindex, 5).Range.Text = partytime.Replace("月", "");                                                                   //"入党时间";
                    string worktime = datatable.Rows[i]["workTime"].ToString().Replace("年", ".");
                    mydoc.Tables[tableindex].Cell(rowindex, 6).Range.Text = worktime.Replace("月", "");
                    //"参加工作时间"
                    mydoc.Tables[tableindex].Cell(rowindex, 7).Range.Text  = datatable.Rows[i]["fullEducation"].ToString() + "\n" + datatable.Rows[i]["fullDegree"].ToString();   //"学历学位";
                    mydoc.Tables[tableindex].Cell(rowindex, 8).Range.Text  = datatable.Rows[i]["fullSchool"].ToString() + "\n" + datatable.Rows[i]["fullSpecialty"].ToString();   // "毕业院校及专业";
                    mydoc.Tables[tableindex].Cell(rowindex, 9).Range.Text  = datatable.Rows[i]["workEducation"].ToString() + "\n" + datatable.Rows[i]["workDegree"].ToString();   //"学历学位";
                    mydoc.Tables[tableindex].Cell(rowindex, 10).Range.Text = datatable.Rows[i]["workGraduate"].ToString() + "\n" + datatable.Rows[i]["workSpecialty"].ToString(); //"毕业院校及专业";
                    mydoc.Tables[tableindex].Cell(rowindex, 11).Range.Text = datatable.Rows[i]["technicalPost"].ToString();                                                       //"专业技术职务";
                    mydoc.Tables[tableindex].Cell(rowindex, 12).Range.Text = datatable.Rows[i]["experiencePost"].ToString();                                                      //"历任主要职务";
                    mydoc.Tables[tableindex].Cell(rowindex, 13).Range.Text = datatable.Rows[i]["knowField"].ToString();                                                           //"熟悉领域";
                    mydoc.Tables[tableindex].Cell(rowindex, 14).Range.Text = datatable.Rows[i]["trainDirection"].ToString();                                                      //"培养方向";
                    mydoc.Tables[tableindex].Cell(rowindex, 15).Range.Text = datatable.Rows[i]["trainMeasure"].ToString();                                                        //"培养措施";
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出失败,请重新操作!" + ex.Message);
                    return;
                }
            }

            #region
            object path = savepath;
            //wordappliction.Documents.Save(path);
            object myobj = System.Reflection.Missing.Value;
            ;
            mydoc.SaveAs(ref path, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj);
            #endregion

            //关闭退出文档
            #region
            //关闭文档
            mydoc.Close(ref myobj, ref myobj, ref myobj);
            //退出应用程序。
            wordappliction.Quit();
            #endregion
            MessageBox.Show("导出成功!");
        }
        /// <summary>
        /// 导出word的方法
        /// </summary>
        /// <param name="idlist">导出后备干部的id号集合</param>
        public void exportword(ArrayList idlist)  //建议把Datatable类型的参数传过来,建议读视图。
        {
            //表头信息
            #region
            string title = null;
            if (this.Unitclass.Equals("省直单位"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = "正厅级后备干部考察对象";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = "副厅级后备干部考察对象";
                }
            }
            else if (this.Unitclass.Equals("省辖市"))
            {
                title = "党政" + this.Qd + "后备干部考察对象";
            }
            else if (this.Unitclass.Equals("省管高校"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = "正校级后备干部考察对象";
                }
                else
                if (this.Qd.Equals("副职"))
                {
                    title = "副校级后备干部考察对象";
                }
            }
            else if (this.Unitclass.Equals("省管企业"))
            {
                title = "领导班子" + this.Qd + "后备干部考察对象";
            }
            if (this.Unitclass.Equals("市直单位"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = "正县级后备干部考察对象";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = "副县级后备干部考察对象";
                }
            }
            else if (this.Unitclass.Equals("县(市、区)"))
            {
                title = "党政" + this.Qd + "后备干部考察对象";
            }
            else if (this.Unitclass.Equals("市管学校"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = "正校级后备干部考察对象";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = "副校级后备干部考察对象";
                }
            }
            else if (this.Unitclass.Equals("市管企业"))
            {
                title = "领导班子" + this.Qd + "后备干部考察对象";
            }
            else if (this.Unitclass.Equals("县(市、区)直"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = "正科级后备干部考察对象";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = "副科级后备干部考察对象";
                }
            }
            else if (this.Unitclass.Equals("乡(镇、街道)"))
            {
                title = "党政" + this.Qd + "后备干部考察对象";
            }
            else if (this.Unitclass.Equals("县管学校"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = "正校级后备干部考察对象";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = "副校级后备干部考察对象";
                }
            }
            else if (this.Unitclass.Equals("县管企业"))
            {
                title = "领导班子" + this.Qd + "后备干部考察对象";
            }


            #endregion

            //选择保存路径
            #region
            string         savepath = "";
            SaveFileDialog sa       = new SaveFileDialog();
            sa.Filter   = "Document(*.doc)|*.doc";
            sa.FileName = "信息采集表";
            if (sa.ShowDialog() == DialogResult.OK)
            {
                savepath = sa.FileName;
            }
            else
            {
                return;
            }
            #endregion

            //创建word应用程序
            wordappliction = new Word.Application();

            //打开模板
            object filepath = System.Windows.Forms.Application.StartupPath + "\\wordModel" + "\\信息采集表.doc";
            mydoc = wordappliction.Documents.Open(ref filepath, ref missing, ref readOnly,
                                                  ref missing, ref missing, ref missing, ref missing, ref missing,
                                                  ref missing, ref missing, ref missing, ref isVisible, ref missing,
                                                  ref missing, ref missing, ref missing);
            //打开模板复制其中的表
            #region
            mydoc.ActiveWindow.Selection.WholeStory();
            mydoc.ActiveWindow.Selection.Select();
            mydoc.ActiveWindow.Selection.Copy();
            #endregion

            string selectid = "";

            for (int i = 0; i < idlist.Count; i++)
            {
                if (i == idlist.Count - 1)
                {
                    selectid = selectid + "'" + idlist[i] + "'";
                }
                else
                {
                    selectid = selectid + "'" + idlist[i] + "',";
                }
            }

            //读取后备干部的相关信息
            string        sql        = "select CID,name,department,position from TB_CommonInfo order by rank, joinTeam desc";
            string        cond       = "cid in (" + selectid + ")";
            DataOperation dataOp     = new DataOperation();
            DataTable     datatableT = dataOp.GetOneDataTable_sql(sql);
            DataTable     datatable  = new DataTable();
            datatable = datatableT.Clone();
            DataView dv = datatableT.AsDataView();
            dv.RowFilter = cond;
            datatable    = dv.ToTable();

            //根据datatable中数据的数量进行文档的拷贝粘贴
            for (int i = 0; i < datatable.Rows.Count - 1; i++)
            #region
            {
                //加入两段避免了最后一行文本被弄到下面
                Word.Paragraph para1;
                Word.Paragraph para2;
                para1 = mydoc.Content.Paragraphs.Add(ref missing);
                para2 = mydoc.Content.Paragraphs.Add(ref missing);
                object pBreak = (int)WdBreakType.wdSectionBreakNextPage;
                para2.Range.InsertBreak(ref pBreak);
                //调用粘贴方法即可粘贴剪贴板中的内容。
                para2.Range.Paste();
            }
            #endregion

            //遍历datatable,读取后备干部信息,将后备干部信息写入文档
            for (int i = 0; i < datatable.Rows.Count; i++)
            #region
            {
                //表一:干部考察对象
                mydoc.Tables[i * 9 + 1].Cell(1, 1).Range.Text = title;
                mydoc.Tables[i * 9 + 1].Cell(4, 1).Range.InsertAfter(datatable.Rows[i]["department"].ToString());
                mydoc.Tables[i * 9 + 1].Cell(5, 1).Range.InsertAfter(datatable.Rows[i]["name"].ToString());
                mydoc.Tables[i * 9 + 1].Cell(6, 1).Range.InsertAfter(datatable.Rows[i]["position"].ToString());

                //表二:家庭主要成员及重要社会关系
                #region
                //得到该干部的家庭信息
                DataTable datatable2 = dataOp.GetOneDataTable_sql("select * from TB_Family where cid ='" + datatable.Rows[i]["CID"].ToString() + "'");
                int       n          = datatable2.Rows.Count;
                for (int j = 0; j < n; j++)
                {
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 1).Range.Text = datatable2.Rows[j]["relationship"].ToString();
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 2).Range.Text = datatable2.Rows[j]["name"].ToString();
                    string age = datatable2.Rows[j]["birthday"].ToString().Replace("年", ".");
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 3).Range.Text = age.Replace("月", "");
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 4).Range.Text = datatable2.Rows[j]["country"].ToString();
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 5).Range.Text = datatable2.Rows[j]["party"].ToString();
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 6).Range.Text = datatable2.Rows[j]["nation"].ToString();
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 7).Range.Text = datatable2.Rows[j]["deptJob"].ToString();
                    mydoc.Tables[i * 9 + 2].Cell(j + 2, 8).Range.Text = datatable2.Rows[j]["remark"].ToString();
                }
                #endregion

                datatable2.Clear();
                //表三:海外学习
                #region
                datatable2 = dataOp.GetOneDataTable_sql("select * from TB_SAbroad where cid ='" + datatable.Rows[i]["CID"].ToString() + "'");
                n          = datatable2.Rows.Count;
                for (int j = 0; j < n; j++)
                {
                    mydoc.Tables[i * 9 + 3].Cell(j + 2, 1).Range.Text = datatable2.Rows[j]["startTime"].ToString();
                    mydoc.Tables[i * 9 + 3].Cell(j + 2, 2).Range.Text = datatable2.Rows[j]["endTime"].ToString();
                    mydoc.Tables[i * 9 + 3].Cell(j + 2, 3).Range.Text = datatable2.Rows[j]["country"].ToString();
                    mydoc.Tables[i * 9 + 3].Cell(j + 2, 4).Range.Text = datatable2.Rows[j]["academy"].ToString();
                    mydoc.Tables[i * 9 + 3].Cell(j + 2, 5).Range.Text = datatable2.Rows[j]["degree"].ToString();
                }
                #endregion

                datatable2.Clear();
                //表四:海外工作
                #region
                datatable2 = dataOp.GetOneDataTable_sql("select * from TB_WAbroad where cid ='" + datatable.Rows[i]["CID"].ToString() + "'");
                n          = datatable2.Rows.Count;
                for (int j = 0; j < n; j++)
                {
                    mydoc.Tables[i * 9 + 4].Cell(j + 2, 1).Range.Text = datatable2.Rows[j]["startTime"].ToString();
                    mydoc.Tables[i * 9 + 4].Cell(j + 2, 2).Range.Text = datatable2.Rows[j]["endTime"].ToString();
                    mydoc.Tables[i * 9 + 4].Cell(j + 2, 3).Range.Text = datatable2.Rows[j]["abroadCountry"].ToString();
                    mydoc.Tables[i * 9 + 4].Cell(j + 2, 4).Range.Text = datatable2.Rows[j]["departmentPosition"].ToString();
                    mydoc.Tables[i * 9 + 4].Cell(j + 2, 5).Range.Text = datatable2.Rows[j]["specialtyArea"].ToString();
                }
                #endregion

                //重大事项信息
                #region
                datatable2 = dataOp.GetOneDataTable_sql("select * from TB_GreatContent where CID = '" + datatable.Rows[i]["CID"].ToString() + "'");
                for (int r = 0; r < datatable2.Rows.Count; r++)
                {
                    string connect = null;
                    //第五张表
                    #region
                    if (datatable2.Rows[r]["matter"].ToString().Equals("1"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 5].Cell(2, 2).Range.Text = connect;
                        continue;
                    }
                    else if (datatable2.Rows[r]["matter"].ToString().Equals("2"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 5].Cell(3, 2).Range.Text = connect;
                        continue;
                    }
                    else if (datatable2.Rows[r]["matter"].ToString().Equals("3"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 5].Cell(4, 2).Range.Text = connect;
                        continue;
                    }
                    else if (datatable2.Rows[r]["matter"].ToString().Equals("4"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 5].Cell(5, 2).Range.Text = connect;
                        continue;
                    }
                    else if (datatable2.Rows[r]["matter"].ToString().Equals("5"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 5].Cell(6, 2).Range.Text = connect;
                        continue;
                    }
                    #endregion
                    //第六张表
                    #region
                    else if (datatable2.Rows[r]["matter"].ToString().Equals("6"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 6].Cell(2, 2).Range.Text = connect;
                        continue;
                    }

                    else if (datatable2.Rows[r]["matter"].ToString().Equals("7"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 6].Cell(3, 2).Range.Text = connect;
                        continue;
                    }
                    else if (datatable2.Rows[r]["matter"].ToString().Equals("8"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 6].Cell(4, 2).Range.Text = connect;
                        continue;
                    }
                    else if (datatable2.Rows[r]["matter"].ToString().Equals("9"))
                    {
                        connect = datatable2.Rows[r]["content"].ToString();
                        if (connect == null || connect.Equals(""))
                        {
                            connect = "无";
                        }
                        mydoc.Tables[i * 9 + 6].Cell(5, 2).Range.Text = connect;
                        continue;
                    }
                    #endregion
                }
                #endregion


                datatable2.Clear();
                //表七:熟悉外语语种
                #region
                datatable2 = dataOp.GetOneDataTable_sql("select * from TB_FamiliarForeign where cid ='" + datatable.Rows[i]["CID"].ToString() + "'");
                n          = datatable2.Rows.Count;
                for (int j = 0; j < n; j++)
                {
                    string level = datatable2.Rows[j]["level"].ToString();
                    mydoc.Tables[i * 9 + 7].Cell(j + 3, 2).Range.Text = datatable2.Rows[j]["foreignKind"].ToString();

                    if (level.Equals("精通"))
                    {
                        mydoc.Tables[i * 9 + 7].Cell(j + 3, 3).Range.Text = "√";
                    }
                    else if (level.Equals("熟练"))
                    {
                        mydoc.Tables[i * 9 + 7].Cell(j + 3, 4).Range.Text = "√";
                    }
                    else if (level.Equals("良好"))
                    {
                        mydoc.Tables[i * 9 + 7].Cell(j + 3, 5).Range.Text = "√";
                    }
                    else if (level.Equals("一般"))
                    {
                        mydoc.Tables[i * 9 + 7].Cell(j + 3, 6).Range.Text = "√";
                    }
                }
                #endregion

                datatable2.Clear();
                //表八:参加培训及实践锻炼情况
                #region
                datatable2 = dataOp.GetOneDataTable_sql("select * from TB_TrainExercise where cid ='" + datatable.Rows[i]["CID"].ToString() + "'");

                for (int r = 0; r < datatable2.Rows.Count; r++)
                {
                    string starttime = datatable2.Rows[r]["startTime"].ToString().Replace("年", ".");
                    string endtime   = datatable2.Rows[r]["endTime"].ToString().Replace("年", ".");
                    if (datatable2.Rows[r]["reportMatter"].ToString().Equals("参加培训情况"))
                    {
                        mydoc.Tables[i * 9 + 8].Cell(2, 2).Range.InsertAfter(starttime.Replace("月", ".").Replace("日", "") + "到" + endtime.Replace("月", ".").Replace("日", "") + ":" + datatable2.Rows[r]["Content"].ToString() + "\n");
                    }
                    if (datatable2.Rows[r]["reportMatter"].ToString().Equals("参加实践锻炼情况"))
                    {
                        mydoc.Tables[i * 9 + 8].Cell(3, 2).Range.InsertAfter(starttime.Replace("月", ".").Replace("日", "") + "到" + endtime.Replace("月", ".").Replace("日", "") + ":" + datatable2.Rows[r]["Content"].ToString() + "\n");
                    }
                }
                #endregion

                datatable2.Clear();
                //表九:培养锻炼措施需求
                datatable2 = dataOp.GetOneDataTable_sql("select * from TB_TrainMethord where cid ='" + datatable.Rows[i]["CID"].ToString() + "'");

                for (int r = 0; r < datatable2.Rows.Count; r++)
                {
                    #region
                    string select = datatable2.Rows[r]["options"].ToString();
                    if (select.Equals("1"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(1, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("2"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(2, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("3"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(3, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("4"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(4, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("5"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(5, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("6"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(6, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("7"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(7, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("8"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(8, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("9"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(9, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("10"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(10, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("11"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(11, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("12"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(12, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("13"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(13, 2).Range.Text = "√"; continue;
                    }
                    else if (select.Equals("14"))
                    {
                        mydoc.Tables[i * 9 + 9].Cell(14, 2).Range.Text = "√";
                        mydoc.Tables[i * 9 + 9].Cell(15, 1).Range.Text = datatable2.Rows[r]["note14"].ToString();
                        continue;
                    }
                    #endregion
                }
                datatable2.Clear();
            }
            #endregion
            //#region
            object path = savepath;
            //wordappliction.Documents.Save(path);
            object myobj = System.Reflection.Missing.Value;;
            mydoc.SaveAs(ref path, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                         ref myobj, ref myobj, ref myobj, ref myobj);
            //关闭退出文档
            #region
            //关闭文档
            mydoc.Close(ref myobj, ref myobj, ref myobj);
            //退出应用程序。
            wordappliction.Quit();
            #endregion
            MessageBox.Show("导出成功!");
        }
        /// <summary>
        /// 导出初步人选名册
        /// </summary>
        /// <param name="idlist">传递所选干部的所有id</param>
        public void exportword(ArrayList idlist)
        {
            #region
            //选择保存路径
            #region
            string         savepath = "";
            SaveFileDialog sa       = new SaveFileDialog();
            sa.Filter   = "Document(*.doc)|*.doc";
            sa.FileName = "初步人选名册";
            if (sa.ShowDialog() == DialogResult.OK)
            {
                savepath = sa.FileName;
            }
            else
            {
                return;
            }
            #endregion

            //创建word应用程序
            wordappliction = new Word.Application();
            //打开模板
            object filepath = System.Windows.Forms.Application.StartupPath + "\\wordModel" + "\\花名册.doc";

            Word.Document mydoc = wordappliction.Documents.Open(ref filepath, ref missing, ref readOnly,
                                                                ref missing, ref missing, ref missing, ref missing, ref missing,
                                                                ref missing, ref missing, ref missing, ref isVisible, ref missing,
                                                                ref missing, ref missing, ref missing);
            //打开模板复制其中的表
            #endregion

            //通过idlist读取所选后备干部的信息,用datatable存在内存中。
            string selectid = "";

            for (int i = 0; i < idlist.Count; i++)
            {
                if (i == idlist.Count - 1)
                {
                    selectid = selectid + "'" + idlist[i] + "'";
                }
                else
                {
                    selectid = selectid + "'" + idlist[i] + "',";
                }
            }

            string sql = "select name,cid,sex,nation,department,position,native,birthday,age,partyTime,workTime,fullEducation,fullDegree,fullSchool,fullSpecialty,workEducation,workDegree,workGraduate,workSpecialty,technicalPost,joinTeam,experiencePost,knowField,trainDirection,trainMeasure,partyClass from TB_CommonInfo order by rank,joinTeam desc";

            string        cond       = "cid in (" + selectid + ")";
            DataOperation dataOp     = new DataOperation();
            DataTable     datatableT = dataOp.GetOneDataTable_sql(sql);
            DataTable     datatable  = new DataTable();
            datatable = datatableT.Clone();
            DataView dv = datatableT.AsDataView();
            dv.RowFilter = cond;
            datatable    = dv.ToTable();
            //确定表头的单位信息
            string title = null;

            if (this.Unitclass.Equals("省直单位"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = this.Unit + "正厅级";
                }
                else if (this.Qd.Equals("副职"))
                {
                    title = this.Unit + "副厅级";
                }
            }
            else if (this.Unitclass.Equals("省辖市"))
            {
                title = this.Unit + "党政" + this.Qd;
            }
            else if (this.Unitclass.Equals("省管高校"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = this.Unit + "正校级";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = this.Unit + "副校级";
                }
            }
            else if (this.Unitclass.Equals("省管企业"))
            {
                title = this.Unit + "领导班子" + this.Qd;
            }//县处级
            else if (this.Unitclass.Equals("市直单位"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = this.Unit + "正县级";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = this.Unit + "副县级";
                }
            }
            else if (this.Unitclass.Equals("市管企业"))
            {
                title = this.Unit + "领导班子" + this.Qd;
            }
            else if (this.Unitclass.Equals("市管学校"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = this.Unit + "正校级";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = this.Unit + "副校级";
                }
            }
            else if (this.Unitclass.Equals("县(市、区)"))
            {
                title = this.Unit + "党政" + this.Qd;
            }//乡科级
            else if (this.Unitclass.Equals("县(市、区)直"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = this.Unit + "正科级";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = this.Unit + "副科级";
                }
            }
            else if (this.Unitclass.Equals("县管学校"))
            {
                if (this.Qd.Equals("正职"))
                {
                    title = this.Unit + "正校级";
                }

                else if (this.Qd.Equals("副职"))
                {
                    title = this.Unit + "副校级";
                }
            }
            else if (this.Unitclass.Equals("县管企业"))
            {
                title = this.Unit + "领导班子" + this.Qd;
            }
            else if (this.Unitclass.Equals("乡(镇、街道)"))
            {
                title = this.Unit + "党政" + this.Qd;
            }
            mydoc.Tables[1].Cell(1, 1).Range.Text = title;
            mydoc.Tables[3].Cell(1, 1).Range.Text = title + "后备干部初步人选名册";
            #region
            mydoc.ActiveWindow.Selection.WholeStory();
            mydoc.ActiveWindow.Selection.Tables[4].Select();
            mydoc.ActiveWindow.Selection.Copy();
            #endregion

            //添加前面的说明信息
            //以下几个变量用于后备干部信息的统计
            int    allcount       = 0;  //统计总人数
            int    mancount       = 0;  //记录男总人数
            int    womencoumt     = 0;  //记录女总人数
            int    unpartyount    = 0;  //记录非中共党员人数
            int    fewnationcount = 0;  //记录少数民族人数
            int    age40          = 0;  //记录40岁以下人数
            int    age41_45       = 0;  //记录41到45岁人数
            int    age46_50       = 0;  //记录46到50岁人数
            int    age51          = 0;  //记录51岁以上人数
            int    doctorgraduate = 0;  //记录博士研究生人数
            int    mastergraduate = 0;  //记录硕士研究生人数
            int    graduate       = 0;  //记录大学生人数
            string aveage         = ""; //记录平均年龄


            //所用到的一切信息统计结果
            #region
            //总人数
            allcount = datatable.Rows.Count;
            //男人数
            DataTable datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and sex = '男'");
            mancount = datatable1.Rows.Count;
            //女人数
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and sex = '女'");
            womencoumt = datatable1.Rows.Count;
            //非中共
            datatable1  = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and partyClass = '中共'");
            unpartyount = allcount - datatable1.Rows.Count;
            //少数民族
            datatable1     = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and nation like '%汉%'");
            fewnationcount = allcount - datatable1.Rows.Count;
            //40岁及40岁以下
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and age <= 40");
            age40      = datatable1.Rows.Count;
            //41岁到45岁
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and age > 40 and age <=45");
            age41_45   = datatable1.Rows.Count;
            //46岁到50岁
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and age > 45 and age <=50");
            age46_50   = datatable1.Rows.Count;
            //51岁以上
            datatable1.Clear();
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and age > 50");
            age51      = datatable1.Rows.Count;
            //平均年龄
            datatable1 = dataOp.GetOneDataTable_sql("select AVG(age) as aveage from TB_CommonInfo where cid in (" + selectid + ")");
            aveage     = datatable1.Rows[0]["aveage"].ToString();
            //平均年龄计算结果可能为空
            if (aveage.Equals("") || aveage == null)
            {
                aveage = "0";
            }
            //博士
            datatable1     = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and ((fullDegree = '博士' and (fullEducation='研究生' or fullEducation='党校研究生')) or (workDegree='博士' and (workEducation='研究生' or workEducation='党校研究生')))");
            doctorgraduate = datatable1.Rows.Count;
            String doctor = "";
            for (int i = 0; i < datatable1.Rows.Count; i++)
            {
                if (i == datatable1.Rows.Count - 1)
                {
                    doctor = doctor + "'" + datatable1.Rows[i][0] + "'";
                }
                else
                {
                    doctor = doctor + "'" + datatable1.Rows[i][0] + "',";
                }
            }
            //硕士
            datatable1     = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and cid not in(" + doctor + ") and ((fullDegree = '硕士' and (fullEducation='研究生' or fullEducation='党校研究生')) or (workDegree='硕士' and (workEducation='研究生' or workEducation='党校研究生')))");
            mastergraduate = datatable1.Rows.Count;
            String master = "";
            for (int i = 0; i < datatable1.Rows.Count; i++)
            {
                if (i == datatable1.Rows.Count - 1)
                {
                    master = master + "'" + datatable1.Rows[i][0] + "'";
                }
                else
                {
                    master = master + "'" + datatable1.Rows[i][0] + "',";
                }
            }
            //学士
            datatable1 = dataOp.GetOneDataTable_sql("select CID from TB_CommonInfo where cid in (" + selectid + ") and cid not in (" + doctor + ") and cid not in (" + master + ") and ((fullDegree = '学士' and (fullEducation='大学本科' or fullEducation='党校大学')) or (workDegree='学士' and (workEducation='大学本科' or workEducation='党校大学')))");
            graduate   = datatable1.Rows.Count;
            #endregion

            //表头和说明信息
            #region
            string explain = "    " + this.Unit + "党政" + this.Qd + "后备干部初步人选共" + allcount + "人,其中:男" + mancount + "人,女" + womencoumt + "人,非中共党员干部" + unpartyount + "人,少数民族干部" + fewnationcount + "人,40岁及以下" + age40 + "人,41-45岁" + age41_45 + "人,46-50岁" + age46_50 + "人,51岁以上" + age51 + "人,平均年龄" + Math.Round(Convert.ToDouble(aveage), 2) + "岁,博士研究生" + doctorgraduate + "人,硕士研究生" + mastergraduate + "人,大学" + graduate + "人。";

            #endregion
            mydoc.Tables[2].Range.Text = explain;



            int tablecount1 = 0;
            if (datatable.Rows.Count % pagecount == 0)
            {
                tablecount1 = datatable.Rows.Count / pagecount - 1;
            }
            else
            {
                tablecount1 = datatable.Rows.Count / pagecount;
            }

            #region

            for (int i = 0; i < tablecount1; i++)
            {
                //第二页的前两行
                Word.Paragraph para1 = mydoc.Content.Paragraphs.Add(ref missing);
                Word.Paragraph para2 = mydoc.Content.Paragraphs.Add(ref missing);
                Word.Paragraph para3 = mydoc.Content.Paragraphs.Add(ref missing);
                //在此粘贴复制的表格
                //控制位置
                object pBreak = (int)WdBreakType.wdSectionBreakNextPage;
                para1.Range.InsertBreak(ref pBreak);
                para3.Range.Paste();
                if (i == tablecount1 - 1)
                {
                    para3.Range.Text = " 注:后备干部人选是民主党派成员的,在“入党时间”栏填写党派名称及加入时间;无党派人士,此栏不填。";
                }
            }
            if (tablecount1 == 0)
            {
                Word.Paragraph para1 = mydoc.Content.Paragraphs.Add(ref missing);
                para1.Range.Text = " 注:后备干部人选是民主党派成员的,在“入党时间”栏填写党派名称及加入时间;无党派人士,此栏不填。";
            }
            #endregion

            for (int i = 0; i < datatable.Rows.Count; i++)
            {
                int tableindex = i / pagecount + 4;
                int rowindex   = i % pagecount + 3;

                string strS = "", strN = "";
                string Remarks = "";
                if (datatable.Rows[i]["sex"].ToString().Trim() != "男")
                {
                    strS = datatable.Rows[i]["sex"].ToString().Trim();
                }
                if (datatable.Rows[i]["nation"].ToString().Trim() != "汉族")
                {
                    strN = datatable.Rows[i]["nation"].ToString().Trim();
                }
                if (strS != "" && strN == "")
                {
                    Remarks = "(" + strS + ")";
                }
                else
                if (strS != "" && strN != "")
                {
                    Remarks = "(" + strS + "、" + strN + ")";
                }
                else
                if (strS == "" && strN != "")
                {
                    Remarks = "(" + strN + ")";
                }
                mydoc.Tables[tableindex].Cell(rowindex, 1).Select();
                mydoc.Tables[tableindex].Cell(rowindex, 1).Range.Text = Remarks;
                //在选中的位置添加文本
                string strna = datatable.Rows[i]["name"].ToString() + "\n";
                wordappliction.Selection.Font.Name = "宋体 ";
                wordappliction.Selection.Font.Size = 10.5f;
                wordappliction.Selection.TypeText(strna);
                try
                {
                    mydoc.Tables[tableindex].Cell(rowindex, 2).Range.Text = datatable.Rows[i]["department"].ToString() + "\n" + datatable.Rows[i]["position"].ToString();         //"工作单位及职务";
                    mydoc.Tables[tableindex].Cell(rowindex, 3).Range.Text = datatable.Rows[i]["native"].ToString();                                                               //"籍贯";
                    string age = datatable.Rows[i]["birthday"].ToString().Replace("年", ".");
                    mydoc.Tables[tableindex].Cell(rowindex, 4).Range.Text = age.Replace("月", "");                                                                                 //"出生年月";
                    string partytime = datatable.Rows[i]["partyTime"].ToString().Replace("年", ".");
                    mydoc.Tables[tableindex].Cell(rowindex, 5).Range.Text = partytime.Replace("月", "");                                                                           //"入党时间";
                    string worktime = datatable.Rows[i]["workTime"].ToString().Replace("年", ".");
                    mydoc.Tables[tableindex].Cell(rowindex, 6).Range.Text  = worktime.Replace("月", "");;                                                                          //"参加工作时间";
                    mydoc.Tables[tableindex].Cell(rowindex, 7).Range.Text  = datatable.Rows[i]["fullEducation"].ToString() + "\n" + datatable.Rows[i]["fullDegree"].ToString();   //"学历学位";
                    mydoc.Tables[tableindex].Cell(rowindex, 8).Range.Text  = datatable.Rows[i]["fullSchool"].ToString() + "\n" + datatable.Rows[i]["fullSpecialty"].ToString();   // "毕业院校及专业";
                    mydoc.Tables[tableindex].Cell(rowindex, 9).Range.Text  = datatable.Rows[i]["workEducation"].ToString() + "\n" + datatable.Rows[i]["workDegree"].ToString();   //"学历学位";
                    mydoc.Tables[tableindex].Cell(rowindex, 10).Range.Text = datatable.Rows[i]["workGraduate"].ToString() + "\n" + datatable.Rows[i]["workSpecialty"].ToString(); //"毕业院校及专业";
                    mydoc.Tables[tableindex].Cell(rowindex, 11).Range.Text = datatable.Rows[i]["technicalPost"].ToString();                                                       //"专业技术职务";
                    mydoc.Tables[tableindex].Cell(rowindex, 12).Range.Text = datatable.Rows[i]["experiencePost"].ToString();                                                      //"历任主要职务";
                    mydoc.Tables[tableindex].Cell(rowindex, 13).Range.Text = datatable.Rows[i]["knowField"].ToString();                                                           //"熟悉领域";
                    mydoc.Tables[tableindex].Cell(rowindex, 14).Range.Text = datatable.Rows[i]["trainDirection"].ToString();                                                      //"培养方向";
                    mydoc.Tables[tableindex].Cell(rowindex, 15).Range.Text = datatable.Rows[i]["trainMeasure"].ToString();                                                        //"培养措施";
                }
                catch (Exception ex)
                {
                    MessageBox.Show("导出失败,请重新操作!" + ex.Message);
                    return;
                }
            }
            try
            {
                object path = savepath;
                //wordappliction.Documents.Save(path);
                object myobj = System.Reflection.Missing.Value;;
                mydoc.SaveAs(ref path, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                             ref myobj, ref myobj, ref myobj, ref myobj, ref myobj, ref myobj,
                             ref myobj, ref myobj, ref myobj, ref myobj);

                //关闭文档
                mydoc.Close(ref myobj, ref myobj, ref myobj);
                //退出应用程序。
                wordappliction.Quit();

                MessageBox.Show("导出成功!");
            }
            catch (Exception ex) {
                MessageBox.Show(ex.Message);
            }
        }