Insertion d'un tableau de chaînes en ligne dans un document Excel à l'aide du SDK Open XML 2.0

StackOverflow https://stackoverflow.com/questions/997297

  •  13-09-2019
  •  | 
  •  

Question

Le code est exécuté, mais corrompt mon document Excel. Toute aide serait appréciée mucho! Je cette comme référence.

    public void AddRow(string fileName, string[] values) {
        using (SpreadsheetDocument doc =
            SpreadsheetDocument.Open(fileName, true)) {
            SharedStringTablePart sharedStringPart =
                GetSharedStringPart(doc);

            WorksheetPart worksheetPart =
                doc.WorkbookPart.WorksheetParts.First();
            uint rowIdx = AppendRow(worksheetPart);

            for (int i = 0; i < values.Length; ++i) {
                int stringIdx = InsertSharedString(values[i],
                    sharedStringPart);

                Cell cell = InsertCell(i, rowIdx, worksheetPart);
                cell.CellValue = new CellValue(stringIdx.ToString());
                cell.DataType = new EnumValue<CellValues>(
                    CellValues.SharedString);

                worksheetPart.Worksheet.Save();
            }
        }
    }

    private SharedStringTablePart GetSharedStringPart(
        SpreadsheetDocument doc) {
        if (doc.WorkbookPart.
            GetPartsCountOfType<SharedStringTablePart>() > 0)
            return doc.WorkbookPart.
                GetPartsOfType<SharedStringTablePart>().First();
        else
            return doc.WorkbookPart.
                AddNewPart<SharedStringTablePart>();
    }

    private uint AppendRow(WorksheetPart worksheetPart) {
        SheetData sheetData = worksheetPart.Worksheet.
            GetFirstChild<SheetData>();

        uint rowIndex = (uint)sheetData.Elements<Row>().Count();

        Row row = new Row() { RowIndex = rowIndex };
        sheetData.Append(row);

        return rowIndex;
    }

    private int InsertSharedString(string s,
        SharedStringTablePart sharedStringPart) {
        if (sharedStringPart.SharedStringTable == null)
            sharedStringPart.SharedStringTable =
                new SharedStringTable();

        int i = 0;

        foreach (SharedStringItem item in
            sharedStringPart.SharedStringTable.
            Elements<SharedStringItem>()) {
            if (item.InnerText == s)
                return i;
            ++i;
        }

        sharedStringPart.SharedStringTable.AppendChild(
            new Text(s));
        sharedStringPart.SharedStringTable.Save();

        return i;
    }

    private Cell InsertCell(int i, uint rowIdx,
        WorksheetPart worksheetPart) {
        SheetData sheetData = worksheetPart.Worksheet.
            GetFirstChild<SheetData>();
        string cellReference = AlphabetMap.Instance[i] + rowIdx;

        Cell cell = new Cell() { CellReference = cellReference };
        Row row = sheetData.Elements<Row>().ElementAt((int)rowIdx);

        row.InsertAt(cell, i);
        worksheetPart.Worksheet.Save();
        return cell;
    }
Était-ce utile?

La solution

Ah, trouvé. Dans la méthode InsertSharedString,

sharedStringPart.SharedStringTable.AppendChild(
        new Text(s));

doit être

sharedStringPart.SharedStringTable.AppendChild(
        new SharedStringItem(new Text(s)));

Autres conseils

Je sais que c'est un très vieille question, mais j'ai trouvé / fixe un autre problème avec le code d'origine:

string cellReference = AlphabetMap.Instance[i] + rowIdx;

devrait être:

string cellReference = AlphabetMap.Instance[i] + (rowIdx + 1);

De cette façon, la première est CellReference A1 au lieu de A0, ce qui provoque Excel crash sur l'ouverture

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top