c#导出excel

 function Export() {
            var dt = document.getElementById("<%=pdgProject.ClientID %>");

            var sheet = new ActiveXObject("OWC.Spreadsheet");
            alert(sheet);
            with (sheet) {
                DataType = "HTMLData";
                HTMLData = dt.outerHTML;
                try {
                    ActiveSheet.Export("e:\\aa.xls", 0);
                    alert("导出完毕");
                }
                catch (e) {
                }
            }
        }
 <asp:Button ID="btnExport" runat="server" Text="导出" OnClick="btnExport_Click" OnClientClick="return Export();" />

  protected void btnExport_Click(object sender, EventArgs e)
        {
            DataGridBind();
            if (dsTemp.Tables[0].Rows.Count > 0)
            {
                StringWriter sw = new StringWriter();
                sw.WriteLine("项目编号\t项目名称\t主办部门\t项目类别\t项目设总A\t项目设总B\t启动日期\t项目完成日期\t项目状态");

                foreach (DataRow dr in dsTemp.Tables[0].Rows)
                {

                    sw.WriteLine(dr["ProjectPhaseCode"] + "\t" + dr["ProjectPhaseName"].ToString() + "\t" + dr["UndertakeDeptName"].ToString() + "\t" + dr["ProjectTypeName"] + "\t" + dr["DesignAName"] + "\t" + dr["DesignBName"] + "\t" + dr["ProjectStartDate"] + "\t" + dr["ProjectEndDate"] + "\t" + dr["ProjectState"]);

                }
                sw.Close();
                string fileName = string.Empty;

                fileName = System.DateTime.Now.ToString("yyyy-MM-dd") + string.Format("阶段项目信息.xls");


                Response.AddHeader("Content-Disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(fileName) + "\"");
                Response.ContentType = "application/vnd.ms-excel";
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

                Response.Write(sw);
                Response.End();
            }
        }


你可能感兴趣的:(c#导出excel)