Last active
August 29, 2015 14:19
-
-
Save Yiabiten/bac9c141c48d9cd2e594 to your computer and use it in GitHub Desktop.
Read from Excel file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using Microsoft.Office.Interop.Excel; | |
/* | |
* NOTE: | |
* Ajoute une Réference vers "Microsoft Office Object Library" | |
* sinon t'auras des erreurs ! | |
*/ | |
namespace Excel_prog | |
{ | |
class Program | |
{ | |
private static void Main(string[] args) | |
{ | |
var app = new Application(); | |
//chemin vers le fichier: | |
string file = "sheet.xlsx"; | |
Workbook wrk = app.Workbooks.Open(file); | |
read(wrk); | |
wrk.Close(false); | |
Marshal.ReleaseComObject(wrk); | |
app.Quit(); | |
Console.ReadKey(); | |
} | |
private static void read(Workbook workBookIn) | |
{ | |
Worksheet sheet = (Worksheet)workBookIn.Sheets[1]; | |
Range excelRange = sheet.UsedRange; | |
object[,] valueArray = (object[,])excelRange.get_Value( | |
XlRangeValueDataType.xlRangeValueDefault); | |
process(valueArray); | |
} | |
private static void process(object[,] arr) | |
{ | |
Console.WriteLine("La zone utilisée est de taille:{0}x{1}", arr.GetLength(0), arr.GetLength(1)); | |
for (int row = 1; row < arr.GetLength(0)+1; row++) | |
{ | |
for (int col = 1; col < arr.GetLength(1)+1; col++) | |
{ | |
if (arr[row,col]==null) | |
{ | |
arr[row, col] = "-"; | |
} | |
Console.Write(arr[row, col]+"\t\t"); | |
} | |
Console.WriteLine(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment