Kim_zh

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

1、DataGrid的button属性设置

    CommandName="ToEdit":

      对其中的button按钮进行选择:

    CommandArgument='<%#Eval("ID") %>':

      可以在后台的DataGrid1_ItemCommand1中获取当前按下的button按钮所在行的ID:

      string keyId = e.CommandArgument.ToString();

  也可以直接在DataGrid属性里配置ID:

    DataKeyField="ID":

      string keyId = DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); 

2、DataGrid中的button,实现导出word文档

  如:

     if (e.CommandName.ToLower() == "toword") //导出word
            {
                //导出word
                string ID = e.CommandArgument.ToString();
                object missing = System.Reflection.Missing.Value;
                string FileName = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath);
                //string aa = ;
                string TemplateFile = FileName + @"Template\文件名字.doc";//带有格式的空的“文件名字.doc”文档
                FileName += @"FckUploadfile\" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc";
                File.Copy(TemplateFile, FileName);
                FileInfo fi = new FileInfo(FileName);
                //判断文件属性是否只读?是则修改为一般属性再保存
                if (fi.Attributes.ToString().IndexOf("ReadOnly") != -1)
                {
                    fi.Attributes = FileAttributes.Normal;
                }
                Microsoft.Office.Interop.Word.Application App = new Microsoft.Office.Interop.Word.Application();
                try
                {
                    object Obj_FileName = FileName;
                    object Visible = false;
                    object ReadOnly = false;


                    Microsoft.Office.Interop.Word.Document Doc = App.Documents.Open(ref Obj_FileName, ref missing, ref ReadOnly, ref missing,
                        ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref Visible,
                        ref missing, ref missing, ref missing,
                        ref missing);
                    Doc.Activate();

                    // Get the data and fill the data( PersonName ,PersonResume) to the appoint BookMark
                    DataRow row = AccountRule.导出试用期考核表信息获取(ID).Rows[0];

                    object BookMarkName = "姓名";
                    object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToBookmark;
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["姓名"].ToString());

                    BookMarkName = "性别";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["姓名"].ToString());

                    BookMarkName = "出生年月";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["出生年月"].ToString());

                    BookMarkName = "政治面貌";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["政治面貌"].ToString());

                    BookMarkName = "单位";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["单位"].ToString());

                    BookMarkName = "从事工作";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["从事工作"].ToString());

                    BookMarkName = "学历学位";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["学历"].ToString() + "\r\n" + row["学位"].ToString());
                    Doc.ActiveWindow.Selection.MoveDown(Microsoft.Office.Interop.Word.WdUnits.wdLine, 6);

                    BookMarkName = "试用期起止时间";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(row["试用期起止时间"].ToString());

                    BookMarkName = "个人总结";
                    Doc.ActiveWindow.Selection.GoTo(ref what, ref missing, ref missing, ref BookMarkName);
                    Doc.ActiveWindow.Selection.TypeText(Convert.IsDBNull(row["个人总结"]) ? string.Empty : row["个人总结"].ToString());
                    File.SetAttributes(FileName, FileAttributes.Normal);

                    Doc.Save();
                    // Save the File  and change the File as stream
                    object IsSave = true;

                    Doc.Close(ref IsSave, ref missing, ref missing);
                    Get(FileName);
                }
                finally
                {

                    App.Quit(ref missing, ref missing, ref missing);
                }



                //string url = Request.ApplicationPath + @"/qgy/dybg_File.aspx?KeyId=" + keyId;
                //Response.Write("<script>window.open('" + url + "','','target=_blank,scrollbars=yes,top=100,left=200,width=600,height=300,scrolling=1');</script>");
            }
        }

        public void Get(string fileName)
        {
            string paths = Request.MapPath("../FckUploadfile");
            string file = Path.Combine(paths, fileName);
            FileInfo fi = new FileInfo(file);

            if (fi.Exists == true)
            {

                // const long ChunkSize = 1024;

                // byte[] buffer = new byte[ChunkSize];

                //Response.Clear();
                FileStream istream = File.OpenRead(file);
                try
                {

                    byte[] buffer = new byte[istream.Length];

                    istream.Read(buffer, 0, buffer.Length);
                    istream.Seek(0, SeekOrigin.Begin);
                    Response.BinaryWrite(buffer);
                    //long Data = istream.Length;

                    Response.ContentType = "application/octet-stream";
                    Response.AddHeader("Content-Disposition", "attachment;filename=" + Server.UrlEncode(file.Substring(file.LastIndexOf("\\") + 1)));
                }
                finally
                {

                }
            }
        }    

posted on 2013-09-29 15:56  Kim.zh  阅读(209)  评论(0编辑  收藏  举报