Cell by Cell写作非常慢,但我正面临范围写作的情况

时间:2013-04-27 08:44:43

标签: c# excel file-io

我有一个非常大的阵列,其大小取决于外部设备,它可能 65536 或它可能是 131072 的两倍数据类型为 ushort

我必须将整个数据写入65536行* 1列 .xlsx 文件。我曾经逐个细胞地写,但是需要4分钟才能完成。

所以我考虑使用范围方法(如给定here),我的部分代码如下:

{
    \\ 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
}

我得到的例外是:

enter image description here

2 个答案:

答案 0 :(得分:0)

使用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();
            }

        }

之后&amp;在方法之前,引用的行和单元格用于插入和附加数据...

答案 1 :(得分:0)

您需要以这种方式声明VoltageFluctuations:

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

(考虑使用类似const int size = 65536的内容。)

不可否认,这不是很直观,至少不适合我。但是,ushort类型的数组根本不起作用,只有一个维度的数组没有给出预期的结果,而是将第一个值写入所有单元格。