//*********************************************************************************************
public static DataTable TXT_To_DataTable(string dirTXT)
{
StreamReader objReader = new StreamReader(dirTXT, Encoding.Default);
DataTable dt = new DataTable();
string strline;
string[] aryline;
bool blnFlag = false;
while ((strline = objReader.ReadLine()) != null)
{
aryline = strline.Split(new char[] { '\t' }, StringSplitOptions.None);
if (blnFlag == false)
{
DataColumn mydc = null;
foreach (string str in aryline)
{
mydc = new DataColumn(str);
dt.Columns.Add(mydc);
}
blnFlag = true;
}
else
{
dt.Rows.Add(aryline);
}
} objReader.Close();
return dt;
}
/// <summary>
/// 以Tab键为间隔,以Tab键表示空值
/// </summary>
/// <param name="dirTXT"></param>
/// <returns></returns>
public static DataTable TXT_To_DataTable2(string dirTXT)
{
StreamReader objReader = new StreamReader(dirTXT, Encoding.Default);
DataTable dt = new DataTable();
string strline;
string[] aryline;
bool blnFlag = false;
while ((strline = objReader.ReadLine()) != null)
{
aryline = strline.Split(new char[] { '\t' }, StringSplitOptions.None);
if (blnFlag == false)
{
foreach (string str in aryline)
{
if (!string.IsNullOrEmpty(str))
{
DataColumn mydc = new DataColumn(str);
dt.Columns.Add(mydc);
}
}
blnFlag = true;
}
else
{
if (aryline.Count() != dt.Columns.Count)
{
strline = Lines_To_Replace(strline);
aryline = strline.Split(new char[] { '\t' }, StringSplitOptions.None);
}
dt.Rows.Add(aryline);
}
}
objReader.Close();
return dt;
}
public static string Lines_To_Replace(string lines)
{
for (int i = ; i < lines.Length; i++)
{
if (i != lines.Length - &&
i > &&
lines.Substring(i - , ) == "\t" &&
lines.Substring(i, ) == "\t" &&
lines.Substring(i + , ) == "\t")
{
//lines.Substring(i, 1) = "\0";
lines = lines.Substring(, i) + "\0" + lines.Substring(i + , lines.Length - i - );
}
}
return lines;
}
/// <summary>
/// 以空格键为间隔,没有空值
/// </summary>
/// <param name="dirTXT"></param>
/// <returns></returns>
public static DataTable TXT_To_DataTable3(string dirTXT)
{
StreamReader objReader = new StreamReader(dirTXT, Encoding.Default);
DataTable dt = new DataTable();
string strline;
bool OK = false;
while ((strline = objReader.ReadLine()) != null)
{
List<string> list = new List<string>();
string tmp = "";
while (strline.Contains(" "))
{
tmp = strline.Substring(, strline.IndexOf(" "));
strline = strline.Substring(strline.IndexOf(" ") + , strline.Length - strline.IndexOf(" ") - ).Trim();
if (!string.IsNullOrEmpty(tmp))
list.Add(tmp);
}
if (!string.IsNullOrEmpty(strline))
list.Add(strline);
if (!OK)
{
foreach (string str in list)
{
if (!string.IsNullOrEmpty(str))
{
DataColumn mydc = new DataColumn(str);
dt.Columns.Add(mydc);
}
}
OK = true;
}
else
dt.Rows.Add(list.ToArray());
}
objReader.Close();
return dt;
} /// <summary>
/// DataTable导出到TXT 以Tab为分隔符,以Tab表示空值
/// </summary>
/// <param name="table"></param>
/// <param name="fullName"></param>
/// <param name="Msg"></param>
/// <returns></returns>
public static bool DataTable_To_TXT2(DataTable table, string fullName, ref string Msg)
{
PathHelper.Check_Directory(fullName);
if (table == null || table.Rows.Count == )
{
Msg += fullName + " 对应的 DataTable 没有数据";
return false;
} FileStream fileStream = new FileStream(fullName, FileMode.Create);
StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.UTF8);
StringBuilder strBuilder = new StringBuilder(); for (int j = ; j < table.Columns.Count; j++)
{
string str1 = table.Columns[j].Caption; if (j == table.Columns.Count - )
{
strBuilder.AppendLine(str1);
}
else
{
str1 += "\t";
strBuilder.Append(str1);
}
} for (int i = ; i < table.Rows.Count - ; i++)
{
for (int j = ; j < table.Columns.Count; j++)
{
string str1 = table.Rows[i][j].NullableToString(); if (string.IsNullOrEmpty(str1) || str1 == "\0")
str1 = "\t"; if (j == table.Columns.Count - )
{
strBuilder.AppendLine(str1);
}
else
{
str1 += "\t";
strBuilder.Append(str1);
}
}
} streamWriter.WriteLine(strBuilder.ToString());
streamWriter.Close();
fileStream.Close();
return true;
}
public static bool DataTable_To_TXT(DataTable table, string fullName, ref string Msg)
{
PathHelper.Check_Directory(fullName);
if (table == null || table.Rows.Count == )
{
Msg += fullName + " 对应的 DataTable 没有数据";
return false;
}
int[] iColumnLength = new int[table.Columns.Count];
FileStream fileStream = new FileStream(fullName, FileMode.Create);
StreamWriter streamWriter = new StreamWriter(fileStream, System.Text.Encoding.Unicode);
StringBuilder strBuilder = new StringBuilder(); for (int i = ; i < table.Columns.Count; i++)
{
int iLength = ;
for (int j = ; j < table.Rows.Count; j++)
{
if (iLength < (table.Rows[j][i].ToString()).Length)
{
iLength = (table.Rows[j][i].ToString()).Length;
if ((table.Rows[j][i].ToString()).Length < table.Columns[i].Caption.Length)
{
iLength = table.Columns[i].Caption.Length;
}
} }
iColumnLength[i] = iLength + ;
} for (int j = ; j < table.Columns.Count; j++)
{
string str1 = table.Columns[j].Caption;
int iLength = str1.Length;
int iColumnWidth = iColumnLength[j] + ; for (int k = iLength; k < iColumnWidth; k++)
{
str1 += " ";
} if (j == table.Columns.Count - )
{
strBuilder.AppendLine(str1);
}
else
{
strBuilder.Append(str1);
}
} for (int i = ; i < table.Rows.Count - ; i++)
{
for (int j = ; j < table.Columns.Count; j++)
{
string str1 = table.Rows[i][j].NullableToString();
if (string.IsNullOrEmpty(str1))
str1 = "\0";
int iLength = str1.Length;
int iColumnWidth = iColumnLength[j] + ; for (int k = iLength; k < iColumnWidth; k++)
{
str1 += " ";
}
if (j == table.Columns.Count - )
{
strBuilder.AppendLine(str1);
}
else
{
strBuilder.Append(str1);
}
}
} streamWriter.WriteLine(strBuilder.ToString());
streamWriter.Close();
fileStream.Close();
return true;
}
//****************************************************************************************

