http://www.woaic.com/2012/06/64

excel文件主要是输出html代码。以xls的文本格式保存文件。

生成excel格式的代码:

/// <summary>

 /// 输出excel格式的html代码

 /// </summary>

 /// <param name="dt"></param>

 /// <param name="fileName"></param>

 /// <param name="tableHeader"></param>

 /// <param name="UseDefinedHeader"></param>

 /// <returns></returns>

 public string MakeExcelStr(DataTable dt, string fileName, string[] tableHeader,bool UseDefinedHeader)

 {

 StringBuilder strResult = new StringBuilder();

 int iColCount = dt.Columns.Count;

 if (dt.Columns.Count == 0)

 return strResult.ToString();

 strResult.Append("<table cellspacing=\"0\" cellpadding=\"5\" rules=\"all\" border=\"1\">");

 strResult.Append("<tr style=\"font-weight: bold; white-space: nowrap;\">");

 if (UseDefinedHeader)//是否是用户自定义表头

 {

 for (int i = 0; i < tableHeader.Length; i++)

 {

 strResult.Append("<td style=\"text-align:center\">" + tableHeader[i] + "</td>");//让文本居中显示

 }

 }

 else

 {

 for (int i = 0; i < iColCount; i++)

 {

 strResult.Append("<td style=\"text-align:center\">" + (dt.Columns[i] + "").ToString() + "</td>");

 }

 }

 strResult.Append("</tr>");

 foreach (DataRow dr in dt.Rows)

 {

 strResult.Append("<tr>");

 for (int i = 0; i < iColCount; i++)

 {

 // style=\"vnd.ms-excel.numberformat:@\"

 strResult.Append("<td style=\"vnd.ms-excel.numberformat:@\">'" + (dr[i] + "").ToString() + "</td>");//设置单元格是文本格式(防止文本是科学计算法,即E+***这种情况)

 }

 strResult.Append("</tr>");

 }

 strResult.Append("</table>");

 

return strResult.ToString();

 }

把生成的excel格式的代码保存成excel文件的方法

/// <summary>

 /// 导出excel表格

 /// </summary>

 /// <param name="dt"></param>

 /// <param name="fileName"></param>

 /// <param name="tableHeader">表头</param>

 /// <param name="Defined">是否自定义表头</param>

 private void ExprotToExcel(DataTable dt, string fileName, string[] tableHeader, bool Defined)

 {

 DogManage.Common.ExcelHelper excelHelper = new DogManage.Common.ExcelHelper();

 string result = excelHelper.MakeExcelStr(dt, fileName, tableHeader,Defined);

 

 HttpContext.Current.Response.Clear();

 System.IO.StringWriter sw = new System.IO.StringWriter();

 sw.Write(result);

 sw.Close();

 string str2 = HttpUtility.UrlEncode(fileName + "_" + DateTime.Now.ToString("yyyy-MM-dd"));

 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" +str2 + ".xls");

 HttpContext.Current.Response.ContentType = "application/ms-excel";

 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");

 HttpContext.Current.Response.Write(sw);

 HttpContext.Current.Response.End();

 }

生成word的代码:

private void exportword()

 {

 Response.Clear();

 Response.Buffer = true;

 Response.Charset = "utf-8";

 Response.AppendHeader("Content-Disposition", "attachment;filename=tmp.doc");

 Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8");

 

Response.ContentType = "application/ms-word";

 this.EnableViewState = false;

 System.IO.StringWriter oStringWriter = new System.IO.StringWriter();

 System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);

 oHtmlTextWriter.WriteLine(hf.Value);

 Response.Write(oStringWriter.ToString());

 Response.End();

 }

csv文件就是一个有特定格式的字符串拼接出来的文件,每个列通过,分割开来。换行则两外输出一行文本就行了

生成csv文件的代码:

/// <summary>

 /// 输出csv文件的代码

 /// </summary>

 /// <param name="dt"></param>

 /// <param name="fileName"></param>

 /// <param name="tableHeader">表头</param>

 /// <returns></returns>

 public string MakeCSVStr(DataTable dt, string fileName, string[] tableHeader)

 {

 StringBuilder strResult = new StringBuilder();

 string temp = string.Empty;

 int iColCount = dt.Columns.Count;

 if (iColCount == 0)

 return strResult.ToString();

 for (int i = 0; i < iColCount; i++)

 {

 temp+="\"" + dt.Columns[i] + "\"";

 if (i < iColCount - 1)

 temp+=",";

 }

 strResult.AppendLine(temp);

 foreach (DataRow dr in dt.Rows)

 {

 temp = "";

 for (int i = 0; i < iColCount; i++)

 {

 if (!Convert.IsDBNull(dr[i]))

 temp+="\"" + dr[i].ToString() + "\"";

 else

 temp += "\"\"";

 if (i < iColCount - 1)

 temp+=",";

 }

 strResult.AppendLine(temp);

 }

 return strResult.ToString();

 }

导出csv文件的方法:

