107 lines
3.5 KiB
C#
107 lines
3.5 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.Office.Interop.Excel; // zusätzlich ist der Projektverweis auf COM -> Microsoft Excel xx-Objectlibrary
|
|
|
|
namespace UpdateStandPartFamMemUsStat
|
|
{
|
|
class Excel
|
|
{
|
|
public Excel(string fileName)
|
|
{
|
|
FileName = fileName;
|
|
}
|
|
|
|
//oExcelApp benötigt den kompletten Namensraum, da andernfalls der Compiler den Unterschied zwischen
|
|
//"Microsoft.Office.Interop.Excel.Application" und "System.Windows.Forms.Application" nicht erkennen kann
|
|
private Microsoft.Office.Interop.Excel.Application oExcelApp = new Microsoft.Office.Interop.Excel.Application();
|
|
private Workbook oWorkbook = null;
|
|
private Worksheet oWorksheet = null;
|
|
|
|
Range oRange;
|
|
|
|
private string FileName { set; get; }
|
|
|
|
public int RangeColumns { private set; get; }
|
|
|
|
public List<object> ReadColumn(string columnName)
|
|
{
|
|
int colNo = ColumnNumber(columnName); //Spaltennummer
|
|
List<object> columnValues = new List<object>();
|
|
|
|
// Datei öffnen und aktives Blatt und benutzten Bereich auswählen
|
|
oWorkbook = oExcelApp.Workbooks.Open(FileName);
|
|
oWorksheet = (Worksheet)oWorkbook.ActiveSheet;
|
|
oRange = oWorksheet.UsedRange;
|
|
|
|
RangeColumns = oRange.Rows.Count;
|
|
|
|
for (int i = 2; i < RangeColumns + 1; i++)
|
|
{
|
|
columnValues.Add((oRange.Cells[i, colNo] as Range).Value);
|
|
}
|
|
|
|
return columnValues;
|
|
}
|
|
|
|
public void WriteColumn(string columnName, List<object> columnValues, List<bool> changeStatus)
|
|
{
|
|
// Datei öffnen und aktives Blatt und benutzten Bereich auswählen
|
|
oWorkbook = oExcelApp.Workbooks.Open(FileName);
|
|
oWorksheet = (Worksheet)oWorkbook.ActiveSheet;
|
|
oRange = oWorksheet.UsedRange;
|
|
|
|
RangeColumns = oRange.Rows.Count;
|
|
int colValue = ColumnNumber(columnName);
|
|
|
|
//Save Columns
|
|
for (int i = 1; i < RangeColumns - 1; i++)
|
|
{
|
|
if (columnValues[i] != null & (bool)changeStatus[i] == true)
|
|
{
|
|
(oWorksheet.Cells[i + 2, colValue] as Range).Value = columnValues[i];
|
|
(oWorksheet.Cells[i + 2, colValue + 1] as Range).Value = "Yes";
|
|
}
|
|
}
|
|
|
|
oWorkbook.Save();
|
|
}
|
|
|
|
public void CloseSheet()
|
|
{
|
|
if (oWorkbook != null)
|
|
{
|
|
oWorkbook.Close();
|
|
}
|
|
}
|
|
|
|
public int ColumnNumber(string columnName)
|
|
{
|
|
//Range oRange;
|
|
|
|
// Datei öffnen und aktives Blatt und benutzten Bereich auswählen
|
|
oWorkbook = oExcelApp.Workbooks.Open(FileName);
|
|
oWorksheet = (Worksheet)oWorkbook.ActiveSheet;
|
|
oRange = oWorksheet.UsedRange;
|
|
|
|
int colomnNo = -1;
|
|
string test;
|
|
for (int i = 1; i < oRange.Columns.Count; i++)
|
|
{
|
|
test = oWorksheet.Cells[1, i].Value;
|
|
if (oWorksheet.Cells[1, i].Value != null)
|
|
{
|
|
if (oWorksheet.Cells[1, i].Value.ToString().ToLower() == columnName.ToLower())
|
|
{
|
|
colomnNo = i;
|
|
}
|
|
}
|
|
}
|
|
|
|
return colomnNo; //-1 wenn Spalte nicht gefunden wurde
|
|
}
|
|
}
|
|
}
|