问题描述
嘿嘿..我现在正面临一个泡菜。
我需要开发一个程序,其中一个功能是可以将 JSON 文件转换为 CSV 文件..
这是我所拥有的:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Aspose;
using Aspose.Cells;
namespace WpfApplication1
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender,RoutedEventArgs e)
{
string str = File.ReadAllText(@"C:\cpi\log\V510ResultsInfo\cpi001.json");
Aspose.Cells.Workbook workbook = new Aspose.Cells.Workbook();
Aspose.Cells.Cells cells = workbook.Worksheets[0].Cells;
Aspose.Cells.Utility.JsonLayoutoptions importOptions = new
Aspose.Cells.Utility.JsonLayoutoptions();
importOptions.ConvertNumericOrDate = true;
importOptions.ArrayAsTable = true;
importOptions.IgnoreArrayTitle = true;
importOptions.IgnoreObjectTitle = true;
Aspose.Cells.Utility.JsonUtility.ImportData(str,cells,importOptions);
workbook.Save(@"C:\cpi\log\V510ResultsInfo\convertedjson.csv");
}
}
}
问题出在 'Aspose.Cells.Utility' 中的 'Utility' .. 一直在网上搜索,但似乎没什么大不了的..
我只知道“Aspose.Cells.Utility.JsonLayoutoptions”位于“Aspose.Cells.Utility”命名空间中,该命名空间位于 Aspose.Cells.dll 中。DLL 没有任何问题,一切都很好。只是错误仍然存在:
错误消息:命名空间“Aspose.Cells”中不存在类型或命名空间名称“Utility”(您是否缺少程序集引用?)
解决方法
添加到 jayrag 提供的答案中,我想说的是,Apsose.Cells.Utility 似乎是一个较新的命名空间,因此它不适用于旧版本的 Aspose。我刚刚检查了它的版本 17,它绝对不可用。我也无法在 Aspose.Cells 中找到任何可以帮助您的替代方法。似乎 json 支持仍然很新,要使用它,您需要升级许可证。鉴于 aspose 的价格,您可能应该进行变通。希望有点帮助。 干杯!
,我认为你不需要 Aspose 来创建 csv 试试这个:
private void Button_Click(object sender,RoutedEventArgs e)
{
string json = File.ReadAllText(@"C:\cpi\log\V510ResultsInfo\cpi001.json");
var model = JsonConvert.DeserializeObject<MyModel>(json,new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore });
InsertRecordInCsv(model,"convertedjson");
}
public static void InsertRecordInCsv<T>(T model,string fileName)
{
string targetFolder = @"C:\cpi\log\V510ResultsInfo\";//fileSaveFolderName
if (!Directory.Exists(targetFolder))
{
Directory.CreateDirectory(targetFolder);
}
string targetPath = Path.Combine(targetFolder,fileName + ".csv");
if (!File.Exists(targetPath))
{
var fs = new FileStream(targetPath,FileMode.Create);
fs.Close();
string csvHeader = string.Empty;
foreach (PropertyInfo info in typeof(T).GetProperties())
{
csvHeader += (!string.IsNullOrEmpty(csvHeader) ? "," : string.Empty) + info.Name;
}
csvHeader += Environment.NewLine;
File.AppendAllText(targetPath,csvHeader);
}
StringBuilder sbData = new StringBuilder();
string csvModel = string.Empty;
foreach (PropertyInfo info in typeof(T).GetProperties())
{
string value = GetPropValue(model,info.Name) != null ? GetPropValue(model,info.Name).ToString() : string.Empty;
sbData.Append("\"" + value.Replace("\"","\"\"") + "\",");
}
sbData.Replace(",",Environment.NewLine,sbData.Length - 1,1);
File.AppendAllText(targetPath,sbData.ToString());
}
public static object GetPropValue(object src,string propName)
{
if (src.GetType().GetProperty(propName) != null)
return src.GetType().GetProperty(propName).GetValue(src,null);
return new object();
}