首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >命名空间'Aspose.Cells‘中不存在类型或名称空间名称'Utility’(您是否缺少程序集引用?)

命名空间'Aspose.Cells‘中不存在类型或名称空间名称'Utility’(您是否缺少程序集引用?)
EN

Stack Overflow用户
提问于 2021-05-05 05:49:12
回答 2查看 671关注 0票数 1

海伊..。我现在正面临困境。

我需要开发一个程序,其中一个功能是它可以将JSON文件转换成CSV文件。

我现在拥有的是:

代码语言:javascript
复制
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'中的‘实用程序’。我一直在网上搜索,但这似乎没什么大不了的。

我只知道“Aspose.Cells.Utility.JsonLayoutOptions”位于Aspose.Cells.dll中的“Aspose.Cells.Utility”命名空间中。DLL没什么问题一切都很好..。只是错误仍然存在:

错误消息:名称空间'Aspose.Cells‘中不存在类型或名称空间名称'Utility’(您缺少程序集引用吗?)

EN

回答 2

Stack Overflow用户

发布于 2021-05-05 07:34:47

除了jayrag提供的答案之外,我想说的是,Apsose.Cells.Utility似乎是一个较新的名称空间,因此它不能用于早期版本的Aspose。我只是检查了它的版本17,在那里,它肯定是不可用的。我在Aspose.Cells中也找不到任何可以帮助您的替代方法。简单地说,json支持似乎还很新,要使用它,您需要升级许可证。考虑到阿什的价格,你也许应该去做个调整。希望这能帮点忙。干杯!

票数 1
EN

Stack Overflow用户

发布于 2021-05-05 06:02:22

我认为您不需要Aspose来创建csv,尝试如下:

代码语言:javascript
复制
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();
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67395803

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档