zoukankan      html  css  js  c++  java
  • 导出EXCEL

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Text;

    public partial class _ss : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("col1");
            dt.Columns.Add("col2");
            dt.Columns.Add("col3");
            dt.Columns.Add("col4");
            dt.Columns.Add("col5");

            for (int i = 0; i < 5000; i++)
            {
                DataRow dr = dt.NewRow();
                dr.ItemArray = new object[]{System.Guid.NewGuid().ToString(),
                System.Guid.NewGuid().ToString(),
                System.Guid.NewGuid().ToString(),
                System.Guid.NewGuid().ToString(),
                System.Guid.NewGuid().ToString()};

                dt.Rows.Add(dr);
            }

            if (dt != null)
            {
                StringBuilder sr = new StringBuilder();
                foreach (DataColumn col in dt.Columns)
                {
                    sr.Append(col.ColumnName + "\t");
                }
                sr.Append("\r\n");

                foreach (DataRow dr in dt.Rows)
                {
                    foreach (object obj in dr.ItemArray)
                    {
                        sr.Append(obj.ToString() + "\t");
                    }
                    sr.Append("\r\n");
                }

                //文本输出为csv个数,可用excel直接打开
                Response.Clear();
                Response.Buffer = true;
                Response.Charset = "gb2312";
                //定义为为附件下载
                Response.AppendHeader("Content-Disposition""attachment;filename=\"" + HttpUtility.UrlEncode("统计报表_", System.Text.Encoding.UTF8) + DateTime.Now.ToString("yyyyMMdd-HHmmss-ffffff") + ".xls\"");
                Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
                Response.ContentType = "application/ms-excel";
                this.EnableViewState = false;
                //文本输出
                System.IO.StringWriter strWriter = new System.IO.StringWriter();
                strWriter.Write(sr.ToString());
                strWriter.Flush();
                //System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(strWriter);
                
    //this.RenderControl(htmlWriter);
                Response.Write(strWriter.ToString());
                Response.End();
            }
        }
    }
     
    点击下载:源码
  • 相关阅读:
    【leetcode】1. Two Sum
    【leetcode】32. Longest Valid Parentheses
    【leetcode】20. Valid Parentheses
    scala细节
    【转】CentOS下firefox安装flash说明
    Spring 容器(一)
    源码学习快捷键
    Gradle编译spring3.x报错找不到itextpdf4.2.2解决方案
    Insertion Sort
    Bubble Sort
  • 原文地址:https://www.cnblogs.com/wangpei/p/2300544.html
Copyright © 2011-2022 走看看