C# Excl(IO流)
2021-01-15 05:11
标签:flush 创建文件 mode oid lin table ppa access excel IO流 使用io流的方式导出Excel到浏览器 C# Excl(IO流) 标签:flush 创建文件 mode oid lin table ppa access excel 原文地址:https://www.cnblogs.com/xianyv/p/11949884.htmlusing 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 )
{
strbu.Append(dt.Columns[i].ColumnName.ToString() + "\t");
}
//加入换行字符串
strbu.Append(Environment.NewLine);
//写入内容
for (int i = 0; i )
{
for (int j = 0; j )
{
strbu.Append(dt.Rows[i][j].ToString() + "\t");
}
strbu.Append(Environment.NewLine);
}
sw.Write(strbu.ToString());
sw.Flush();
file.Flush();
sw.Close();
sw.Dispose();
file.Close();
file.Dispose();
}
}
}