ASP.NET导出TXT(注释部分为Excel需using Microsoft.Office.Interop.Excel;)

        StringBuilder sb = new StringBuilder();

            //Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
            //excel.Visible = false;
            //object ms = Type.Missing;
            //Microsoft.Office.Interop.Excel.Workbook wk = excel.Workbooks.Add(ms);
            //Microsoft.Office.Interop.Excel.Worksheet ws = wk.Worksheets[1] as Microsoft.Office.Interop.Excel.Worksheet;
            DataSet ds = DbHelperSQL.Query("select email from Newsletter group by email");
            //for (int i = 0; i < ds.Tables[0].Columns.Count; i++)
            //{
            //    ws.Cells[1, i + 1] = ds.Tables[0].Columns[i].ColumnName;
            //}

            for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
            {
                for (int j = 0; j < ds.Tables[0].Columns.Count; j++)
                {
                    // ws.Cells[i + 2, j + 1] = ds.Tables[0].Rows[i][j].ToString();
                    sb.Append(ds.Tables[0].Rows[i][j].ToString() + "\r\n");
                }
            }

            //if (System.IO.File.Exists(this.MapPath("\\emailinfo.xls")))
            //{
            //    System.IO.File.Delete(this.MapPath("\\emailinfo.xls"));
            //}
            //  wk.SaveAs(this.MapPath("\\emailinfo.xls"), ms, ms, ms, ms, ms, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlShared, ms, ms, ms, ms, ms);
            //excel.Quit(); 
            //this.ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "window.open('../emailinfo.xls');", true);



            //System.IO.File.WriteAllText(this.MapPath("\\emailinfo.txt"), sb.ToString(), System.Text.Encoding.ASCII);



            Page.Response.Clear();
            Page.Response.Buffer = true;
            Page.Response.Charset = "GB2312";
            Page.Response.AppendHeader("Content-Disposition", "attachment;filename=emailinfo.txt");
            Page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
            Response.ContentType = "text/plain";//设置输出文件类型为txt文件。 
            this.EnableViewState = false;
            System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
            Page.Response.Write(sb);
            Page.Response.End();
原文地址:https://www.cnblogs.com/StevenFu/p/2858421.html