Reading csv files with Aspose.Cells (Aspose 2.2)

Hi,

I am using Aspose.Cells (Aspose 2.2). Is it possible to read the content of a csv file line by line with Aspose.Cells? I want to implement it in C#, so could you give me a small code snippet where I am reading the contents of csv file line by line and save each line string in an array?

Thank you for your support!

Regards,

Ian

Hi,


Thank you for using Aspose.Cells

You don’t need to import a CSV file line by line. Aspose.Cells API provides you with easy methods to import your CSV files using few lines of code as follow:


//Instantiate LoadOptions specified by the LoadFormat.
LoadOptions loadOptions = new LoadOptions(LoadFormat.CSV);

//Create a Workbook object and opening the file from its path
Workbook wb = new Workbook(“E://SourceCSV.csv”, loadOptions);

Please download and use this latest version of Aspose.Cells i.e. Aspose.Cells for .NET v7.2.1.4.You can also visit our Documentation Page here for additional notes and examples on different topics.

Hi,


If you need to save the contents of a CSV file to an Array, please see the following sample code for your reference. I have loaded a sample CSV file and then export the contents to a two dimensional array, finally I have added a new worksheet to the workbook and import the two dimensional array to that sheet. I save an XLSX file lastly.

Sample code:

TxtLoadOptions loadOptions = new TxtLoadOptions(LoadFormat.Csv);
loadOptions.ConvertNumericData = false;

Workbook workbook = new Workbook(“e:\test2\Test_CSV.csv”, loadOptions);
Worksheet sheet = workbook.Worksheets[0];

object[,] dataArray = sheet.Cells.ExportArray(0, 0, sheet.Cells.MaxDataRow +1, sheet.Cells.MaxDataColumn +1);

if (dataArray != null)
{
Console.WriteLine("Array Length " + dataArray.Length);
}


Worksheet sheet2 = workbook.Worksheets[workbook.Worksheets.Add()];
sheet2.Cells.ImportTwoDimensionArray(dataArray,0,0);
workbook.Save(“e:\test2\outTest_1.xlsx”, SaveFormat.Xlsx);

Thank you!

How do I add the Aspose.Cells dll file and the TBL file into my C# Project? Currently I am having following files in my C# Project:

Aspose.Cells (xml file)

Aspose.Cells.dll

Do I just copy and replace them with the delivered files?

Hi,


You just need to replace the Aspose.Cells.Dll file (only) in your project. The Aspose.Cells.xml file is for documentation purpose that describes the types/APIs. Regarding TLB file, well, it is type library file, you may open the Aspose.Cells.TLB file into VS.NET 2010 and check the overloaded methods types to see the exact definitions. This file is useful if you need to know and use the types/APIs on COM (e.g use Aspose.Cells for .NET on classic ASP (with VBScript / JavaScript etc.).


Thank you.

Hi,


when i try to add the namespace LoadOptions in my code, I am getting the message “namespace cannot be found”. Is it possible that LoadOptions is not supported in the current Aspose.Cells which I am using now? I am using the following Aspose.Cells version:

Version 4.8.0.0
Runtime Verision v2.0.50727

Thanks!

Hi,

Yes, your are right. You will have to upgrade to latest version:
Aspose.Cells
for NET v7.2.1.4


Please download and use it.

I will also recommend you to download and try the latest offline demos. These demos will be helpful in upgrading your code if you encounter any problems.

Also these demos can work with Visual Studio 2005, 2008 or 2010. Please read the readme.txt file before running the demos.