博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CSVHelper 导出CSV 格式
阅读量:5234 次
发布时间:2019-06-14

本文共 10264 字,大约阅读时间需要 34 分钟。

public class CSVHelper    {        System.Windows.Forms.SaveFileDialog saveFileDialog1;//保存        private string header = string.Empty;//标题        ///         /// 初始化打印设置        ///         ///         protected void InitExportController(Dictionary
addition) { saveFileDialog1 = new System.Windows.Forms.SaveFileDialog(); saveFileDialog1.Filter = "导出CSV (*.csv)|*.csv"; saveFileDialog1.FilterIndex = 1; saveFileDialog1.RestoreDirectory = true; saveFileDialog1.CreatePrompt = false; saveFileDialog1.Title = "导出文件保存路径"; saveFileDialog1.RestoreDirectory = true;//保存对话框是否记忆上次打开的目录 header = addition["HeardTitle"].ToString(); saveFileDialog1.FileName = header; } ///
/// 导出 /// ///
表格 ///
路径 public bool Export(Dictionary
addition, System.Data.DataTable dt) { if (!object.Equals(dt, null)) { InitExportController(addition); try { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string filename = saveFileDialog1.FileName; return ExportToSvc(addition, dt, filename); } } catch { saveFileDialog1.FileName = "请输入文件名称"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string filename = saveFileDialog1.FileName; return ExportToSvc(addition, dt, filename); } } } return false; } ///
/// 导出 /// ///
内容 ///
路径 public bool Export(Dictionary
addition, DataGridView gv) { if (gv.Rows.Count > 0) { InitExportController(addition); try { if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string filename = saveFileDialog1.FileName; return ExportToSvc(addition, gv, filename); } } catch { saveFileDialog1.FileName = "请输入文件名称"; if (saveFileDialog1.ShowDialog() == DialogResult.OK) { string filename = saveFileDialog1.FileName; return ExportToSvc(addition, gv, filename); } } } return false; } ///
/// 导出为svc文件,strFileName为要导出的csv格式文件的路径和文件名 /// ///
头部信息 ///
表格 ///
路径 protected bool ExportToSvc(Dictionary
addition, System.Data.DataTable dt, string strFileName) { string strPath = strFileName; if (File.Exists(strPath)) { File.Delete(strPath); } //先打印标头 StringBuilder strColu = new StringBuilder(); StringBuilder strValue = new StringBuilder(); int i = 0; try { StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312")); if (addition.ContainsKey("HeardTitle")) { sw.WriteLine(addition["HeardTitle"].ToString()); } if (addition.ContainsKey("Content")) { sw.WriteLine(addition["Content"].ToString()); sw.WriteLine(); } for (i = 0; i <= dt.Columns.Count - 1; i++) { if (addition.ContainsKey(dt.Columns[i].ColumnName)) { strColu.Append(addition[dt.Columns[i].ColumnName].ToString().Trim()); } else { strColu.Append(dt.Columns[i].ColumnName); } strColu.Append(","); } strColu.Remove(strColu.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strColu); foreach (DataRow dr in dt.Rows) { strValue.Remove(0, strValue.Length);//移出 for (i = 0; i <= dt.Columns.Count - 1; i++) { strValue.Append(dr[i].ToString()); strValue.Append(","); } strValue.Remove(strValue.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strValue); } if (addition.ContainsKey("Foot")) { sw.WriteLine(); sw.WriteLine(addition["Foot"].ToString()); } sw.Close(); return true; } catch (Exception ex) { throw ex; } } ///
/// 导出为svc文件,strFileName为要导出的csv格式文件的路径和文件名 /// ///
头部信息 ///
DataGridView控件 ///
protected bool ExportToSvc(Dictionary
addition, DataGridView gv, string strFileName) { string strPath = strFileName; if (File.Exists(strPath)) { File.Delete(strPath); } //先打印标头 StringBuilder strColu = new StringBuilder(); StringBuilder strValue = new StringBuilder(); int i = 0; try { StreamWriter sw = new StreamWriter(new FileStream(strPath, FileMode.CreateNew), Encoding.GetEncoding("GB2312")); if (addition.ContainsKey("HeardTitle")) { sw.WriteLine(addition["HeardTitle"].ToString()); } if (addition.ContainsKey("Content")) { sw.WriteLine(addition["Content"].ToString()); sw.WriteLine(); } for (i = 0; i <= gv.Columns.Count - 1; i++) { if (gv.Columns[i].Visible == true) {
//可见的列导出,隐藏的列过滤 strColu.Append(gv.Columns[i].HeaderText.Trim()); strColu.Append(","); } } strColu.Remove(strColu.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strColu); foreach (DataGridViewRow dr in gv.Rows) { strValue.Remove(0, strValue.Length);//移出 for (i = 0; i <= gv.Columns.Count - 1; i++) { if (gv.Columns[i].Visible == true) {
//可见的导出 strValue.Append(dr.Cells[i].Value.ToString()); strValue.Append(","); } } strValue.Remove(strValue.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strValue); } if (addition.ContainsKey("Foot")) { sw.WriteLine(); sw.WriteLine(addition["Foot"].ToString()); } sw.Close(); return true; } catch (Exception ex) { throw ex; } } ///
/// 导出CSV /// ///
显示标题 ///
文件名 ///
一至多个表源 ///
public bool Export(string title, string name, params DataTable[] dts) { bool result = false; if (dts != null && dts.Length > 0) { var dict = new Dictionary
(); dict["HeardTitle"] = name; InitExportController(dict); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { try { using (StreamWriter sw = new StreamWriter(new FileStream(saveFileDialog1.FileName, FileMode.CreateNew), Encoding.GetEncoding("GB2312"))) { sw.WriteLine(title); foreach (DataTable dt in dts) { StringBuilder strValue = new StringBuilder(); StringBuilder strColu = new StringBuilder(); for (int i = 0; i <= dt.Columns.Count - 1; i++) { strColu.Append(dt.Columns[i].ColumnName); strColu.Append(","); } strColu.Remove(strColu.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strColu); foreach (DataRow dr in dt.Rows) { strValue.Remove(0, strValue.Length);//移出 for (int i = 0; i <= dt.Columns.Count - 1; i++) { strValue.Append(dr[i].ToString()); strValue.Append(","); } strValue.Remove(strValue.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strValue); } sw.WriteLine(); } } result = true; } catch (Exception exc) { Console.WriteLine(exc.Message); } } } return result; } ///
/// 导出CSV /// ///
显示标题 ///
文件名 ///
一至多个表源 ///
public bool Export(string title, string name, DataSet ds) { bool result = false; if (ds != null && ds.Tables.Count > 0) { var dict = new Dictionary
(); dict["HeardTitle"] = name; InitExportController(dict); if (saveFileDialog1.ShowDialog() == DialogResult.OK) { try { using (StreamWriter sw = new StreamWriter(new FileStream(saveFileDialog1.FileName, FileMode.CreateNew), Encoding.GetEncoding("GB2312"))) { sw.WriteLine(title); foreach (DataTable dt in ds.Tables) { StringBuilder strValue = new StringBuilder(); StringBuilder strColu = new StringBuilder(); for (int i = 0; i <= dt.Columns.Count - 1; i++) { strColu.Append(dt.Columns[i].ColumnName); strColu.Append(","); } strColu.Remove(strColu.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strColu); foreach (DataRow dr in dt.Rows) { strValue.Remove(0, strValue.Length);//移出 for (int i = 0; i <= dt.Columns.Count - 1; i++) { strValue.Append(dr[i].ToString()); strValue.Append(","); } strValue.Remove(strValue.Length - 1, 1);//移出掉最后一个,字符 sw.WriteLine(strValue); } sw.WriteLine(); } } result = true; } catch (Exception exc) { Console.WriteLine(exc.Message); } } } return result; } }

 

转载于:https://www.cnblogs.com/dragon-L/p/3777215.html

你可能感兴趣的文章
Windows多线程入门のCreateThread与_beginthreadex本质区别(转)
查看>>
Nginx配置文件(nginx.conf)配置详解1
查看>>
linux php编译安装
查看>>
name phone email正则表达式
查看>>
721. Accounts Merge
查看>>
OpenCv-Python 图像处理基本操作
查看>>
「Unity」委托 将方法作为参数传递
查看>>
重置GNOME-TERMINAL
查看>>
redis哨兵集群、docker入门
查看>>
hihoCoder 1233 : Boxes(盒子)
查看>>
oracle中anyData数据类型的使用实例
查看>>
C++对vector里面的元素排序及取任意重叠区间
查看>>
软件测试——性能测试总结
查看>>
12.4站立会议
查看>>
Java Concurrentmodificationexception异常原因和解决方法
查看>>
客户端访问浏览器的流程
查看>>
codeforces水题100道 第二十二题 Codeforces Beta Round #89 (Div. 2) A. String Task (strings)
查看>>
c++||template
查看>>
[BZOJ 5323][Jxoi2018]游戏
查看>>
编程面试的10大算法概念汇总
查看>>