/// <summary>

 /// 导出csv文件

 /// </summary>

 /// <param name="dt"></param>

 /// <param name="fileName"></param>

 /// <param name="tableHeader">表头</param>

 /// <param name="Defined">是否自定义表头</param>

 private void ExprotToCSV(DataTable dt,string fileName, string[] tableHeader)

 {

 DogManage.Common.ExcelHelper excelHelper = new DogManage.Common.ExcelHelper();

 string result=excelHelper.MakeCSVStr(dt, fileName, tableHeader);

 

HttpContext.Current.Response.Clear();

 System.IO.StringWriter sw = new System.IO.StringWriter();

 sw.Write(result);

 sw.Close();

 HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName + ".csv");

 HttpContext.Current.Response.ContentType = "application/vnd.ms-excel";

 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");

 HttpContext.Current.Response.Write(sw);

 HttpContext.Current.Response.End();

 }

导出excel、word、csv文件方法汇总的更多相关文章

  1. PHP导出数据到CSV文件函数/方法

    如果不清楚什么是CSV文件,可看如下文章介绍  CSV格式的是什么文件?CSV是什么的缩写? /** * 导出数据到CSV文件 * @param array $data 数据 * @param arr ...

  2. (转载)DBGridEh导出Excel等格式文件

    DBGridEh导出Excel等格式文件 uses DBGridEhImpExp; {--------------------------------------------------------- ...

  3. Excel打开csv文件乱码问题的解决办法

    excel打开csv 出现乱码怎么解决 https://jingyan.baidu.com/article/ac6a9a5e4c681b2b653eacf1.html CSV是逗号分隔值的英文缩写,通 ...

  4. js原生导出excel和csv

    ​ 严格意义来说并不是真正的excel文件,只是可以用excel打开查看而已,实际上的格式是逗号分隔文件即csv文件. 这里有几个坑要说一下: 不加Unicode的utf8头部标识excel打开文件会 ...

  5. mysql导出数据到csv文件

    在日常工作中经常会遇见导出表中的数据到csv文件的操作,这里就简单总结一下导出的操作. 下面对csv文件的描述是摘录: 据RFC4180文档设置的,该文档全称Common Format and MIM ...

  6. PHP导出数据到CSV文件函数 csv_export()

    后台往往需要导出各种数据到 Excel文档中.通常我们是导出 .csv文件格式,PHP导出函数参考代码如下: /** * 导出数据到CSV文件 * * @param array $data 二维数组( ...

  7. PHP导出数据到CSV文件

    后台往往需要导出各种数据到 Excel文档中.通常我们是导出 .csv文件格式,PHP导出函数参考代码如下: /** * 导出数据到CSV文件 * * @param array $data 二维数组( ...

  8. DataGird导出EXCEL的几个方法

    DataGird导出EXCEL的几个方法(WebControl) using System;using System.Data;using System.Text;using System.Web;u ...

  9. excel打开csv文件乱码解决办法

    参考链接: https://jingyan.baidu.com/article/4dc408484776fbc8d846f168.html 问题:用 Excel 打开 csv 文件,确认有乱码的问题. ...

随机推荐

  1. Vulhub-DC-3靶场

    Vulhub-DC-3靶场 前言 今天把DC-3的靶场打了一遍,可以说这个靶场用到的思路是非常经典的,从信息搜集到漏洞利用包括内核提权.最最重要的是为了下载它的提权EXP,我它喵还花了一块二买了个CS ...

  2. Kali中John的使用方法

    John是一个破解系统密码的工具. 参数 John the Ripper 1.9.0-jumbo-1 OMP [linux-gnu 64-bit x86_64 AVX AC] Copyright (c ...

  3. maven将依赖打入jar包中

    1.在pom.xml中加入maven-assembly-plugin插件: <build> <plugins> <plugin> <artifactId> ...

  4. 【SpringBoot技术专题】「JWT技术专区」SpringSecurity整合JWT授权和认证实现

    JWT基本概念 JWT,即 JSON Web Tokens(RFC 7519),是一个广泛用于验证 REST APIs 的标准.虽说是一个新兴技术,但它却得以迅速流行. JWT的验证过程是: 前端(客 ...

  5. 温故知新,微软官方推荐的Visual Studio源代码管理之Git Ignore清单,开启新项目必备宝书

    什么是Git Ignore清单 https://git-scm.com/docs/gitignore 简单来说,在Git进行源代码管理中,我们可以通过建立.gitignore来实现一个忽略的黑名单管理 ...

  6. Sqli-Labs less11-12

    less-11 11关以后已经和前几关不同.页面由get方式变成了类似form表单的post方式的登陆界面,我们不能直接看到数据,所以要用到burp抓包. 抓包方式前面已经说过,这里直接使用,我们先输 ...

  7. JVM学习笔记之class文件结构【七】

    一.概念 1.1 无符号数: 以 u1.u2.u3.u4.u8 代表 1 个字节,2 个字节.4 个字节.8 个字节的无符号数.无符号数可以描述数字,索引引用.数量值和按照 UTF-8 编码构成的字符 ...

  8. 数据结构与算法-排序(九)基数排序(Radix Sort)

    摘要 基数排序是进行整数序列的排序,它是将整数从个位开始,直到最大数的最后一位截止,每一个进位(比如个位.十位.百位)的数进行排序比较. 每个进位做的排序比较是用计数排序的方式处理,所以基数排序离不开 ...

  9. Docker创建Docker-Registry-私服

    docker-compose.yml version: '3.1' services: registry: image: registry restart: always container_name ...

  10. 【C#】GC和析构函数(Finalize 方法)

    析构函数: (来自百度百科)析构函数(destructor) 与构造函数相反,当对象脱离其作用域时(例如对象所在的函数已调用完毕),系统自动执行析构函数.析构函数往往用来做"清理善后&quo ...