http://code.msdn.microsoft.com/office/Export-GridView-to-07c9f836

https://exporter.codeplex.com/

http://closedxml.codeplex.com/

http://epplus.codeplex.com/

  1. /// <summary>
  2. /// 塗聚文修改
  3. /// </summary>
  4. /// <param name="dt"></param>
  5. /// <param name="Response"></param>
  6. /// <param name="filename"></param>
  7. public static void Convertword(DataTable dt, HttpResponse Response, string filename)
  8. {
  9. Response.Clear();
  10. Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".doc");
  11. Response.Charset = "utf-8";
  12. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  13. Response.ContentType = "application/vnd.word";
  14. System.IO.StringWriter stringWrite = new System.IO.StringWriter();
  15. System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
  16. System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
  17. dg.DataSource = dt;
  18. dg.DataBind();
  19. dg.RenderControl(htmlWrite);
  20. Response.Write(stringWrite.ToString());
  21. Response.End();
  22. //HttpContext.Current.ApplicationInstance.CompleteRequest();
  23.  
  24. }
  25. /// <summary>
  26. ///
  27. /// </summary>
  28. /// <param name="dt"></param>
  29. /// <param name="Response"></param>
  30. /// <param name="filename"></param>
  31. public static void Convertexcel(DataTable dt, HttpResponse Response, string filename)
  32. {
  33.  
  34. Response.Clear();
  35. Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");//
  36. Response.Charset = "utf-8";
  37. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  38. Response.ContentType = "application/vnd.ms-excel";
  39. System.IO.StringWriter stringWrite = new System.IO.StringWriter();
  40. System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
  41. System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
  42. dg.DataSource = dt;
  43. dg.DataBind();
  44. dg.RenderControl(htmlWrite);
  45. Response.Write(stringWrite.ToString());
  46. Response.End();
  47. //HttpContext.Current.ApplicationInstance.CompleteRequest();
  48.  
  49. }
  50. /// <summary>
  51. ///
  52. /// </summary>
  53. /// <param name="dataTable"></param>
  54. /// <param name="Response"></param>
  55. /// <param name="filename"></param>
  56. public static void ConvertCSV(DataTable dataTable, HttpResponse Response, string filename)
  57. {
  58.  
  59. Response.Clear();
  60. Response.Buffer = true;
  61. Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".csv");
  62. Response.Charset = "utf-8";
  63. Response.Cache.SetCacheability(HttpCacheability.NoCache);
  64. Response.ContentType = "Application/x-msexcel";
  65. StringBuilder sb = new StringBuilder();
  66. if (dataTable.Columns.Count != 0)
  67. {
  68. foreach (DataColumn column in dataTable.Columns)
  69. {
  70. sb.Append(column.ColumnName + ',');
  71. }
  72. sb.Append("\r\n");
  73. foreach (DataRow row in dataTable.Rows)
  74. {
  75. foreach (DataColumn column in dataTable.Columns)
  76. {
  77. if (row[column].ToString().Contains(",") == true)
  78. {
  79. row[column] = row[column].ToString().Replace(",", "");
  80. }
  81. sb.Append(row[column].ToString() + ',');
  82. }
  83. sb.Append("\r\n");
  84. }
  85. }
  86. Response.Write(sb.ToString());
  87. Response.End();
  88. //HttpContext.Current.ApplicationInstance.CompleteRequest();
  89.  
  90. }

