IOExcelRead

Inheritance Hierarchy
  • Laga.IO
    • public class IOExcelRead()
      • IOReadRange()
      • IOReadCell()
      • IORead_SetActiveSheet()
      • TestExcelOpen()
      • IORead_ExcelWorksheetNames()
      • IORead_OpenExcelApp()
      • CloseExcelApp()
    • Properties
      • MatrixDataExcel
      • SheetNum
Class Constructor:
Syntax Parameters
IOExcelRead(string FilePath) FilePath: The file name
IOExcelRead(string FilePath, int SheetNumber) FilePath: The file name, SheetNumber: the excel sheet to read
IOExcelRead(string FilePath, int SheetNumber, string XlsxRange) FilePath: The file name, SheetNumber: the excel sheet to read, XlsxRange: the range of cells to read


Methods

IOReadRange(string strXlRange)

  • Description: Read the cells range specified in the parameter.
  • Parameters:
    • strXlRange: the range of cells to read, format “A1:B2” if is an empty string (“”) will read the whole data in the workbook.
  • Return: List<List<string>>

Example

string filename = @"C:\Users\delab\Documents\ioexcelread.xlsx";
            
IOExcelRead iOExcelRead = new IOExcelRead(filename); //start the class
iOExcelRead.IORead_OpenExcelApp(true); //open excel
  
iOExcelRead.IORead_SetActiveSheet(1, true); //from which page to read...
iOExcelRead.IOReadRange("A1:C30"); //the range to read
List<List<string>> dataExcel = iOExcelRead.MatrixDataExcel; //get the data

for (int i = 0; i < dataExcel.Count; i++)
Console.WriteLine(dataExcel[i][0] + ", " + dataExcel[i][1] + ", " + dataExcel[i][2]);

iOExcelRead.CloseExcelApp();
Console.ReadLine();

//result
1, 3.98306363839769, 1
2, 4.96018750724116, -7.86960440108936
3, 3.56495267776939, -12.6429528726791
4, 0.11438267235808, -15.7392088021787
5, -1.6530287197259, -17.9165117443133
6, 1.46509966439624, -19.5125572737685
7, 7.74049884462132, -20.7074824941596
8, 11.0564586265768, -21.6088132032681
9, 6.8506590207656, -22.2859057453582
10, -2.2986184553039, -22.7861161454027
11, -7.85829961846795, -23.1432215285704
12, -3.29728236241543, -23.3821616748578
13, 8.60376413233612, -23.5218761281312
14, 17.010095633318, -23.577086895249
15, 12.8959102559465, -23.5594646169925
16, -1.46486041305125, -23.4784176043574
17, -13.2021647083627, -23.3416412472936
18, -10.3761777883004, -23.1555101464476
19, 5.98925963718589, -22.9253640821914
20, 21.4004976681423, -22.6557205464921
21, 20.711361062847, -22.3504353668387
22, 2.94686384920091, -22.0128259296597
23, -16.3214766424391, -21.6457669901007
24, -18.5922880345692, -21.2517660759472
25, -0.167201098854532, -20.8330234886267
26, 22.9681123660595, -20.3914805292205
27, 28.9637427205114, -19.9288586180374
28, 10.7269547262101, -19.4466912963383
29, -16.1037899885863, -18.9463506099195
30, -26.4993560691961, -18.4290690180818

IOReadCell(string strXlCell)

  • Description: Read a specific excel cell.
  • Parameters:
    • strXlCell[string]: The cell to read in excel, format “A1”.
  • Return: string

Example

string filename = @"C:\Users\delab\Documents\ioexcelread.xlsx";
            
IOExcelRead iOExcelRead = new IOExcelRead(filename); //start the class
iOExcelRead.IORead_OpenExcelApp();
iOExcelRead.IORead_SetActiveSheet(1, false); //only get the data...

string A1 = iOExcelRead.IOReadCell("A1");
string strNoData = iOExcelRead.IOReadCell("E1");

iOExcelRead.CloseExcelApp();
            
Console.WriteLine("data in A1 cell: " + A1);
Console.WriteLine("No data found in cell E1: " + strNoData);

Console.ReadLine();

//result
data in A1 cell: 1
No data found in cell E1: != //when no data is found returns the operator "not equal": !=

IORead_SetActiveSheet(int pos, bool display)

  • Description: Read a specific excel cell.
  • Parameters:
    • pos[int]: The position of the excel sheet in the document.
    • display[bool]: decide to visualize the excel sheet.
  • Return: void

Example


TestExcelOpen()

  • Description: Test if excel application was opened by Laga.IO.
  • Parameters:
  • Return: bool

Example

string filename = @"C:\Users\delab\Documents\ioexcelread.xlsx";
            
IOExcelRead iOExcelRead = new IOExcelRead(filename); //start the class)
iOExcelRead.IORead_OpenExcelApp(true);

if(iOExcelRead.TestExcelOpen())
{
    Console.WriteLine("the file is open");
}
else
{
    Console.WriteLine("the file is closed");
}
iOExcelRead.CloseExcelApp();
Console.ReadLine();

//result
the file is closed

IORead_ExcelWorksheetNames()

  • Description: Open the App, return all the excel sheet names. Close the App and release the Marshalls.
  • Parameters:
  • Return: List<string>

Example

string filename = @"C:\Users\delab\Documents\ioexcelread.xlsx";
List<string> sheetnames = new List<string>();

IOExcelRead iOExcelRead = new IOExcelRead(filename);
sheetnames = iOExcelRead.IORead_ExcelWorksheetNames();

foreach(string s in sheetnames)
    Console.WriteLine(s);
            
Console.ReadLine();

//result
One
two
three

IORead_OpenExcelApp()

  • Description: Open excel app based on the constructor.
  • Parameters:
  • Return: void

Example


IORead_OpenExcelApp(bool display)

  • Description: Open excel app based on the constructor.
  • Parameters:
    • display[bool]: The position of the excel sheet in the document.
  • Return: void

Example


CloseExcelApp()

  • Description: Clean all the marshalls and kill excel app.
  • Parameters:
  • Return: void

Example


IORead_CloseExcelApp(bool saveFile)

  • Description: Clean all the marshalls and save - close the excel app.
  • Parameters:
    • saveFile[bool]: If is true, will save the file.
  • Return: void

Example


IORead_CloseExcelApp(bool saveFile, string fileName, string directory)

  • Description: Clean all the marshalls and save - close the excel app.
  • Parameters:
    • saveFile[bool]: If is true, will save the file.
    • fileName[string]: The file name to save the excel file.
    • directory[string]: The address where to sabe the excel file.
  • Return: void

Example



In progress / updated on: 2019/06/14