1 Star 4 Fork 0

许先生/Tools

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
Program.cs 2.20 KB
一键复制 编辑 原始数据 按行查看 历史
xu 提交于 5年前 . csv 转成 json
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
namespace CSV2JS
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("拖入CSV文件...");
string path = Console.ReadLine();
string outPath = path.Substring(0, path.LastIndexOf('\\')) + @"\_data.js";
OpenCSV(path, outPath);//读写取csv
Console.WriteLine("按任意键结束...");
Console.ReadLine();
}
public static void OpenCSV(string filePath ,string outPath)
{
FileStream fs = new FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
StreamReader sr = new StreamReader(fs, Encoding.UTF8);
//记录每次读取的一行记录
string strLine;
string[] keys = { };
List<List<string>> values = new List<List<string>>();
bool isFirst = false;
List<string> outLines = new List<string>();
//写入信息
outLines.Add("var _data = {");
//逐行读取CSV中的数据
while ((strLine = sr.ReadLine()) != null)
{
if (isFirst == false)
{
isFirst = true;
keys = strLine.Split(",");
}
else
{
//{ id_1 : {key1:value1}, }
string[] value = strLine.Split(",");
string writeStr = "";
writeStr += ("\"" + keys[0] + "_" + value[0] + "\":{" );
for (int i = 1; i < value.Length; ++i)
{
writeStr += "\"" + keys[i] + "\":" + "\"" + value[i] + "\",";
}
writeStr = writeStr.Substring(0, writeStr.Length - 1);
writeStr += @"},";
outLines.Add(writeStr);
}
}
sr.Close();
fs.Close();
outLines.Add("}");
System.IO.File.WriteAllLines(outPath, outLines);
Console.WriteLine("输出路径:" + outPath);
}
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/xuxsheng/Tools.git
git@gitee.com:xuxsheng/Tools.git
xuxsheng
Tools
Tools
master

搜索帮助