-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectListViewExtensions.cs
More file actions
74 lines (68 loc) · 2.46 KB
/
ObjectListViewExtensions.cs
File metadata and controls
74 lines (68 loc) · 2.46 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
72
73
74
using BrightIdeasSoftware;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Excel = Microsoft.Office.Interop.Excel;
namespace XXX
{
public static class ObjectListViewExtensions
{
/// <summary>
/// 內容匯出Excel
/// </summary>
/// <param name="olv"></param>
public static void ExportToExcel(this ObjectListView olv)
{
if (olv.Items.Count == 0)
return;
// 初始化 Excel
Excel.Application excelApp = new Excel.Application { };
Excel.Workbook workbook = excelApp.Workbooks.Add();
Excel.Worksheet worksheet = workbook.Sheets[1];
try
{
// 將 OLV 的 Column Header 填入 Excel
for (int i = 0; i < olv.Columns.Count; i++)
{
worksheet.Cells[1, i + 1] = olv.AllColumns[i].Text; // Header Text
}
// 將 OLV 的資料填入 Excel
for (int row = 0; row < olv.Items.Count; row++)
{
for (int col = 0; col < olv.Columns.Count; col++)
{
worksheet.Cells[row + 2, col + 1] = olv.GetModelObject(row)
.GetType()
.GetProperty(olv.AllColumns[col].AspectName)?
.GetValue(olv.GetModelObject(row))?.ToString() ?? "";
}
}
// 自動調整欄寬
worksheet.Columns.AutoFit();
// 顯示 Excel
excelApp.Visible = true;
}
catch (Exception ex)
{
MessageBox.Show("Error:" + ex.Message);
}
finally
{
workbook = null;
worksheet = null;
excelApp = null;
}
}
/// <summary>
/// ObjectListView 取得勾選項目轉指定資料模型列舉
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="olv"></param>
/// <returns></returns>
public static IEnumerable<T> CheckedList<T>(this ObjectListView olv) where T : class
{
return olv.CheckedObjects.Cast<object>().OfType<T>().ToList();
}
}
}