zoukankan      html  css  js  c++  java
  • C# Excl(IO流)

    IO流

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data;
    using System.IO;
    using System.Text;
    
    namespace Reorts.HelpClass
    {
        public class Class1
        {
            public DataTable Dat(DataTable dt)
            {
                DataTable Tables = new DataTable();
                Tables.Columns.Add("时间参数");
                for (int i = 0; i < 24; i++)
                {
                    Tables.Columns.Contains("");
                    Tables.Columns.Add(i + ":00");
                }
                foreach (DataRow item in dt.Rows)
                {
                    DataRow DR = Tables.NewRow();
                    // Tables.Rows.Add
                    DR["时间参数"] = new Reorts.Controllers.ReortsController().TAGStr(item["TAG"].ToString());
                    for (int i = 0; i < 24; i++)
                    {
                        DR[i + ":00"] = item[i.ToString()].ToString();
                    }
                    Tables.Rows.Add(DR);
                }
                return Tables;
            }
    
    
            public void ExportExcel(DataTable dt,string ExcelName)
            {
                //设置导出文件路径
                string path = HttpContext.Current.Server.MapPath("Export/");
    
                //设置新建文件路径及名称
               // string savePath = path + DateTime.Now.ToString("yyyy-MM-dd-HH-mm-ss") + ".xls";
                string savePath = "C:\" + ExcelName + ".xls";
                //创建文件
                FileStream file = new FileStream(savePath, FileMode.CreateNew, FileAccess.Write);
              
    
              
    
    
                //以指定的字符编码向指定的流写入字符
                StreamWriter sw = new StreamWriter(file, Encoding.GetEncoding("GB2312"));
    
                StringBuilder strbu = new StringBuilder();
                strbu.Append("表头");
                strbu.Append(Environment.NewLine);
                strbu.Append("时间"+DateTime.Now.ToString("yyyy年MM月dd日"));
                strbu.Append(Environment.NewLine);
                //写入标题
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    strbu.Append(dt.Columns[i].ColumnName.ToString() + "	");
                }
                //加入换行字符串
                strbu.Append(Environment.NewLine);
    
                //写入内容
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        strbu.Append(dt.Rows[i][j].ToString() + "	");
                    }
                    strbu.Append(Environment.NewLine);
                }
    
                sw.Write(strbu.ToString());
                sw.Flush();
                file.Flush();
    
                sw.Close();
                sw.Dispose();
    
                file.Close();
                file.Dispose();
            }
        }
    }

     使用io流的方式导出Excel到浏览器

     
  • 相关阅读:
    进度条
    html5 表单新增事件
    html5 表单的新增type属性
    html5 表单的新增元素
    html5 语义化标签
    jq 手风琴案例
    codeforces 702D D. Road to Post Office(数学)
    codeforces 702C C. Cellular Network(水题)
    codeforces 702B B. Powers of Two(水题)
    codeforces 702A A. Maximum Increase(水题)
  • 原文地址:https://www.cnblogs.com/xianyv/p/11949884.html
Copyright © 2011-2022 走看看