Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void SaveFile()
- {
- if (Data.Count > 0)
- {
- var excelApp = new Excel.Application();
- excelApp.Visible = false;
- excelApp.DisplayAlerts = false;
- excelApp.Workbooks.Add();
- Excel._Worksheet workSheet = (Excel.Worksheet)excelApp.ActiveSheet;
- GenerateHeaderLine(workSheet);
- int line = 2;
- foreach (T item in Data)
- {
- for (int i = 0, j = Headers.Count; i < j; i++)
- {
- object value = item.GetType().GetProperty(Headers[i]).GetValue(item, null);
- if (value == null)
- {
- value = "";
- }
- int s = 65 + i;
- char c = (char)s;
- workSheet.Cells[line, c.ToString()] = value.ToString();
- }
- line++;
- }
- string fullPath = Path.Combine(Dir, FileName);
- if (File.Exists(fullPath))
- {
- File.Delete(fullPath);
- }
- excelApp.ActiveWorkbook.SaveCopyAs(fullPath);
- excelApp.ActiveWorkbook.Close();
- excelApp.Quit();
- excelApp = null;
- System.Diagnostics.Process.GetProcessesByName("EXCEL").ToList().ForEach((x) =>
- {
- x.Kill();
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement