-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy pathReadFromExcelToRange.cs
More file actions
71 lines (60 loc) · 2.32 KB
/
ReadFromExcelToRange.cs
File metadata and controls
71 lines (60 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//Read from Excel to Range, excel C# to range C# to array, cell to array
using System;
using Excel = Microsoft.Office.Interop.Excel;
class StartUp
{
static void Main()
{
string filePath = @"C:\Sample.xlsx";
int rowsCount = 5;
int colsCount = 6;
Excel.Application excel = new Excel.Application();
excel.Visible = false;
excel.EnableAnimations = false;
Excel.Workbook wkb = Open(excel, filePath);
Excel.Worksheet wk = (Excel.Worksheet)excel.Worksheets.get_Item(1);
Excel.Range startCell = wk.Cells[1, 1];
Excel.Range endCell = wk.Cells[rowsCount, colsCount];
Excel.Range currentRange = wk.get_Range(startCell, endCell).Cells;
currentRange.Interior.Color = Excel.XlRgbColor.rgbWhite;
object[,] matrixRead = (object[,])currentRange.Value;
bool[,] matrixResult = new bool[rowsCount+1,colsCount+1];
for (int rows = 1; rows <= rowsCount; rows++)
{
for (int cols = 1; cols < colsCount; cols++)
{
if (matrixRead[rows,cols].ToString()==matrixRead[rows,cols+1].ToString())
{
matrixResult[rows, cols] = true;
matrixResult[rows, cols + 1] = true;
}
}
}
for (int rows = 1; rows <= rowsCount; rows++)
{
for (int cols = 1; cols <= colsCount; cols++)
{
if (matrixResult[rows, cols])
{
currentRange.Cells[rows, cols].interior.color =
Excel.XlRgbColor.rgbRed;
}
}
}
excel.EnableAnimations = true;
wkb.Close(true);
excel.Quit();
Console.WriteLine("Finished!");
}
private static Excel.Workbook Open(Excel.Application excelInstance,
string fileName, bool readOnly = false,
bool editable = true, bool updateLinks = true)
{
Excel.Workbook book = excelInstance.Workbooks.Open(
fileName, updateLinks, readOnly,
Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, editable, Type.Missing, Type.Missing, Type.Missing,
Type.Missing, Type.Missing);
return book;
}
}