//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); workbook.ActiveSheet.Range["A:B"].ColumnWidth = 35; workbook.ActiveSheet.Range["A2:B2"].HorizontalAlignment = HorizontalAlignment.Right; //Number of times to read data. int count = 10; #region ImportData //Create a new workbook. var importDataWorkbook = new GrapeCity.Documents.Excel.Workbook(); //Total time consumed to import data "count-2" times. long importDataTotalTimeConsumed = 0; for (int i = 0; i < count; i++) { var importDataFileStream = GetResourceStream("xlsx\\AgingReport.xlsx"); //Start time to read Excel data. var importDatasw = new System.Diagnostics.Stopwatch(); importDatasw.Start(); //Import data of the range from the fileStream. var data = GrapeCity.Documents.Excel.Workbook.ImportData(importDataFileStream, "Aging Report", 8, 2, 21, 7); // Assign the data to current workbook importDataWorkbook.Worksheets[0].Range[0, 0, 21, 7].Value = data; //End time to read Excel data. importDatasw.Stop(); //The operating system has a cache to open the file, and it takes time to open the file for the first time. //For the accuracy of statistics, the time of the first and last time is removed. if (i > 0 && i < count - 1) { importDataTotalTimeConsumed += importDatasw.ElapsedMilliseconds; } } //Average time consumed to read data using "importData" method. double importDataTimeConsumed = (double)importDataTotalTimeConsumed / (count - 2); //Save to an excel file. (replace MemoryStream with your file path) importDataWorkbook.Save(new MemoryStream()); #endregion #region Open //Create a new workbook. var openWorkbook = new GrapeCity.Documents.Excel.Workbook(); //Total time consumed to open Excel file "count-2" times. long openTotalTimeConsumed = 0; for (int i = 0; i < count; i++) { var openFileStream = GetResourceStream("xlsx\\AgingReport.xlsx"); //Start time to read Excel data. var opensw = new System.Diagnostics.Stopwatch(); opensw.Start(); //Import Excel data using "Open" Method. openWorkbook.Open(openFileStream); //End time to read Excel data. opensw.Stop(); //The operating system has a cache to open the file, and it takes time to open the file for the first time. //For the accuracy of statistics, the time of the first and last time is removed. if (i > 0 && i < count - 1) { openTotalTimeConsumed += opensw.ElapsedMilliseconds; } } //Average time consumed to import Excel data using "Open" method. double openTimeConsumed = (double)openTotalTimeConsumed / (count - 2); //Save to an excel file (replace MemoryStream with your file path) openWorkbook.Save(new MemoryStream()); #endregion workbook.ActiveSheet.Range["A1"].Value = "Time consumed by ImportData method"; workbook.ActiveSheet.Range["B1"].Value = "Time consumed by Open method"; //Store the time information of ImportData method in cell A2. workbook.ActiveSheet.Range["A2"].Value = importDataTimeConsumed.ToString() + "ms"; //Store the time information of Open method in cell B2. workbook.ActiveSheet.Range["B2"].Value = openTimeConsumed.ToString() + "ms"; // Save to an excel file workbook.Save("ImportDataForRange.xlsx");
' Create a new Workbook Dim workbook As New Workbook workbook.ActiveSheet.Range("A:B").ColumnWidth = 35 workbook.ActiveSheet.Range("A2:B2").HorizontalAlignment = HorizontalAlignment.Right 'Number of times to read data. Dim count As Integer = 10 #Region "ImportData" 'Create a New workbook. Dim importDataWorkbook = New GrapeCity.Documents.Excel.Workbook() 'Total time consumed to import data "count-2" times. Dim importDataTotalTimeConsumed As Long = 0 For i = 0 To count - 1 Dim importDataFileStream = GetResourceStream("xlsx\\AgingReport.xlsx") 'Start time to read Excel data. Dim importDatasw = New System.Diagnostics.Stopwatch() importDatasw.Start() 'Import data of the range from the fileStream. Dim data = GrapeCity.Documents.Excel.Workbook.ImportData(importDataFileStream, "Aging Report", 8, 2, 21, 7) 'Assign the data to current workbook importDataWorkbook.Worksheets(0).Range(0, 0, 21, 7).Value = data 'End time to read Excel data. importDatasw.Stop() 'The operating system has a cache to open the file, And it takes time to open the file for the first time. 'For the accuracy of statistics, the time of the first And last time Is removed. If i > 0 And i < count - 1 Then importDataTotalTimeConsumed += importDatasw.ElapsedMilliseconds End If Next 'Average time consumed to read data using "importData" method. Dim importDataTimeConsumed As Double = importDataTotalTimeConsumed / (count - 2) 'Save to an Excel file. (replace MemoryStream with your file path) importDataWorkbook.Save(New MemoryStream) #End Region #Region "Open" 'Create a New workbook. Dim openWorkbook = New GrapeCity.Documents.Excel.Workbook() 'Total time consumed to open Excel file "count-2" times. Dim openTotalTimeConsumed As Long = 0 For i = 0 To count - 1 Dim openFileStream = GetResourceStream("xlsx\\AgingReport.xlsx") 'Start time to read Excel data. Dim opensw = New System.Diagnostics.Stopwatch() opensw.Start() 'Import Excel data using "Open" Method. openWorkbook.Open(openFileStream) 'End time to read Excel data. opensw.Stop() 'The operating system has a cache to open the file, And it takes time to open the file for the first time. 'For the accuracy of statistics, the time of the first And last time Is removed. If i > 0 And i < count - 1 Then openTotalTimeConsumed += opensw.ElapsedMilliseconds End If Next 'Average time consumed to import Excel data using "Open" method. Dim openTimeConsumed As Double = openTotalTimeConsumed / (count - 2) 'save to an Excel file. (replace MemoryStream with your file path) openWorkbook.Save(New MemoryStream) #End Region workbook.ActiveSheet.Range("A1").Value = "Time consumed by ImportData method" workbook.ActiveSheet.Range("B1").Value = "Time consumed by Open method" 'Store the time information of ImportData method in cell A2. workbook.ActiveSheet.Range("A2").Value = importDataTimeConsumed.ToString() + "ms" 'Store the time information of Open method in cell B2. workbook.ActiveSheet.Range("B2").Value = openTimeConsumed.ToString() + "ms" Dim fileStream = GetResourceStream("xlsx\\AgingReport.xlsx") ' save to an excel file workbook.Save("ImportDataForRange.xlsx")