asp.net Microsoft.Office.Interop.Word 导出word模版-合并单元格

 public void ExportToWord(int iRecordID)
        {
            DataTable dtResult = dbo.Query("usp_get_Entity_FPBKSQ " + iRecordID + ",0");
            //取得报表模板文件路径
            string wordTemplatePath = HttpContext.Current.Server.MapPath("..//..//..//Instance//Plugin//Biz//template/bfjzsq.doc");
            //导出报表文件名
            string fileName = "西北化工销售公司困难人员帮扶救助申请表"+ System.DateTime.Now.ToString("yyyMMddHHmmss") +".doc";
            //导出文件路径
            string outPutFilePath = HttpContext.Current.Server.MapPath("..//..//..//Instance//Plugin//Biz//template//Temp/" + fileName);
            //创建word对象
            object missing = System.Reflection.Missing.Value;
            Microsoft.Office.Interop.Word.Application app = new Microsoft.Office.Interop.Word.Application();
            Microsoft.Office.Interop.Word.Document doc = new Microsoft.Office.Interop.Word.Document();
            //Aspose.Word.Word app = new  Aspose.Word.Word();
            //Aspose.Word.Document doc = null;   
            File.Copy(wordTemplatePath, outPutFilePath,true);//true 相同文件名时,覆盖
            //打开doc
            object output = (object)outPutFilePath;
            doc = app.Documents.Open(ref output, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);
            doc.Activate();
            //app.SetLicenseCOM(AppDomain.CurrentDomain.BaseDirectory + "Aspose.Word.lic");
            //doc = app.Open(outPutFilePath);
            //Aspose.Word.DocumentBuilder docBuild = new Aspose.Word.DocumentBuilder(doc);
            //docBuild.MoveToBookmark("姓名");
            //docBuild.InsertField("asdf", "adf");
            //docBuild.MoveToCell(0, 4, 0, 0);


            //docBuild.InsertCell();
            //docBuild.InsertCell();
            //docBuild.InsertCell();
            //docBuild.InsertCell();
            foreach (Microsoft.Office.Interop.Word.Bookmark mark in doc.Bookmarks)
            {
                if (mark.Name == "姓名")
                    mark.Range.Text = dtResult.Rows[0]["Name"].ToString();
                else if (mark.Name == "性别")
                    mark.Range.Text = dtResult.Rows[0]["xb"].ToString();
                else if (mark.Name == "年龄")
                    mark.Range.Text = dtResult.Rows[0]["nl"].ToString();
                else if (mark.Name == "联系电话")
                    mark.Range.Text = dtResult.Rows[0]["LXDH"].ToString();
                else if (mark.Name == "政治面貌")
                    mark.Range.Text = dtResult.Rows[0]["zzmm"].ToString();
                else if (mark.Name == "婚姻状况")
                    mark.Range.Text = dtResult.Rows[0]["HYZK"].ToString();
                else if (mark.Name == "家庭详细住址")
                    mark.Range.Text = dtResult.Rows[0]["JTDZ"].ToString();
                else if (mark.Name == "本人月收入")
                    mark.Range.Text = dtResult.Rows[0]["YSR"].ToString();
                else if (mark.Name == "家庭人口数")
                    mark.Range.Text = dtResult.Rows[0]["ZRS"].ToString();
                else if (mark.Name == "月总收入")
                    mark.Range.Text = dtResult.Rows[0]["YZSR"].ToString();
                else if (mark.Name == "月人均生活费")
                    mark.Range.Text = dtResult.Rows[0]["YRJSHF"].ToString();
                else if (mark.Name == "人员类别")
                    mark.Range.Text = dtResult.Rows[0]["RYLB"].ToString();
                else if (mark.Name == "二级单位审核意见")
                    mark.Range.Text = dtResult.Rows[0]["EJDWSHYJ"].ToString();
                else if (mark.Name == "公司工会审核意见")
                    mark.Range.Text = dtResult.Rows[0]["GSGHSHYJ"].ToString();
                else if (mark.Name == "配偶姓名")
                    mark.Range.Text = dtResult.Rows[0]["PONAME"].ToString();
                else if (mark.Name == "工作单位")
                    mark.Range.Text = dtResult.Rows[0]["POGZDW"].ToString();
                else if (mark.Name == "月收入")
                    mark.Range.Text = dtResult.Rows[0]["POYSR"].ToString();
                else if (mark.Name == "身份")
                    mark.Range.Text = dtResult.Rows[0]["sf"].ToString();
                else if (mark.Name == "所属单位")
                    mark.Range.Text = dtResult.Rows[0]["OrgName"].ToString();
            }
            using (DataTable dt = dbo.Query("usp_get_Entity_FPBKSQ " + iRecordID + ",1"))
            {
                int i = 5;
                if (dt.Rows.Count > 0)
                {
                    for (int j = 1; j < dt.Rows.Count; j++)
                    {
                        DataRow dr = dt.Rows[j - 1];
                        Microsoft.Office.Interop.Word.Row beforeRow = doc.Tables[1].Rows[i++];
                        //复制上行
                        object before = (object)beforeRow;
                        Microsoft.Office.Interop.Word.Row newRow = doc.Tables[1].Rows.Add(ref before);
                        //修改上行内容
                        newRow.Cells[2].Range.Text = dr["KIDNAME"].ToString();
                        newRow.Cells[3].Range.Text = beforeRow.Cells[3].Range.Text;
                        newRow.Cells[4].Range.Text = dr["xb"].ToString();
                        newRow.Cells[5].Range.Text = beforeRow.Cells[5].Range.Text;
                        newRow.Cells[6].Range.Text = dr["nl"].ToString();
                        newRow.Cells[7].Range.Text = beforeRow.Cells[7].Range.Text;
                        newRow.Cells[8].Range.Text = dr["xz"].ToString();
                        //设置合并内容
                        //newRow.Cells[1].Range.Text = "";
                    }


                    doc.Tables[1].Rows[i].Cells[2].Range.Text = dt.Rows[dt.Rows.Count - 1]["KIDNAME"].ToString();
                    doc.Tables[1].Rows[i].Cells[4].Range.Text = dt.Rows[dt.Rows.Count - 1]["xb"].ToString();
                    doc.Tables[1].Rows[i].Cells[6].Range.Text = dt.Rows[dt.Rows.Count - 1]["nl"].ToString();
                    doc.Tables[1].Rows[i].Cells[8].Range.Text = dt.Rows[dt.Rows.Count - 1]["xz"].ToString();
                    //合并从第5行开始的 “子女姓名”
                    doc.Tables[1].Rows[5].Cells[1].Merge(doc.Tables[1].Rows[i].Cells[1]);

                }
            }            
            //doc.Save(fileName);
            doc.Save();
            object IsSave = true;
            doc.Close(ref IsSave, ref missing, ref missing);//关闭word
            app.Quit(ref IsSave, ref missing, ref missing);
            System.Runtime.InteropServices.Marshal.ReleaseComObject(app); //释放内存
            string path = outPutFilePath;
            System.IO.FileInfo file = new System.IO.FileInfo(path);
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.Charset = "GB2312";
            HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
            // 添加头信息,为"文件下载/另存为"对话框指定默认文件名  
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename = " + HttpContext.Current.Server.UrlEncode(fileName));
            // 添加头信息,指定文件大小,让浏览器能够显示下载进度  
            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            // 指定返回的是一个不能被客户端读取的流,必须被下载  
            HttpContext.Current.Response.ContentType = "application/ms-word";
            // 把文件流发送到客户端  
            HttpContext.Current.Response.WriteFile(file.FullName);
            // 停止页面的执行  
            System.IO.DirectoryInfo pathFolder = new System.IO.DirectoryInfo(HttpContext.Current.Server.MapPath("..//Biz//template//Temp"));
            DeleteExcel(pathFolder, fileName);//删除历史文件            
            HttpContext.Current.Response.End();


            //Response.Write(" <script> window.close(); </script> ");
        }
        //删除历史下载的临时excel
        private void DeleteExcel(System.IO.DirectoryInfo path, string downpath)
        {
            foreach (System.IO.FileInfo f in path.GetFiles())
            {
                if (f.Name != downpath && f.Name.Length > 19 && f.Name.Substring(0, 19) == "西北化工销售公司困难人员帮扶救助申请表")
                {
                    f.Delete();
                }
            }
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值