C#操作CSV文件类实例
作者:feige 时间:2023-04-29 13:49:44
本文实例讲述了C#操作CSV文件类。分享给大家供大家参考。具体分析如下:
这个C#类用于转换DataTable为CSV文件、CSV文件转换成DataTable,如果需要进行CSV和DataTable之间进行转换,使用这个类非常合适。
using System.Data;
using System.IO;
namespace DotNet.Utilities
{
/// <summary>
/// CSV文件转换类
/// </summary>
public static class CsvHelper
{
/// <summary>
/// 导出报表为Csv
/// </summary>
/// <param name="dt">DataTable</param>
/// <param name="strFilePath">物理路径</param>
/// <param name="tableheader">表头</param>
/// <param name="columname">字段标题,逗号分隔</param>
public static bool dt2csv(DataTable dt, string strFilePath, string tableheader, string columname)
{
try
{
string strBufferLine = "";
StreamWriter strmWriterObj = new StreamWriter(strFilePath, false, System.Text.Encoding.UTF8);
strmWriterObj.WriteLine(tableheader);
strmWriterObj.WriteLine(columname);
for (int i = 0; i < dt.Rows.Count; i++)
{
strBufferLine = "";
for (int j = 0; j < dt.Columns.Count; j++)
{
if (j > 0)
strBufferLine += ",";
strBufferLine += dt.Rows[i][j].ToString();
}
strmWriterObj.WriteLine(strBufferLine);
}
strmWriterObj.Close();
return true;
}
catch
{
return false;
}
}
/// <summary>
/// 将Csv读入DataTable
/// </summary>
/// <param name="filePath">csv文件路径</param>
/// <param name="n">表示第n行是字段title,第n+1行是记录开始</param>
public static DataTable csv2dt(string filePath, int n, DataTable dt)
{
StreamReader reader = new StreamReader(filePath, System.Text.Encoding.UTF8, false);
int i = 0, m = 0;
reader.Peek();
while (reader.Peek() > 0)
{
m = m + 1;
string str = reader.ReadLine();
if (m >= n + 1)
{
string[] split = str.Split(',');
System.Data.DataRow dr = dt.NewRow();
for (i = 0; i < split.Length; i++)
{
dr[i] = split[i];
}
dt.Rows.Add(dr);
}
}
return dt;
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,CSV,文件,类
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java中Synchronized的用法解析
2023-07-28 22:40:51
![](https://img.aspxhome.com/file/2023/7/62227_0s.png)
mybatis foreach遍历LIST读到数据为null的问题
2021-05-24 20:15:27
![](https://img.aspxhome.com/file/2023/4/68554_0s.jpg)
一文详解Java中Stream流的使用
2021-08-23 11:54:51
![](https://img.aspxhome.com/file/2023/0/64800_0s.png)
Spring循环依赖代码演示及解决方案
2022-05-17 01:56:08
![](https://img.aspxhome.com/file/2023/1/86781_0s.jpg)
Java 反射之私有字段和方法详细介绍
2021-12-14 05:41:53
用java开发dota英雄最华丽的技能(实例讲解)
2022-05-27 00:10:20
![](https://img.aspxhome.com/file/2023/8/62158_0s.jpg)
java spring mvc处理器映射器介绍
2021-11-22 01:01:35
深入理解Java设计模式之装饰模式
2021-11-23 11:29:52
![](https://img.aspxhome.com/file/2023/3/88153_0s.jpg)
Springboot项目引入druid安装部署使用教程
2023-07-28 01:39:25
![](https://img.aspxhome.com/file/2023/5/57625_0s.png)
MyBatis 如何配置多个别名 typeAliasesPackage
2021-11-16 06:35:54
Java使用ThreadLocal实现当前登录信息的存取功能
2023-06-06 12:00:34
![](https://img.aspxhome.com/file/2023/0/59950_0s.png)
c#使用正则表达式匹配字符串验证URL示例
2023-01-01 10:40:10
Spring Security如何实现升级密码加密方式详解
2023-09-02 08:47:31
![](https://img.aspxhome.com/file/2023/2/94302_0s.png)
Java基础之多线程
2022-11-30 11:01:43
spring-boot-autoconfigure模块用法详解
2023-11-25 12:59:19
![](https://img.aspxhome.com/file/2023/8/60198_0s.png)
解决javac不是内部或外部命令,也不是可运行程序的报错问题
2022-07-13 22:39:25
![](https://img.aspxhome.com/file/2023/3/66623_0s.jpg)
c#多线程的应用全面解析
2023-03-03 21:15:02
![](https://img.aspxhome.com/file/2023/2/77272_0s.png)
jdk线程池的实现
2023-07-05 18:44:35
![](https://img.aspxhome.com/file/2023/1/88751_0s.jpg)
Java8中的 Lambda表达式教程
2023-10-13 01:32:29
Android Studio中Logcat写入和查看日志
2021-08-05 12:37:18
![](https://img.aspxhome.com/file/2023/9/106019_0s.png)