Cell by Cell写得很慢,但是我正在面临写作范围的情况

我有一个非常大的数组,它的大小取决于外部设备,它可以是65536 ,也可以是131072的两倍,其数据types是ushort

我必须将这整个数据写入65536行* 1列.xlsx文件。 我以前一个一个的写单元格,但是需要4分钟才能完成。

所以我想到了使用范围的方法( 这里给出),我的部分代码如下:

{ \\ VoltageFluctuations is of ushort type with 65536 values xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Add(misValue); // Voltage Fluctuations xlWorkSheet1 = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); xlWorkSheet1.Name = "Voltage Fluctuations"; xlWorkSheet1.Cells[1, 1] = ("Minutes"); xlWorkSheet1.Cells[1, 2] = ("Voltage"); var startCell = (Excel.Range)xlWorkSheet1.Cells[2, 2]; var endCell = new object(); endCell = (Excel.Range)xlWorkSheet1.Cells[65536, 2]; var writeRange = xlWorkSheet1.get_Range(startCell, endCell); writeRange.set_Value(Type.Missing, VoltageFluctuations); // As soon as the above line is executed my program crashes // Finally saving this as a .xlsx file type } 

我得到的例外是:

在这里输入图像说明

使用OpenXML SDK来试试这个问题:

对于UI:

  private static Worksheet GetWorksheet(SpreadsheetDocument document, string worksheetName = "Sheet1") { var sheets = document.WorkbookPart.Workbook .Descendants<Sheet>().Where(s => s.Name == worksheetName); var worksheetPart = (WorksheetPart)document.WorkbookPart .GetPartById(sheets.First().Id); return worksheetPart.Worksheet; } private static string GetColumnName(int columnIndex) { var dividend = columnIndex; var columnName = String.Empty; while (dividend > 0) { var modifier = (dividend - 1) % 26; columnName = Convert.ToChar(65 + modifier).AsString() + columnName; dividend = (dividend - modifier) / 26; } return columnName; } private static Cell CreateTextCell(int columnIndex, int rowIndex, object cellValue) { var cell = new Cell(); var inlineString = new InlineString(); var txt = new Text(); cell.DataType = CellValues.InlineString; cell.CellReference = GetColumnName(columnIndex) + rowIndex; txt.Text = cellValue.AsString(); inlineString.AppendChild(txt); cell.AppendChild(inlineString); return cell; } private static Row CreateContentRow(int rowIndex, object[] cellValues) { var row = new Row { RowIndex = (UInt32)rowIndex }; for (var i = 0; i < cellValues.Length; i++) { var dataCell = CreateTextCell(i + 1, rowIndex, cellValues[i]); row.AppendChild(dataCell); } return row; } 

电子表格设置数据:

  var sourcePath = "Report_Template.xlsx"; var data = File.ReadAllBytes(sourcePath); var memoryStream = new MemoryStream(); memoryStream.Write(data, 0, data.Length); var processSettings = new MarkupCompatibilityProcessSettings ( MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2007 ); var openSettings = new OpenSettings() { MarkupCompatibilityProcessSettings = processSettings, AutoSave = true }; using (var spreadSheet = SpreadsheetDocument.Open(memoryStream, true, openSettings)) { var worksheet = GetWorksheet(spreadSheet); var worksheetPart = worksheet.WorksheetPart; const int lastRowIndex = 1; var sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>(); var lastRow = sheetData.Elements<Row>().LastOrDefault(l => l.RowIndex == lastRowIndex); var reportItems=null;//Get your reoirting data... if (lastRow != null) { foreach (var newRow in (from reportItem in reportItems let newRowIndex = lastRow.RowIndex + 1 select CreateContentRow((int)newRowIndex, new object[] { newRowIndex-lastRow.RowIndex, reportItem.Column1, reportItem.Column2 { sheetData.InsertAfter(newRow, lastRow); } worksheet.Save(); } } 

之前和之后的方法,引用行和单元格插入和附加数据…

您需要这样声明VoltageFluctuations:

 object[,] VoltageFluctuations = new object[65536, 1]; 

(考虑使用类似const int size = 65536东西。)

诚然,这不是很直观,至less不适合我。 但是,ushorttypes的数组完全不工作,只有一个维度的数组没有给出预期的结果,而是将第一个值写入所有单元格。