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();
  • 相关阅读:
    winfrom 正则验证
    winfrom api发送消息,实现滚动效果(重绘滚动条用)
    C#程序员开发WinForm必须知道的 Window 消息大全
    C# VS EditorBrowsable特性 控制智能提示
    C# Attribute简介
    Win32窗口鼠标消息
    C# 自定义控件容器,设计时可添加控件
    Linux管线命令
    Linux数据流重定向
    Shell中的变量
  • 原文地址:https://www.cnblogs.com/StevenFu/p/2858421.html
Copyright © 2011-2022 走看看