C# 类型或命名空间名称';实用工具';命名空间中不存在';Aspose.Cells';(是否缺少程序集引用?)

C# 类型或命名空间名称';实用工具';命名空间中不存在';Aspose.Cells';(是否缺少程序集引用?),c#,asp.net,json,visual-studio-2013,namespaces,C#,Asp.net,Json,Visual Studio 2013,Namespaces,嗨。。我现在正面临困境 我需要开发一个程序,它的许多功能之一就是可以将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.

嗨。。我现在正面临困境

我需要开发一个程序,它的许多功能之一就是可以将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, 0, 0, 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”(是否缺少程序集引用?我认为创建csv不需要Aspose 试试这个:

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();
 }
private void按钮\u单击(对象发送者,路由目标)
{
字符串json=File.ReadAllText(@“C:\cpi\log\V510ResultInfo\cpi001.json”);
var model=JsonConvert.DeserializeObject(json,新的JsonSerializerSettings{NullValueHandling=NullValueHandling.Ignore});
InsertRecordInCsv(模型,“convertedjson”);
}
公共静态void InsertRecordInCsv(T模型,字符串文件名)
{
字符串targetFolder=@“C:\cpi\log\V510ResultInfo\”;//fileSaveFolderName
如果(!Directory.Exists(targetFolder))
{
目录.CreateDirectory(targetFolder);
}
字符串targetPath=Path.Combine(targetFolder,文件名+“.csv”);
如果(!File.Exists(targetPath))
{
var fs=new FileStream(targetPath,FileMode.Create);
fs.Close();
string csvHeader=string.Empty;
foreach(typeof(T).GetProperties()中的PropertyInfo信息)
{
csvHeader+=(!string.IsNullOrEmpty(csvHeader)?“,”:string.Empty)+info.Name;
}
csvHeader+=Environment.NewLine;
文件.AppendAllText(targetPath,csvHeader);
}
StringBuilder sbData=新的StringBuilder();
string csvModel=string.Empty;
foreach(typeof(T).GetProperties()中的PropertyInfo信息)
{
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);
AppendAllText(targetPath,sbData.ToString());
}
公共静态对象GetPropValue(对象src,字符串propName)
{
if(src.GetType().GetProperty(propName)!=null)
返回src.GetType().GetProperty(propName).GetValue(src,null);
返回新对象();
}

除了jayrag提供的答案之外,我想说的是,apose.Cells.Utility似乎是一个较新的名称空间,因此它不适用于较旧版本的Aspose。我刚刚检查了它的版本17,但它肯定不可用。我在Aspose.Cells中也找不到任何有帮助的替代方法你。看来json支持还是很新的,要使用它,你需要升级许可证。考虑到aspose的价格,你可能需要做一些变通。希望这对你有所帮助。
干杯!

嘿,老兄!首先感谢你这么快回复我……我对代码可以执行并不感到惊讶!当我按下按钮时,它会创建一个CSV文件……但问题是CSV文件中没有数据:(好的,我知道怎么了……它是:sbData.Replace(“,”,Environment.NewLine,sbData.Length-1,1);但我不知道为什么..您创建了
Common.GetPropValue()
函数吗?我创建了..公共字符串名称{get{return model;}set{Name=value;}}}刚刚更新了答案检查添加了
GetPropValue()
function我就是这么想的,这就是为什么我找不到任何东西的原因。Haizz谢谢你,是的,将JSON导入Excel和将Excel导出JSON都是较新的功能,所以你必须升级到并使用更新版本的Aspose.Cells来执行你的代码段.PS。我在Aspose担任支持开发人员/布道者。