Initial Project Commit
This commit is contained in:
85
Business/ExcelReader.cs
Normal file
85
Business/ExcelReader.cs
Normal file
@@ -0,0 +1,85 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using Excel = Microsoft.Office.Interop.Excel;
|
||||
|
||||
namespace MergeCMInpro.Business
|
||||
{
|
||||
internal class ExcelReader
|
||||
{
|
||||
public static string ReadExcel(string filename, string vorgangId)
|
||||
{
|
||||
Excel.Application excel = null;
|
||||
excel = new Excel.Application();
|
||||
excel.Visible = true;
|
||||
Excel.Workbook wkb = null;
|
||||
|
||||
wkb = Open(excel, filename);
|
||||
Excel.Range searchedRange = excel.get_Range("A1", "XFD1048576");
|
||||
Excel.Range currentFind = searchedRange.Find(vorgangId);
|
||||
Excel.Sheets excelSheets = wkb.Worksheets;
|
||||
Excel.Worksheet excelWorksheet = (Excel.Worksheet)excelSheets.get_Item(1);
|
||||
string res = string.Empty;
|
||||
|
||||
if (currentFind != null)
|
||||
{
|
||||
var cellValue = Convert.ToString((excelWorksheet.Cells[currentFind.Row, currentFind.Column+10] as Excel.Range).Value);
|
||||
DateTime dt = DateTime.Parse(cellValue);
|
||||
res = dt.ToShortDateString();
|
||||
}
|
||||
else
|
||||
{
|
||||
res = "01.01.1900";
|
||||
//TODO: Logger, falls nicht gefunden. Und sonst wird 01.01.1900 als default gesetzt
|
||||
}
|
||||
|
||||
wkb.Close(true);
|
||||
excel.Quit();
|
||||
return res;
|
||||
}
|
||||
|
||||
public 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;
|
||||
}
|
||||
|
||||
public static void ReleaseComObjects(ref List<object> comObjects)
|
||||
{
|
||||
for (int i = 0; i < comObjects.Count; i++)
|
||||
{
|
||||
object obj = comObjects[i];
|
||||
if (Marshal.IsComObject(obj))
|
||||
{
|
||||
while (obj != null && Marshal.ReleaseComObject(obj) != 0) ;
|
||||
while (obj != null && Marshal.FinalReleaseComObject(obj) != 0) ;
|
||||
}
|
||||
obj = null;
|
||||
}
|
||||
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
}
|
||||
|
||||
public static int GetColumnNumber(string column)//A --> 1 B --> 2 CI --> 87
|
||||
{
|
||||
int col = 0;
|
||||
char[] temp = column.ToUpper().ToCharArray();
|
||||
for (int i = temp.Length - 1; i >= 0; i--)
|
||||
{
|
||||
int num = Convert.ToInt32(temp[i]) - 64;
|
||||
col += num * Convert.ToInt32(Math.Pow(26, temp.Length - 1 - i));
|
||||
}
|
||||
return col;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user