TXT 与 DataTable 互转的更多相关文章

  1. List和DataTable互转

    /// <summary> /// List和DataTable互转 /// </summary> static class ListUtility { /// <sum ...

  2. json datatable互转(真正能用的-原创)

    网上有不少的转换类 可是不全 或者有错误 我现在贴一个 js 和C# 互转代码 希望能帮到需要的童鞋 首先C#转成 json /// <summary>         /// DataT ...

  3. C#实现XML与DataTable互转

    private string ConvertDataTableToXML(DataTable xmlDS) { MemoryStream stream = null; XmlTextWriter wr ...

  4. 集合List与DataTable互转

    /// <summary> /// 将泛类型集合List类转换成DataTable /// </summary> /// <param name="list&q ...

  5. C#读取txt文件返回DATATABLE

    //1.打开资源管理器 OpenFileDialog open = new OpenFileDialog(); if (open.ShowDialog() == DialogResult.OK) { ...

  6. DataTable,List,Dictonary互转,筛选及相关写法

    1.创建自定义DataTable  /// 创建自定义DataTable(一) 根据列名字符串数组, /// </summary> /// <param name="sLi ...

  7. List实体类、DataTable与xml互转

    序列化常用Attribute讲解说明 [XmlRootAttribute("MyCity", Namespace="abc.abc", IsNullable=f ...

  8. C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表

    调用word的com组件将400条数据导入word表格中耗时10分钟简直不能忍受,使用NPOI组件耗时4秒钟.但是NPOI中替换书签内容的功能不知道是不支持还是没找到. 辅助类 Excel表格数据与D ...

  9. DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化

    http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...

随机推荐

  1. 为什么Docker比虚拟机快?

    1.Docker有着比虚拟机更少的抽象层,由于Docker不需要Hypervisor实现硬件资源虚拟化,运行在Docker容器上的程序直接使用的都是实际物理机的硬件资源,因此在Cpu.内存利用率上Do ...

  2. Two Sum I & II & III & IV

    Two Sum I Given an array of integers, find two numbers such that they add up to a specific target nu ...

  3. IDE SATA SCSI iSCSI等存储硬盘对比与分析

    原文地址:http://blog.csdn.net/trochiluses/article/details/21229283 IDE是并口硬盘,(5400-7200转): SATA是串口硬盘,(720 ...

  4. oracle 存储过程 clob 字段 调试

    clob 没法直接赋值调试,可以新建一个存储过程,赋值给clob字段,然后调试

  5. CSS高度塌陷问题解决方案

    高度塌陷的存在:原因分析 1 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /& ...

  6. shell脚本中冒号

    格式:: your comment here 格式:# your comment here 写代码注释(单行注释). 例如: 格式:: 'comment line1 comment line2 mor ...

  7. 转载:2.1 运行中的Nginx进程间的关系《深入理解Nginx》(陶辉)

    原文:https://book.2cto.com/201304/19624.html 在正式提供服务的产品环境下,部署Nginx时都是使用一个master进程来管理多个worker进程,一般情况下,w ...

  8. webstrom随手笔记

    1.node代码智能提示功能: 2.页面代码格式化快捷键: Ctrl+Alt+L键,或者Code =>Rearrange code

  9. Oracle12c 性能优化攻略:攻略1-2:创建具有最优性能的表空间

    问题描述:    1:表空间是存储数据库对象(例如索引 .表)的逻辑容器.    2:在创建数据库对象不为其指定存储属性,则相应的表和索引会自动继承表空间的存储特性.    故:若需要好的索引.表的性 ...

  10. Spark的HA部署

    一.安装JDK.Scala 二.安装zookeeper 三.安装Hadoop 四.安装Spark 1.修改spark/conf/spark-env.sh export JAVA_HOME=/usr/j ...