291 lines
11 KiB
C#
291 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using Microsoft.Office.Interop.Excel; // zusätzlich ist der Projektverweis auf COM -> Microsoft Excel xx-Objectlibrary
|
|
|
|
|
|
namespace Test_ExcelReadColumn
|
|
{
|
|
public partial class Form1 : Form
|
|
{
|
|
#region Version und Copyright
|
|
// Version und Copyright
|
|
string programName = Path.GetFileNameWithoutExtension(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase); //Den Programmnamen auslesen
|
|
string programVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();
|
|
static object[] attributes = Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(AssemblyCopyrightAttribute), false);
|
|
string copyright = ((AssemblyCopyrightAttribute)attributes[0]).Copyright;
|
|
string icon = System.Windows.Forms.Application.StartupPath + "\\Info.bmp";
|
|
//Assembly Datum und Zeit
|
|
static DateTime value = AssemblyDateTime();
|
|
string date = value.ToShortDateString();
|
|
string time = value.ToLongTimeString();
|
|
private static DateTime AssemblyDateTime()
|
|
{
|
|
var version = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
|
|
var buildDateTime = new DateTime(2000, 1, 1).Add(new TimeSpan(TimeSpan.TicksPerDay * version.Build + TimeSpan.TicksPerSecond * 2 * version.Revision));
|
|
//Tage seit dem 1. Januar 2000 und Sekunden seit Mitternacht, (multiplizier mit 2 ergibt das Original)
|
|
return buildDateTime;
|
|
}
|
|
#endregion
|
|
|
|
#region Programm-Info
|
|
/// <summary>
|
|
/// Contains the name of the program file
|
|
/// </summary>
|
|
public static string ProgramName { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the version of the program file
|
|
/// </summary>
|
|
public static string ProgramVersion { set; get; }
|
|
|
|
/// <summary>
|
|
/// Contains the copyright notice
|
|
/// </summary>
|
|
public static string Copyright { set; get; }
|
|
|
|
private void SetProgramInfo()
|
|
{
|
|
//Name, Version und Copyright setzen
|
|
ProgramName = programName;
|
|
ProgramVersion = programVersion;
|
|
Copyright = copyright;
|
|
}
|
|
#endregion
|
|
|
|
const string colItemNumber = "ItemNumber";
|
|
const string colStatus = "Status";
|
|
|
|
//Dateiname
|
|
private string FileName { set; get; }
|
|
|
|
//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;
|
|
|
|
|
|
public Form1()
|
|
{
|
|
SetProgramInfo(); //Name, Version und Copyright setzen
|
|
InitializeComponent();
|
|
}
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
{
|
|
this.MaximumSize = new Size(808, Screen.PrimaryScreen.Bounds.Height - 30); //Um 30 kleiner, weil sonst der untere Rand hinter der Start-Leiste verschwindet
|
|
}
|
|
|
|
private void buttonReadSapColumn_Click(object sender, EventArgs e)
|
|
{
|
|
//Datei öffnen
|
|
string fileName = ReadFileName("Browse SAP Excel File");
|
|
|
|
if (!String.IsNullOrEmpty(fileName))
|
|
{
|
|
buttonReadSapColumn.Enabled = false;
|
|
|
|
int rangeSAPcolumns = 0;
|
|
List<string> columnObject = new List<string>();
|
|
List<string> columnVStat = new List<string>();
|
|
|
|
var colObject = new ColumnValues(fileName);
|
|
|
|
WriteColumnToDataGrid(colObject.ReadValues("Objekt"), colObject.ReadValues("Objekt").Count, 0, true);
|
|
rangeSAPcolumns = colObject.ReadValues("Objekt").Count;
|
|
WriteColumnToDataGrid(colObject.ReadValues("VStat"), rangeSAPcolumns, 1, true);
|
|
TranslateColValue(rangeSAPcolumns);
|
|
colObject.CloseExcel();
|
|
|
|
buttonReadReuseColumns.Enabled = true;
|
|
}
|
|
}
|
|
|
|
private void buttonReadReuseColumns_Click(object sender, EventArgs e)
|
|
{
|
|
//Datei öffnen
|
|
FileName = ReadFileName("Browse Reuse Excel File");
|
|
|
|
if (!String.IsNullOrEmpty(FileName))
|
|
{
|
|
buttonReadReuseColumns.Enabled = false;
|
|
|
|
int rangeReuseColumns = 0;
|
|
List<string> columnItem = new List<string>();
|
|
List<string> columnOldStatus = new List<string>();
|
|
List<string> columnNewStatus = new List<string>();
|
|
|
|
var colItem = new ColumnValues(FileName);
|
|
|
|
WriteColumnToDataGrid(colItem.ReadValues("ItemNumber"), colItem.ReadValues("ItemNumber").Count, 0, false);
|
|
rangeReuseColumns = colItem.ReadValues("ItemNumber").Count;
|
|
WriteColumnToDataGrid(colItem.ReadValues("Status"), rangeReuseColumns, 1, false);
|
|
MatchValues();
|
|
//colItem.CloseExcel();
|
|
|
|
buttonSaveChanges.Enabled = true;
|
|
}
|
|
}
|
|
|
|
private void buttonSaveChanges_Click(object sender, EventArgs e)
|
|
{
|
|
int range = dataGridViewExcelReuse.Rows.Count;
|
|
List<string> status = new List<string>();
|
|
|
|
for (int i = 1; i < range; i++)
|
|
{
|
|
if (dataGridViewExcelReuse.Rows[i].Cells[3].Value != null && (bool)dataGridViewExcelReuse.Rows[i].Cells[3].Value == true)
|
|
{
|
|
status.Add(dataGridViewExcelReuse.Rows[i].Cells[2].Value.ToString());
|
|
}
|
|
else
|
|
{
|
|
status.Add("");
|
|
}
|
|
}
|
|
|
|
var colValues = new ColumnValues(FileName);
|
|
|
|
colValues.SaveChanges(status);
|
|
|
|
//if (oWorkbook != null)
|
|
//{
|
|
// oWorkbook.Close();
|
|
//}
|
|
|
|
buttonSaveChanges.Enabled = false;
|
|
buttonReadSapColumn.Enabled = true;
|
|
}
|
|
|
|
private string ReadFileName(string title)
|
|
{
|
|
openFileDialogExcel = new OpenFileDialog();
|
|
openFileDialogExcel.Title = title;
|
|
openFileDialogExcel.DefaultExt = "xlsx";
|
|
openFileDialogExcel.Filter = "Excel file (*.xlsx)|*.xlsx|All files (*.*)|*.*";
|
|
openFileDialogExcel.FilterIndex = 1;
|
|
openFileDialogExcel.CheckFileExists = true;
|
|
openFileDialogExcel.CheckPathExists = true;
|
|
openFileDialogExcel.ShowDialog();
|
|
|
|
return openFileDialogExcel.FileName;
|
|
}
|
|
|
|
private void WriteColumnToDataGrid(List<string> columnObject, int rangeColObject, int columnNumber, bool sap)
|
|
{
|
|
if (sap)
|
|
{
|
|
if (dataGridViewExcelSAP.Rows.Count == 1)
|
|
{
|
|
for (int j = 1; j < rangeColObject; j++)
|
|
{
|
|
dataGridViewExcelSAP.Rows.Add(); //Zeilen einfügen
|
|
}
|
|
}
|
|
|
|
for (int j = 0; j < rangeColObject; j++)
|
|
{
|
|
dataGridViewExcelSAP.Rows[j].Cells[columnNumber].Value = columnObject[j];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (dataGridViewExcelReuse.Rows.Count == 1)
|
|
{
|
|
for (int j = 1; j < rangeColObject; j++)
|
|
{
|
|
dataGridViewExcelReuse.Rows.Add(); //Zeilen einfügen
|
|
}
|
|
}
|
|
|
|
for (int j = 0; j < rangeColObject; j++)
|
|
{
|
|
dataGridViewExcelReuse.Rows[j].Cells[columnNumber].Value = columnObject[j];
|
|
}
|
|
}
|
|
}
|
|
|
|
private void TranslateColValue(int rangeSAPcolumns)
|
|
{
|
|
string temp = "";
|
|
|
|
for (int i = 0; i < rangeSAPcolumns; i++)
|
|
{
|
|
temp = dataGridViewExcelSAP.Rows[i].Cells[1].Value.ToString();
|
|
temp = temp.Remove(temp.LastIndexOf(")"));
|
|
temp = temp.Remove(0, temp.IndexOf("(") + 1);
|
|
dataGridViewExcelSAP.Rows[i].Cells[2].Value = temp;
|
|
}
|
|
}
|
|
|
|
private void MatchValues()
|
|
{
|
|
string value = "";
|
|
int range = dataGridViewExcelReuse.Rows.Count;
|
|
|
|
//for (int i = 0; i < oRangeReuse.Rows.Count; i++)
|
|
for (int i = 0; i < range; i++)
|
|
{
|
|
//Zeilenweise die Spalte 1 einlesen. Wenn der Wert nicht '<Kein Wert>' ist, dann in der SAP Liste Spalte 1 durchsuchen ob der Wert existiert. Existiert der Wert, dann den Wert aus Spalte 3 in die Reuse Liste Spalte 3 schreiben und Spalte 4 setzen.
|
|
if (dataGridViewExcelReuse.Rows[i].Cells[0].Value != null)
|
|
{
|
|
if (dataGridViewExcelReuse.Rows[i].Cells[0].Value.ToString() != "<Kein Wert>")
|
|
{
|
|
value = DetermineValue(dataGridViewExcelReuse.Rows[i].Cells[0].Value.ToString());
|
|
|
|
if (dataGridViewExcelReuse.Rows[i].Cells[1].Value.ToString() != value)
|
|
{
|
|
if (value != "")
|
|
{
|
|
dataGridViewExcelReuse.Rows[i].Cells[2].Value = value;
|
|
dataGridViewExcelReuse.Rows[i].Cells[3].Value = true;
|
|
}
|
|
else
|
|
{
|
|
dataGridViewExcelReuse.Rows[i].Cells[2].Value = dataGridViewExcelReuse.Rows[i].Cells[1].Value;
|
|
}
|
|
|
|
}
|
|
else
|
|
{
|
|
dataGridViewExcelReuse.Rows[i].Cells[2].Value = dataGridViewExcelReuse.Rows[i].Cells[1].Value;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private string DetermineValue(string value)
|
|
{
|
|
string newValue = "";
|
|
string objectSAP = "";
|
|
|
|
for (int j = 0; j < dataGridViewExcelSAP.Rows.Count; j++)
|
|
{
|
|
objectSAP = dataGridViewExcelSAP.Rows[j].Cells[0].Value.ToString();
|
|
if (objectSAP != null)
|
|
{
|
|
if (objectSAP == value)
|
|
{
|
|
newValue = dataGridViewExcelSAP.Rows[j].Cells[2].Value.ToString();
|
|
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
return newValue;
|
|
}
|
|
}
|
|
}
|