【问题标题】:The type or namespace name 'Utility' does not exist in the namespace 'Aspose.Cells' (are you missing an assembly reference?)命名空间“Aspose.Cells”中不存在类型或命名空间名称“Utility”(您是否缺少程序集引用?)
【发布时间】:2021-07-27 10:50:03
【问题描述】:

嘿嘿……我现在正面临着泡菜。

我需要开发一个程序,其中一个功能是它可以将 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.dll 中的“Aspose.Cells.Utility”命名空间中。DLL 没有任何问题,一切都很好。只是错误仍然存​​在:

错误消息:命名空间“Aspose.Cells”中不存在类型或命名空间名称“Utility”(您是否缺少程序集引用?)

【问题讨论】:

    标签: c# asp.net json visual-studio-2013 namespaces


    【解决方案1】:

    除了 jayrag 提供的答案之外,我想说的是,Apsose.Cells.Utility 似乎是一个较新的命名空间,因此它不适用于旧版本的 Aspose。我刚刚检查了它的版本 17,它肯定不可用。我在 Aspose.Cells 中也找不到任何可以帮助您的替代方法。似乎对 json 的支持仍然很新,要使用它,您需要升级许可证。鉴于 aspose 的价格,您可能应该采取一种解决方法。希望有点帮助。 干杯!

    【讨论】:

    • 这就是我的想法,这就是为什么我无法找到任何东西.. 无论如何谢谢 Haizz
    • 是的,将 JSON 导入 Excel 和将 Excel 导出为 JSON 是较新的功能,因此您必须升级到并使用较新版本的 Aspose.Cells 来执行您的代码段。 PS。我在 Aspose 担任支持开发人员/布道者。
    【解决方案2】:

    我认为您不需要 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();
     }
    

    【讨论】:

    • 嘿伙计!首先感谢您这么快回复我。我对代码可以执行并不感到惊讶!当我按下按钮时,它会创建一个 CSV 文件。但问题是 CSV 文件中没有数据:(
    • 好的,我知道出了什么问题。它是:sbData.Replace(",", Environment.NewLine, sbData.Length - 1, 1);但我不知道为什么..
    • 你创建Common.GetPropValue()函数了吗?
    • 我做了.. public string Name { get { return model; } 设置 { 名称 = 值; } }
    • 刚刚更新的答案检查添加了GetPropValue()函数
    猜你喜欢
    • 2015-02-14
    • 2017-05-04
    • 2018-07-15
    • 2021-08-12
    • 2012-07-26
    • 2019-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多