zoukankan      html  css  js  c++  java
  • 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();
  • 相关阅读:
    filterFilter用法
    angular.copy()克隆数据
    angularjs中是否选择所有和$filter过滤orderBy排序
    qt5.5 qtcreator中文乱码
    shared_ptr
    Thrift-0.9.2编译安装
    一行代码获取通讯录联系框架
    IOS枚举使用
    Static Cell-静态TableView
    NavigationController的使用整理
  • 原文地址:https://www.cnblogs.com/StevenFu/p/2858421.html
Copyright © 2011-2022 走看看