Open XML SDK 2.0을 사용하여 Excel 문서에 행으로 문자열 배열 삽입

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

  •  13-09-2019
  •  | 
  •  

문제

코드는 실행되지만 내 Excel 문서를 손상시킵니다. 어떤 도움이든 감사 할 것입니다! 나는 사용했다 이것 참조로.

    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;
    }
도움이 되었습니까?

해결책

아, 찾았 어. insertsharedstring 방법에서

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

해야한다

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

다른 팁

나는 이것이 a라는 것을 알고있다 매우 오래된 질문이지만 원래 코드에서 또 다른 문제를 발견/수정했습니다.

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

해야한다:

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

이런 식으로 첫 번째 셀 레퍼런스는 A0 대신 A1이므로 개방시 Excel이 충돌합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top