Aspose.cells异步读写操作

时间:2024-08-18 16:36:56
 public class AsyncExcel : Excel
{
static readonly object _objForlock = new object();
//public List<IAsyncResult> ReadAsyncResults
//{
// get;
// set;
//} //public List<IAsyncResult> WriteAsyncResults
//{
// get;
// set;
//}
public override void Read()
{
if (string.IsNullOrEmpty(this.Path) || !System.IO.File.Exists(this.Path))
{
throw new Exception(string.Format("文件“{0}”为空或不存在。", this.Path));
}
//ReadAsyncResults = new List<IAsyncResult>();
Workbook wb = new Workbook();
wb.Open(this.Path);
MemoryDataTables = new System.Data.DataTable[wb.Worksheets.Count];
object[] rowArrData;
for (var i = ; i < wb.Worksheets.Count; i++)
{
MemoryDataTables[i] = new System.Data.DataTable();
MemoryDataTables[i].TableName = wb.Worksheets[i].Name;
for (var j = ; j < wb.Worksheets[i].Cells.MaxColumn; j++)
{
//row = MemoryDataTables[i].NewRow();
//MemoryDataTables[i].Rows.Add(row);
rowArrData = new object[wb.Worksheets[i].Cells.MaxColumn];
for (var k = ; k < wb.Worksheets[i].Cells.MaxColumn; k++)
{
rowArrData[k] = wb.Worksheets[i].Cells[j, k].Value;
if (CellAction != null)
{
CellAction.BeginInvoke(rowArrData[k], null, null);
}
}
if (RowAction != null)
{
RowAction.BeginInvoke(rowArrData, null, null);
}
if (DataTableRowOpAction != null)
{
DataTableRowOpAction.BeginInvoke(rowArrData, null, null);
} }
if (DataTableAction != null)
{
DataTableAction.BeginInvoke(MemoryDataTables[i], null, null);
}
}
wb = null;
} public override void Write()
{
if (string.IsNullOrEmpty(this.Path))
{
throw new Exception(string.Format("保存路径“{0}”为空。", this.Path));
}
if (MemoryDataTables == null || MemoryDataTables.Length <= )
{
throw new Exception("没有数据写入。");
}
//WriteAsyncResults = new List<IAsyncResult>();
Workbook wb = new Workbook();
for (var i = ; i < MemoryDataTables.Length; i++)
{
wb.Worksheets.Add(MemoryDataTables[i].TableName);
wb.Worksheets[i].Cells.ImportDataRow(MemoryDataTables[i].Rows[], , wb.Worksheets[i].Cells.MinColumn);
for (var j = ; j < MemoryDataTables[i].Rows.Count; j++)
{
for (var k = ; k < MemoryDataTables[i].Columns.Count; k++)
{
wb.Worksheets[i].Cells[j + , k].PutValue(MemoryDataTables[i].Rows[j][k]);
if (CellAction != null)
{
CellAction.BeginInvoke(wb.Worksheets[i].Cells[j + , k], null, null);
}
}
if (RowAction != null)
{
RowAction.BeginInvoke(MemoryDataTables[i].Rows[j].ItemArray, null, null);
}
if (DataTableRowOpAction != null)
{
DataTableRowOpAction.BeginInvoke(MemoryDataTables[i].Rows[j].ItemArray, null, null);
}
}
if (DataTableAction != null)
{
DataTableAction.BeginInvoke(MemoryDataTables[i], null, null);
}
}
if (!System.IO.Directory.Exists(System.IO.Path.GetDirectoryName(this.Path)))
{
System.IO.Directory.CreateDirectory(System.IO.Path.GetDirectoryName(this.Path));
}
wb.Save(System.IO.Path.GetFullPath(this.Path));
} public Action<object[]> RowAction
{
get;
set;
} /// <summary>
/// 异步不会自动添加DataRow到DataTable,如果需要异步执行的最后结果,请在这个Action把Row添加到DataTable
/// </summary>
public Action<object[]> DataTableRowOpAction
{
get;
set;
} public Action<object> CellAction
{
get;
set;
} public Action<System.Data.DataTable> DataTableAction
{
get;
set;
}
}