指定路径批量将xls转换成csv

 PS : 用到spire库,.net控制台应用程序

    其实本来没打算写这个工具的,只是最近需要用到,手头上正好没有这样的工具,那么怎么办,写呗!

    其实说白了就是省事,策划想怎么玩,把表把工具丢给他,省得策划两分钟来拍一次你的后背,哈哈哈!

    下面附上代码,注释得很清楚,我挑需要注意的说一下就好...

  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Threading.Tasks;
  6 using System.IO;
  7 using Spire.Xls;
  8 using Spire.Xls.Collections;
  9 
 10 namespace ExcelTools
 11 {
 12     class Program
 13     {
 14         /// <summary>
 15         /// 程序入口
 16         /// </summary>
 17         /// <param name="args"></param>
 18         static void Main(string[] args)
 19         {
 20             try
 21             {
 22                 //exe同级目录下的pathconfig配置文件
 23                 string dataPath = Directory.GetCurrentDirectory() + @"\PathConfig.txt";
 24                 //用流方式读取配置文件
 25                 StreamReader streamReader = new StreamReader(dataPath);
 26                 //因为只设置了一行,用逗号隔开,所以只读一行
 27                 string msg = streamReader.ReadLine();
 28                 //将两个路径分开
 29                 string[] getPath = msg.Split(',');
 30                 //第一个是输入路径
 31                 string inPath = getPath[0];
 32                 //第二是输出路径
 33                 string outPath = getPath[1];
 34                 //读完后关闭加载器
 35                 streamReader.Close();
 36                 Console.WriteLine("文件路径:" + inPath);
 37                 Console.WriteLine("转换存放路径:" + outPath);
 38                 List<string> xlsPathList = new List<string>();//用来接指定文件路径
 39                 List<string> tmpList = new List<string>();//递归过渡缓存
 40                 //拿到指定xls路径列表
 41                 xlsPathList = GetAllXls(inPath, tmpList);
 42                 //遍历转换
 43                 for (int i = 0; i < xlsPathList.Count; i++)
 44                 {
 45                     Console.WriteLine(string.Format("开始转换{0}...", xlsPathList[i]));
 46                     StartConver(xlsPathList[i], outPath);
 47                 }
 48                 Console.WriteLine("全部转换已经完成!");
 49                 Console.ReadLine();
 50                 Console.Clear();
 51             }
 52             catch (Exception e)
 53             {
 54                 Console.WriteLine(e.Message);
 55                 Console.ReadLine();
 56                 return;
 57             }
 58         }
 59         /// <summary>
 60         /// 开始转换
 61         /// </summary>
 62         /// <param name="inPath">指定文件路径</param>
 63         /// <param name="outPath">转换完成输出路径</param>
 64         static void StartConver(string inPath, string outPath)
 65         {
 66             try
 67             {
 68                 Workbook work = new Workbook();//实例化一个对象
 69                 work.LoadFromFile(inPath);//调用对象方法,加载指定文件
 70                 WorksheetsCollection sheetArr = work.Worksheets;//拿到这个xls所有的sheet
 71                 //遍历所有的sheet
 72                 for (int i = 0; i < sheetArr.Count; i++)
 73                 {
 74                     string printPath = outPath + sheetArr[i].Name + ".csv";//输出路径指定命名
 75                     sheetArr[i].SaveToFile(printPath, ",", Encoding.UTF8);//保存转换完成的文件到路径下,设置编码格式
 76                     Console.WriteLine(string.Format("转换完成{0}", sheetArr[i].Name));
 77                 }
 78             }
 79             catch (Exception e)
 80             {
 81                 Console.WriteLine(e.Message);
 82                 Console.ReadLine();
 83             }
 84         }
 85         /// <summary>
 86         /// 获取指定路径下所有指定类型的文件路径
 87         /// </summary>
 88         /// <param name="inPath">指定文件夹路径</param>
 89         /// <param name="fileList">路径列表</param>
 90         /// <returns></returns>
 91         static List<string> GetAllXls(string inPath, List<string> fileList)
 92         {
 93             string fileName;
 94             DirectoryInfo dir = new DirectoryInfo(inPath);//实例化路径对象
 95             FileInfo[] fil = dir.GetFiles();//从对象上拿到所有的文件列表
 96             DirectoryInfo[] dii = dir.GetDirectories();//得到当前路径的所有子目录
 97             //遍历文件列表,拿到指定类型文件
 98             foreach (FileInfo f in fil)
 99             {
100                 fileName = f.FullName;
101                 if (fileName.EndsWith("xls"))
102                 {
103                     fileList.Add(fileName);
104                 }
105             }
106             //这里是递归,用来拿到子目录里面还有文件里面的指定文件
107             foreach (DirectoryInfo d in dii)
108             {
109                 GetAllXls(d.FullName, fileList);
110             }
111             return fileList;
112         }
113     }
114 }

(1)路劲问题

      你要转文件,肯定得有一个起点一个终点,不推荐在代码里面写死路径,这样可以保证通用性,我写得比较懒,直接把两个路径写成配置文件,间接明了

      这里需要注意的是,stream这些文件流在使用完成后必须关闭;Directory.GetCurrentDirectory() 这个就是你的exe所在文件夹路径

指定路径批量将xls转换成csv

 

 

 

 (2)用递归去获取xls文件是为了防止有些表分类很细很细,这里也是写死了是xls

指定路径批量将xls转换成csv

 

 

(3)到这里转换就需要用到spire库,主要的转换功能就在它,就在它,就在它!重要的事情说三遍

指定路径批量将xls转换成csv

 

 指定路径批量将xls转换成csv

 

 (3)最后就可以运行了,我是直接转到unity里读取,当然你也可以再继续转成json文件放到unity里,其实都是一样的

上一篇:查找文件中的中文并替换,以支持程序中英文(国际化)


下一篇:跨C/C++(非托管)和托管C#代码共享通用定义