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();
  • 相关阅读:
    Git更新或提交出错的解决办法
    webpack简单学习的入门教程
    CentOS源码安装QT
    后台程序在向tty/串口写数据的时候stop了
    Linux signal 处理
    Linux C 获取 文件的大小
    Microsoft Excel 标题栏或首行锁定
    Socket连接何时需要断开
    Windows MFC 打开文本
    动态库的生产和调用
  • 原文地址:https://www.cnblogs.com/StevenFu/p/2858421.html
Copyright © 2011-2022 走看看