VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > c#编程 >
  • C#关于Excel文件转TXT文本的实现

没什么可介绍的,但是要注意文件的地址别写错了,如下是具体实现代码:

复制代码
private DataTable FileToDataTable(string fileName,string sheetName)
        {
            Monster m = new Monster();
            DataSet dst = new DataSet();
            DataTable dt = new DataTable();

            dt = m.XlsxToDataTable(fileName, sheetName);

            return dt;
        }
        public ExecutionResult DataTableToTxt(DataTable vContent, string vOutputFilePath)
        {
            ExecutionResult sResult = new ExecutionResult();
            StringBuilder sTxtContent;

            try
            {
                if (File.Exists(vOutputFilePath))
                    File.Delete(vOutputFilePath);

                sTxtContent = new StringBuilder();

                //数据
                foreach (DataRow row in vContent.Rows)
                {
                    for (int i = 0; i < vContent.Columns.Count; i++)
                    {
                        sTxtContent.Append(row[i].ToString().Trim());
                        sTxtContent.Append(i == vContent.Columns.Count - 1 ? "\r\n" : "\t");
                    }
                }
                File.WriteAllText(vOutputFilePath, sTxtContent.ToString(), Encoding.Unicode);
                sResult.Status = true;
            }
            catch (Exception ex)
            {
                sResult.Message = ex.Message;
                sResult.Status = false;
            }
            return sResult;
        }
复制代码

 


相关教程