Aspose.cells 비동기식 읽기 및 쓰기 작업
21153 단어 읽기 및 쓰기 작업
1 public class AsyncExcel : Excel
2 {
3 static readonly object _objForlock = new object();
4 //public List<IAsyncResult> ReadAsyncResults
5 //{
6 // get;
7 // set;
8 //}
9
10 //public List<IAsyncResult> WriteAsyncResults
11 //{
12 // get;
13 // set;
14 //}
15 public override void Read()
16 {
17 if (string.IsNullOrEmpty(this.Path) || !System.IO.File.Exists(this.Path))
18 {
19 throw new Exception(string.Format(" “{0}” 。", this.Path));
20 }
21 //ReadAsyncResults = new List<IAsyncResult>();
22 Workbook wb = new Workbook();
23 wb.Open(this.Path);
24 MemoryDataTables = new System.Data.DataTable[wb.Worksheets.Count];
25 object[] rowArrData;
26 for (var i = 0; i < wb.Worksheets.Count; i++)
27 {
28 MemoryDataTables[i] = new System.Data.DataTable();
29 MemoryDataTables[i].TableName = wb.Worksheets[i].Name;
30 for (var j = 0; j < wb.Worksheets[i].Cells.MaxColumn; j++)
31 {
32 //row = MemoryDataTables[i].NewRow();
33 //MemoryDataTables[i].Rows.Add(row);
34 rowArrData = new object[wb.Worksheets[i].Cells.MaxColumn];
35 for (var k = 0; k < wb.Worksheets[i].Cells.MaxColumn; k++)
36 {
37 rowArrData[k] = wb.Worksheets[i].Cells[j, k].Value;
38 if (CellAction != null)
39 {
40 CellAction.BeginInvoke(rowArrData[k], null, null);
41 }
42 }
43 if (RowAction != null)
44 {
45 RowAction.BeginInvoke(rowArrData, null, null);
46 }
47 if (DataTableRowOpAction != null)
48 {
49 DataTableRowOpAction.BeginInvoke(rowArrData, null, null);
50 }
51
52 }
53 if (DataTableAction != null)
54 {
55 DataTableAction.BeginInvoke(MemoryDataTables[i], null, null);
56 }
57 }
58 wb = null;
59 }
60
61
62 public override void Write()
63 {
64 if (string.IsNullOrEmpty(this.Path))
65 {
66 throw new Exception(string.Format(" “{0}” 。", this.Path));
67 }
68 if (MemoryDataTables == null || MemoryDataTables.Length <= 0)
69 {
70 throw new Exception(" 。");
71 }
72 //WriteAsyncResults = new List<IAsyncResult>();
73 Workbook wb = new Workbook();
74 for (var i = 0; i < MemoryDataTables.Length; i++)
75 {
76 wb.Worksheets.Add(MemoryDataTables[i].TableName);
77 wb.Worksheets[i].Cells.ImportDataRow(MemoryDataTables[i].Rows[0], 0, wb.Worksheets[i].Cells.MinColumn);
78 for (var j = 0; j < MemoryDataTables[i].Rows.Count; j++)
79 {
80 for (var k = 0; k < MemoryDataTables[i].Columns.Count; k++)
81 {
82 wb.Worksheets[i].Cells[j + 1, k].PutValue(MemoryDataTables[i].Rows[j][k]);
83 if (CellAction != null)
84 {
85 CellAction.BeginInvoke(wb.Worksheets[i].Cells[j + 1, k], null, null);
86 }
87 }
88 if (RowAction != null)
89 {
90 RowAction.BeginInvoke(MemoryDataTables[i].Rows[j].ItemArray, null, null);
91 }
92 if (DataTableRowOpAction != null)
93 {
94 DataTableRowOpAction.BeginInvoke(MemoryDataTables[i].Rows[j].ItemArray, null, null);
95 }
96 }
97 if (DataTableAction != null)
98 {
99 DataTableAction.BeginInvoke(MemoryDataTables[i], null, null);
100 }
101 }
102 if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(this.Path)))
103 {
104 System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(this.Path));
105 }
106 wb.Save(System.IO.Path.GetFullPath(this.Path));
107 }
108
109
110 public Action<object[]> RowAction
111 {
112 get;
113 set;
114 }
115
116 /// <summary>
117 /// DataRow DataTable, , Action Row DataTable
118 /// </summary>
119 public Action<object[]> DataTableRowOpAction
120 {
121 get;
122 set;
123 }
124
125 public Action<object> CellAction
126 {
127 get;
128 set;
129 }
130
131 public Action<System.Data.DataTable> DataTableAction
132 {
133 get;
134 set;
135 }
136 }