C# 实现TXT文档转Table的示例代码
作者:農碼一生 时间:2022-04-23 07:47:52
代码:
public DataTable TXTToDataTable(string fileName, string columnName)
{
DataTable dt = new DataTable();
FileStream fs = new FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
StreamReader sr = new StreamReader(fs, System.Text.Encoding.Default);
//记录每次读取的一行记錄
string strLine = "";
//記錄每行記錄中的各字段内容
string[] aryLine;
//标示列数
int columnCount = 0;
//标示是否是读取的第一行
bool IsFirst = true;
if (IsFirst == true)
{
//strLine = "ATTENDANCE_DATE,EMP,ATTENDANCE_DEPT,EMP_TYPE,SHITF,PLANT_CODE";
strLine = columnName;
aryLine = strLine.Split(',');
IsFirst = false;
columnCount = aryLine.Length;
//创建列
for (int i = 0; i < columnCount; i++)
{
DataColumn dc = new DataColumn(aryLine[i].ToUpper());
dt.Columns.Add(dc);
}
}
//逐行读取txt中的数據
while ((strLine = sr.ReadLine()) != null)
{
aryLine = strLine.Split('\t');//tab分隔符
DataRow dr = dt.NewRow();
for (int j = 0; j < columnCount; j++)
{
dr[j] = aryLine[j].ToUpper();
}
dt.Rows.Add(dr);
}
sr.Close();
fs.Close();
return dt;
}
来源:https://www.cnblogs.com/wml-it/p/13195101.html
标签:c#,txt,文档,table
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
spring的xml文件打开没有namespace等操作选项的解决方案
2022-09-10 14:56:47
![](https://img.aspxhome.com/file/2023/4/66514_0s.jpg)
Java模拟计算机的整数乘积计算功能示例
2022-01-30 19:25:51
![](https://img.aspxhome.com/file/2023/8/75678_0s.png)
SpringBoot快速配置数据源的方法
2023-07-28 13:22:42
彻底掌握C语言strcpy函数的用法
2023-07-03 07:21:18
![](https://img.aspxhome.com/file/2023/4/103104_0s.png)
解决Mybatis的@Param()注解导致分页失效的问题
2022-05-01 13:24:53
![](https://img.aspxhome.com/file/2023/0/63400_0s.jpg)
Java中间消息件ActiveMQ使用实例
2021-10-10 14:24:21
![](https://img.aspxhome.com/file/2023/2/74752_0s.png)
java与c#的语法区别详细介绍
2022-05-02 14:01:10
全面总结java IO体系
2023-05-16 13:19:12
![](https://img.aspxhome.com/file/2023/3/73093_0s.jpg)
java实现支付宝支付接口的调用
2023-10-13 09:43:40
![](https://img.aspxhome.com/file/2023/1/58611_0s.jpg)
Idea中SpringBoot多模块项目的建立实现
2023-11-08 07:52:34
![](https://img.aspxhome.com/file/2023/3/58743_0s.png)
使用C#发送Http请求实现模拟登陆实例
2023-06-22 22:25:07
![](https://img.aspxhome.com/file/2023/7/67047_0s.png)
只需两步实现Eclipse+Maven快速构建第一个Spring Boot项目
2023-08-03 23:47:43
![](https://img.aspxhome.com/file/2023/4/78354_0s.png)
C# PDF转图片(JPG,Png)的项目实践
2021-11-10 17:59:24
![](https://img.aspxhome.com/file/2023/7/92907_0s.png)
详解Java中的Lambda表达式
2023-06-05 01:37:16
一文让你搞懂如何手写一个redis分布式锁
2023-11-29 02:46:30
你真的知道Java中对象的销毁吗
2022-09-14 22:31:38
java动态绑定和静态绑定用法实例详解
2022-04-17 19:55:20
Java获取文件的路径及常见问题解决方案
2023-04-16 04:24:15
![](https://img.aspxhome.com/file/2023/9/79939_0s.png)
java使用@Transactional时常犯的N种错误
2021-08-16 01:58:44
如何优雅的处理Spring Boot异常信息详解
2023-11-29 09:50:02
![](https://img.aspxhome.com/file/2023/6/60536_0s.jpg)