/// <summary> /// 对源数据进行排版,来让其中的Id集体与指定的目标Id集合相对应。 /// </summary> /// <param name="SourceResult">要进行排版的数据源</param> /// <param name="DestinationIdList">要匹配到的目标Id集合</param> /// <returns></returns> /// <remarks></remarks> private static DataResultArray SortResult(DataResultList SourceResult, List <long> DestinationIdList) { int NodeCount = DestinationIdList.Count; object[,] NodeId = new object[NodeCount - 1 + 1, 1]; object[,] X = new object[NodeCount - 1 + 1, 1]; object[,] Y = new object[NodeCount - 1 + 1, 1]; object[,] Z = new object[NodeCount - 1 + 1, 1]; //---------------------- 开始进行数据排版 ------------------------------ int SourceIndex = 0; int DestiIndex = 0; // DataResultList with_1 = SourceResult; List <long> SourceNode = with_1.NodeId; for (SourceIndex = 0; SourceIndex <= SourceNode.Count - 1; SourceIndex++) { DestiIndex = DestinationIdList.IndexOf(SourceNode[SourceIndex], DestiIndex); if (DestiIndex < 0) { // 搜索原理: // 这里有一个前提假设:在SourceNode与DestinationIdList中,节点的Id都是从小到大排列的。 // 所以,一旦SourceNode中的节点在DestinationIdList集体中找不到对应的匹配节点号,则SourceNode中后面的节点都不会找到匹配的节点号了。 break; } else { // 将数据放置在对应的位置上 NodeId[DestiIndex, 0] = with_1.NodeId[SourceIndex]; X[DestiIndex, 0] = with_1.X[SourceIndex]; Y[DestiIndex, 0] = with_1.Y[SourceIndex]; Z[DestiIndex, 0] = with_1.Z[SourceIndex]; } } //--------------------------------------------------------------------- DataResultArray Res = new DataResultArray(); Res.NodeId = NodeId; Res.X = X; Res.Y = Y; Res.Z = Z; return(Res); }
/// <summary> /// 将一个dat文本中的所有数据写入对应的Worksheet中。 /// </summary> /// <param name="AllData">一个dat文本中的所有数据。注意:在一个文本中, /// 记录节点位置的坐标点的数量与记录节点位移的坐标点的数量并不一定是相同的。</param> /// <param name="wkSheet">要进行写入的工作表</param> /// <remarks></remarks> private static void WriteDataToExcel(List <DataResultList> AllData, Worksheet wkSheet) { Application app = wkSheet.Application; int BlockCount = AllData.Count; uint RowsCount = 0; // 每一个数据块的数据行数 DataResultList results = new DataResultList(); // 先写入测点的位置信息 results = AllData[0]; RowsCount = (uint)results.NodeId.Count; wkSheet.Range["A1:D1"].Value = new[] { "ID", "X", "Y", "Z" }; wkSheet.Range[wkSheet.Cells[cstRow_FirstData, 1], wkSheet.Cells[cstRow_FirstData + RowsCount - 1, 1]].Value = app.WorksheetFunction.Transpose(results.NodeId.ToArray()); wkSheet.Range[wkSheet.Cells[cstRow_FirstData, 2], wkSheet.Cells[cstRow_FirstData + RowsCount - 1, 2]].Value = app.WorksheetFunction.Transpose(results.X.ToArray()); wkSheet.Range[wkSheet.Cells[cstRow_FirstData, 3], wkSheet.Cells[cstRow_FirstData + RowsCount - 1, 3]].Value = app.WorksheetFunction.Transpose(results.Y.ToArray()); wkSheet.Range[wkSheet.Cells[cstRow_FirstData, 4], wkSheet.Cells[cstRow_FirstData + RowsCount - 1, 4]].Value = app.WorksheetFunction.Transpose(results.Z.ToArray()); //写入X数据 var startColumn = 5; for (int BlockNum = 1; BlockNum <= BlockCount - 1; BlockNum++) { startColumn++; results = AllData[BlockNum]; // 将 DataResultArray SortedRes = SortResult(results, AllData[0].NodeId); RowsCount = (uint)SortedRes.Count; wkSheet.Range[ wkSheet.Cells[cstRow_FirstData, startColumn], wkSheet.Cells[cstRow_FirstData + RowsCount - 1, startColumn]].Value = SortedRes.X; // app.WorksheetFunction.Transpose(results.X.ToArray) wkSheet.Range[ wkSheet.Cells[cstRow_FirstData, startColumn + BlockCount], wkSheet.Cells[cstRow_FirstData + RowsCount - 1, startColumn + BlockCount]].Value = SortedRes.Y; // app.WorksheetFunction.Transpose(results.Y.ToArray) wkSheet.Range[ wkSheet.Cells[cstRow_FirstData, startColumn + BlockCount * 2], wkSheet.Cells[cstRow_FirstData + RowsCount - 1, startColumn + BlockCount * 2]].Value = SortedRes.Z; // app.WorksheetFunction.Transpose(results.Z.ToArray) } try { wkSheet.Activate(); wkSheet.Range[wkSheet.Cells[1, 1], wkSheet.Cells[1, startColumn + BlockCount * 2]].Select(); if (wkSheet.AutoFilterMode) { app.Selection.AutoFilter(); app.Selection.AutoFilter(); } else { app.Selection.AutoFilter(); } } catch (Exception) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine( "Warnning : Can not set the autofilter for the worksheet: {0}, but it will not affect the extraction of data.", wkSheet.Name); Console.ForegroundColor = ConsoleColor.Gray; } }