• 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到浏览器

     
  • 相关阅读:
    PHP工具下载地址
    Eclipse开发PHP环境配置
    Windows下搭建PHP开发环境
    无插件Vim编程技巧
    mvn详解
    Python读写文件
    python 大文件以行为单位读取方式比对
    insert时出现主键冲突的处理方法【转载】
    Python机器学习——线性模型
    机器学习算法与Python实践之(七)逻辑回归(Logistic Regression)
  • 原文地址:https://www.cnblogs.com/xianyv/p/11949884.html
Copyright © 2020-2023  润新知