winform:

  1. /// <summary>
  2. /// winform
  3. /// Encoding导出无问题
  4. /// 涂聚文测试
  5. /// </summary>
  6. /// <param name="dgv"></param>
  7. /// <param name="fileName"></param>
  8. public static void ExportToCSV(DataGridView dgv, string fileName)
  9. {
  10. if (dgv.Rows.Count < 1)
  11. {
  12. MessageBox.Show("没有记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
  13. return;
  14. }
  15.  
  16. SaveFileDialog sfDialog = new SaveFileDialog();
  17. sfDialog.Filter = "CSV文件(*.csv)|*.csv|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
  18. sfDialog.FilterIndex = 0;
  19. sfDialog.FileName = fileName;
  20. if (sfDialog.ShowDialog() == DialogResult.OK)
  21. {
  22.  
  23. string strFileName = sfDialog.FileName;
  24. StreamWriter sw = new StreamWriter(strFileName, false, Encoding.Unicode);
  25. string strLine = "";
  26. foreach (DataGridViewColumn col in dgv.Columns)
  27. {
  28. if (col.Visible)
  29. {
  30. strLine += "\"" + col.HeaderText.Trim().Replace("\"", "\\\"") + "\"" + "\t";
  31. }
  32. }
  33. strLine = strLine.Substring(0, strLine.Length - 1);
  34. sw.WriteLine(strLine);
  35. sw.Flush();
  36.  
  37. foreach (DataGridViewRow dgvr in dgv.Rows)
  38. {
  39. strLine = "";
  40. foreach (DataGridViewCell dgvc in dgvr.Cells)
  41. {
  42. if (dgvc.Visible)
  43. {
  44. if (dgvc.Value == null)
  45. {
  46. strLine += "\t";
  47. }
  48. else
  49. {
  50. strLine += "\"" + dgvc.Value.ToString().Trim().Replace("\"","\"\"") + "\"" + "\t";
  51. }
  52. }
  53. }
  54. sw.WriteLine(strLine);
  55. sw.Flush();
  56. }
  57. sw.Close();
  58. MessageBox.Show(string.Format("数据已成功导出至\n{0}\n文件中!", strFileName), "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
  59. }
  60. }

  

csharp: DataTable export to excel,word,csv etc的更多相关文章

  1. csv/json/list/datatable导出为excel的通用模块设计

    导出excel的场景我一般都是一个List直接导出成一张sheet,用Npoi.Mapper库很方便,最近我经常是需要将接口返回的jsonarray转成一张excel表,比如从elasticsearc ...

  2. js导出到word、json、excel、csv

    tableExport.js ///*The MIT License (MIT) //Copyright (c) 2014 https://github.com/kayalshri/ //Permis ...

  3. [转].net 使用NPOI或MyXls把DataTable导出到Excel

    本文转自:http://www.cnblogs.com/yongfa365/archive/2010/05/10/NPOI-MyXls-DataTable-To-Excel-From-Excel.ht ...

  4. DataTable导出到Excel(.NET 4.0)

    最近在论坛里又看到很多关于DataTable(DataSet)导入Excel的帖子,我也温故知新一下,用VS2010重新整理了一个Sample.这个问题简化一下就是内存数据到文件,也就是遍历赋值,只不 ...

  5. How to Export to Excel

    https://simpleisbetterthancomplex.com/tutorial/2016/07/29/how-to-export-to-excel.html Export data to ...

  6. Aspose office (Excel,Word,PPT),PDF 在线预览

    前文: 做个备份,拿的是试用版的 Aspose,功能见标题 代码: /// <summary> /// Aspose office (Excel,Word,PPT),PDF 在线预览 // ...

  7. 把 DataTable 输出到 excel 文件

    ''' <summary> ''' 把 DataTable 输出到 excel 文件 ''' </summary> ''' <param name="dt_da ...

  8. DataTable导出到Excel

    简单的导出到Excel中: 代码如下: using System; using System.Collections.Generic; using System.Data; using System. ...

  9. PHP链式操作输出excel(csv)

    工作中经常会遇到产品运营让导出一些简单的比较规范的数据,这时候要是有一个简单的方法可以用就简单多了.下面是我的一个输出简单的excel(csv)的方法类,用到了链式操作.说到链式操作,在jquery中 ...

随机推荐

  1. mysql数据库表的基本操作sql语句总结

    1,命令行登录命令 mysql -h localhost -u root -p C:\Users\lenovo>mysql -u root -p Enter password: ***** We ...

  2. 2016级算法第五次上机-F.ModricWang的水系法术

    1066 ModricWang的水系法术 思路 比较典型的最大流问题,需要注意的是,题目已经暗示(明示)了这里的边是双向的,在建图的时候需要加上反向边的容量值. 解决最大流问题的基本思路就是不断在残量 ...

  3. [软件工程]项目选择与NABCD模型分析

    项目 内容 这个作业属于哪个课程 2019春季计算机学院软件工程(罗杰) 这个作业的要求在哪里 团队项目选择 这课程的目标是 以实践形式熟悉软件开发流程,团队开发,合作学习 本次作业对课程的帮助是 确 ...

  4. 关于JAVA泛型中的通配符类型

    之前对JAVA一知半解时就拿起weiss的数据结构开始看,大部分数据结构实现都是采取通配符的思想,好处不言而喻. 首先建立两个类employee和manager,继承关系如下.其次Pair类是一个简单 ...

  5. 01-复杂度2 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  6. sort sorted() reverse() reversed() 的区别1

    sort()是可变对象(字典.列表)的方法,无参数,无返回值,sort()会改变可变对象,因此无需返回值.sort()方法是可变对象独有的方法或者属性,而作为不可变对象如元组.字符串是不具有这些方法的 ...

  7. element ui 表格提交时获取所有选中的checkbox的数据

    <el-table ref="multipleTable" :data="appList" @selection-change="changeF ...

  8. Oracle系统表整理 --sql

    -- DBA/ALL/USER/V_$/GV_$/SESSION/INDEX开头的绝大部分都是视图-- DBA_TABLES意为DBA拥有的或可以访问的所有的关系表.-- ALL_TABLES意为某一 ...

  9. [转]C#算法题

    1:不允许使用循环语句.条件语句,在控制台中打印出1-200这200个数. 参考答案:这里我使用的是递归. static void Main(string[] args) { Print(); Con ...

  10. LR自我总结的问题

    1.Controller中添加负载测试时,最后运行完提示the following graph s were not created.导致最后没有数据报表生成. 解决方法:在result中将auto ...