csharp: DataTable export to excel,word,csv etc
http://code.msdn.microsoft.com/office/Export-GridView-to-07c9f836
https://exporter.codeplex.com/
http://closedxml.codeplex.com/
http://epplus.codeplex.com/
- /// <summary>
- /// 塗聚文修改
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="Response"></param>
- /// <param name="filename"></param>
- public static void Convertword(DataTable dt, HttpResponse Response, string filename)
- {
- Response.Clear();
- Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".doc");
- Response.Charset = "utf-8";
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- Response.ContentType = "application/vnd.word";
- System.IO.StringWriter stringWrite = new System.IO.StringWriter();
- System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
- System.Web.UI.WebControls.GridView dg = new System.Web.UI.WebControls.GridView();
- dg.DataSource = dt;
- dg.DataBind();
- dg.RenderControl(htmlWrite);
- Response.Write(stringWrite.ToString());
- Response.End();
- //HttpContext.Current.ApplicationInstance.CompleteRequest();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dt"></param>
- /// <param name="Response"></param>
- /// <param name="filename"></param>
- public static void Convertexcel(DataTable dt, HttpResponse Response, string filename)
- {
- Response.Clear();
- Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls");//
- Response.Charset = "utf-8";
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- Response.ContentType = "application/vnd.ms-excel";
- System.IO.StringWriter stringWrite = new System.IO.StringWriter();
- System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
- System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
- dg.DataSource = dt;
- dg.DataBind();
- dg.RenderControl(htmlWrite);
- Response.Write(stringWrite.ToString());
- Response.End();
- //HttpContext.Current.ApplicationInstance.CompleteRequest();
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="dataTable"></param>
- /// <param name="Response"></param>
- /// <param name="filename"></param>
- public static void ConvertCSV(DataTable dataTable, HttpResponse Response, string filename)
- {
- Response.Clear();
- Response.Buffer = true;
- Response.AddHeader("content-disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".csv");
- Response.Charset = "utf-8";
- Response.Cache.SetCacheability(HttpCacheability.NoCache);
- Response.ContentType = "Application/x-msexcel";
- StringBuilder sb = new StringBuilder();
- if (dataTable.Columns.Count != 0)
- {
- foreach (DataColumn column in dataTable.Columns)
- {
- sb.Append(column.ColumnName + ',');
- }
- sb.Append("\r\n");
- foreach (DataRow row in dataTable.Rows)
- {
- foreach (DataColumn column in dataTable.Columns)
- {
- if (row[column].ToString().Contains(",") == true)
- {
- row[column] = row[column].ToString().Replace(",", "");
- }
- sb.Append(row[column].ToString() + ',');
- }
- sb.Append("\r\n");
- }
- }
- Response.Write(sb.ToString());
- Response.End();
- //HttpContext.Current.ApplicationInstance.CompleteRequest();
- }
winform:
- /// <summary>
- /// winform
- /// Encoding导出无问题
- /// 涂聚文测试
- /// </summary>
- /// <param name="dgv"></param>
- /// <param name="fileName"></param>
- public static void ExportToCSV(DataGridView dgv, string fileName)
- {
- if (dgv.Rows.Count < 1)
- {
- MessageBox.Show("没有记录!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
- return;
- }
- SaveFileDialog sfDialog = new SaveFileDialog();
- sfDialog.Filter = "CSV文件(*.csv)|*.csv|文本文件(*.txt)|*.txt|所有文件(*.*)|*.*";
- sfDialog.FilterIndex = 0;
- sfDialog.FileName = fileName;
- if (sfDialog.ShowDialog() == DialogResult.OK)
- {
- string strFileName = sfDialog.FileName;
- StreamWriter sw = new StreamWriter(strFileName, false, Encoding.Unicode);
- string strLine = "";
- foreach (DataGridViewColumn col in dgv.Columns)
- {
- if (col.Visible)
- {
- strLine += "\"" + col.HeaderText.Trim().Replace("\"", "\\\"") + "\"" + "\t";
- }
- }
- strLine = strLine.Substring(0, strLine.Length - 1);
- sw.WriteLine(strLine);
- sw.Flush();
- foreach (DataGridViewRow dgvr in dgv.Rows)
- {
- strLine = "";
- foreach (DataGridViewCell dgvc in dgvr.Cells)
- {
- if (dgvc.Visible)
- {
- if (dgvc.Value == null)
- {
- strLine += "\t";
- }
- else
- {
- strLine += "\"" + dgvc.Value.ToString().Trim().Replace("\"","\"\"") + "\"" + "\t";
- }
- }
- }
- sw.WriteLine(strLine);
- sw.Flush();
- }
- sw.Close();
- MessageBox.Show(string.Format("数据已成功导出至\n{0}\n文件中!", strFileName), "导出成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
- }
- }
csharp: DataTable export to excel,word,csv etc的更多相关文章
- csv/json/list/datatable导出为excel的通用模块设计
导出excel的场景我一般都是一个List直接导出成一张sheet,用Npoi.Mapper库很方便,最近我经常是需要将接口返回的jsonarray转成一张excel表,比如从elasticsearc ...
- js导出到word、json、excel、csv
tableExport.js ///*The MIT License (MIT) //Copyright (c) 2014 https://github.com/kayalshri/ //Permis ...
- [转].net 使用NPOI或MyXls把DataTable导出到Excel
本文转自:http://www.cnblogs.com/yongfa365/archive/2010/05/10/NPOI-MyXls-DataTable-To-Excel-From-Excel.ht ...
- DataTable导出到Excel(.NET 4.0)
最近在论坛里又看到很多关于DataTable(DataSet)导入Excel的帖子,我也温故知新一下,用VS2010重新整理了一个Sample.这个问题简化一下就是内存数据到文件,也就是遍历赋值,只不 ...
- How to Export to Excel
https://simpleisbetterthancomplex.com/tutorial/2016/07/29/how-to-export-to-excel.html Export data to ...
- Aspose office (Excel,Word,PPT),PDF 在线预览
前文: 做个备份,拿的是试用版的 Aspose,功能见标题 代码: /// <summary> /// Aspose office (Excel,Word,PPT),PDF 在线预览 // ...
- 把 DataTable 输出到 excel 文件
''' <summary> ''' 把 DataTable 输出到 excel 文件 ''' </summary> ''' <param name="dt_da ...
- DataTable导出到Excel
简单的导出到Excel中: 代码如下: using System; using System.Collections.Generic; using System.Data; using System. ...
- PHP链式操作输出excel(csv)
工作中经常会遇到产品运营让导出一些简单的比较规范的数据,这时候要是有一个简单的方法可以用就简单多了.下面是我的一个输出简单的excel(csv)的方法类,用到了链式操作.说到链式操作,在jquery中 ...
随机推荐
- mysql数据库表的基本操作sql语句总结
1,命令行登录命令 mysql -h localhost -u root -p C:\Users\lenovo>mysql -u root -p Enter password: ***** We ...
- 2016级算法第五次上机-F.ModricWang的水系法术
1066 ModricWang的水系法术 思路 比较典型的最大流问题,需要注意的是,题目已经暗示(明示)了这里的边是双向的,在建图的时候需要加上反向边的容量值. 解决最大流问题的基本思路就是不断在残量 ...
- [软件工程]项目选择与NABCD模型分析
项目 内容 这个作业属于哪个课程 2019春季计算机学院软件工程(罗杰) 这个作业的要求在哪里 团队项目选择 这课程的目标是 以实践形式熟悉软件开发流程,团队开发,合作学习 本次作业对课程的帮助是 确 ...
- 关于JAVA泛型中的通配符类型
之前对JAVA一知半解时就拿起weiss的数据结构开始看,大部分数据结构实现都是采取通配符的思想,好处不言而喻. 首先建立两个类employee和manager,继承关系如下.其次Pair类是一个简单 ...
- 01-复杂度2 Maximum Subsequence Sum (25 分)
Given a sequence of K integers { N1, N2, ..., NK }. A continuous subsequence is defined to ...
- sort sorted() reverse() reversed() 的区别1
sort()是可变对象(字典.列表)的方法,无参数,无返回值,sort()会改变可变对象,因此无需返回值.sort()方法是可变对象独有的方法或者属性,而作为不可变对象如元组.字符串是不具有这些方法的 ...
- element ui 表格提交时获取所有选中的checkbox的数据
<el-table ref="multipleTable" :data="appList" @selection-change="changeF ...
- Oracle系统表整理 --sql
-- DBA/ALL/USER/V_$/GV_$/SESSION/INDEX开头的绝大部分都是视图-- DBA_TABLES意为DBA拥有的或可以访问的所有的关系表.-- ALL_TABLES意为某一 ...
- [转]C#算法题
1:不允许使用循环语句.条件语句,在控制台中打印出1-200这200个数. 参考答案:这里我使用的是递归. static void Main(string[] args) { Print(); Con ...
- LR自我总结的问题
1.Controller中添加负载测试时,最后运行完提示the following graph s were not created.导致最后没有数据报表生成. 解决方法:在result中将auto ...