0

I have a problem with retrieving data from .csv file (windows-1251 encoding). I use Aspose Cells and here is my code:

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();
}

When I try to get data from the cell, I get a string with a strange characters

I've tried to change the encoding to different encodings, but that does not help. Can you help me to solve this problem, please?

iOne
  • 153
  • 2
  • 12
  • `Can you help me to solve this problem, please?` How can we help? Which code+data should we test? We don't have crystal ball. – L.B Mar 21 '14 at 22:05

1 Answers1

0

You can use TextLoadOptions to load an encoded CSV file in Aspose.Cells. Following is the sample code I used:

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();
}

Now, it works fine at my end with the latest Aspose.Cells version. In case you still face issue, share your CSV for testing.