Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 编码冲突_.net_String_Encoding_Aspose Cells - Fatal编程技术网

.net 编码冲突

.net 编码冲突,.net,string,encoding,aspose-cells,.net,String,Encoding,Aspose Cells,我在从.csv文件(windows-1251编码)检索数据时遇到问题。我使用Aspose单元格,下面是我的代码: public static IEnumerable<ElmarketByExcelModel> ElmarketByParce(string filePath) { if (String.IsNullOrEmpty(filePath)) throw new ArgumentException("should be not null", "fileP

我在从.csv文件(windows-1251编码)检索数据时遇到问题。我使用Aspose单元格,下面是我的代码:

public static IEnumerable<ElmarketByExcelModel> ElmarketByParce(string filePath)
{
    if (String.IsNullOrEmpty(filePath))
        throw new ArgumentException("should be not null", "filePath");

    //Opening an existing workbook
    var workbook = new Workbook(filePath);

    //Accessing first worksheet
    var worksheet = workbook.Worksheets[0];

    var cells = worksheet.Cells;

    var list = new List<ElmarketByExcelModel>(worksheet.Cells.Rows.Count);
    for (var i = 2; i < cells.Rows.Count + 1; i++)
    {
        var model = new ElmarketByExcelModel();

        var value = cells["A" + i].Value.ToString();

        var newValue = GetString(Encoding.Convert(Encoding.GetEncoding(workbook.Settings.Encoding.HeaderName), Encoding.Default, GetBytes(value)));

        var values = newValue.Split(';');
        foreach (var val in values)
        {
            Console.WriteLine(val);
        }

        list.Add(model);
    }

    return list.AsEnumerable();
}
公共静态IEnumerable ElmarketByParce(字符串文件路径)
{
if(String.IsNullOrEmpty(filePath))
抛出新ArgumentException(“应不为null”、“文件路径”);
//打开现有工作簿
var工作簿=新工作簿(文件路径);
//访问第一个工作表
var工作表=工作簿。工作表[0];
变量单元格=工作表单元格;
var list=新列表(工作表.单元格.行.计数);
对于(变量i=2;i
当我试图从单元格中获取数据时,我得到一个带有奇怪字符的字符串

我尝试将编码更改为不同的编码,但这没有帮助。
您能帮我解决这个问题吗?

您可以使用TextLoadOptions在Aspose.Cells中加载编码的CSV文件。以下是我使用的示例代码:

TxtLoadOptions txtLoadOptions = new TxtLoadOptions(Aspose.Cells.LoadFormat.CSV);
txtLoadOptions.Encoding = Encoding.GetEncoding("windows-1251");

//Opening an existing workbook
var workbook = new Workbook("C:\\data\\Encoding.csv", txtLoadOptions);

//Accessing first worksheet
var worksheet = workbook.Worksheets[0];

var cells = worksheet.Cells;

for (var i = 1; i < cells.Rows.Count + 1; i++)
{
    var value = cells["A" + i].Value.ToString();
}
txtloadpoptions txtloadpoptions=新的txtloadpoptions(Aspose.Cells.LoadFormat.CSV);
txtloadpoptions.Encoding=Encoding.GetEncoding(“windows-1251”);
//打开现有工作簿
var工作簿=新工作簿(“C:\\data\\Encoding.csv”,txtloadpoptions);
//访问第一个工作表
var工作表=工作簿。工作表[0];
变量单元格=工作表单元格;
对于(变量i=1;i

现在,最新版本在我这边运行良好。如果您仍然面临问题,请共享您的CSV以进行测试。

您能帮助我解决此问题吗?
我们如何帮助?我们应该测试哪些代码+数据?我们没有水晶球。