gpt4 book ai didi

c# - 打开 XML : How to add rows and cell value after appending new sheet in existing Excel file using c#

转载 作者:行者123 更新时间:2023-12-04 19:50:58 26 4
gpt4 key购买 nike

我使用打开的 XML 在现有 Excel 文件中插入/附加了新工作表,但我无法添加行和单元格值。以下是我的代码:

  using (var workbook = SpreadsheetDocument.Open(excelFilePath, true))
{
var workbookPart = workbook.WorkbookPart;
var wb = workbookPart.Workbook;
int rowIndex = 0;
// Add a blank WorksheetPart.
WorksheetPart newWorksheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
newWorksheetPart.Worksheet = new Worksheet(new SheetData());
Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<Sheets>();
string relationshipId = workbook.WorkbookPart.GetIdOfPart(newWorksheetPart);

// Get a unique ID for the new worksheet.
uint sheetId = 1;
if (sheets.Elements<Sheet>().Count() > 0)
{
sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1;
}

// Give the new worksheet a name.
string sheetName = "Data";

// Append the new worksheet and associate it with the workbook.
Sheet dataSheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
sheets.Append(sheet);
workbookPart.Workbook.Save();
var sharedStringPart = workbookPart.SharedStringTablePart;
var values = sharedStringPart.SharedStringTable.Elements<SharedStringItem>().ToArray();
Row headerRow = new Row();
Cell cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue("EmpId");
headerRow.AppendChild<Cell>(cell);
cell = new Cell();
cell.DataType = CellValues.String;
cell.CellValue = new CellValue("Name");
headerRow.AppendChild<Cell>(cell);

headerRow.AppendChild<Cell>(cell);
dataSheet.InsertAt<Row>(headerRow, rowIndex++);
workbookPart.Workbook.Save();
}
}

它抛出一个异常:

Non-composite elements do not have child elements.

最佳答案

当我收到此错误(“非复合元素没有子元素。”)是关于我的工作表对象。我得到了 Apeend 的 worng 对象,所以你需要改变使用。附加到类似的东西:

Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();
sheetData.Append(row);

一个完整的例子

            using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Create(tmp, SpreadsheetDocumentType.Workbook))
{
// Add a WorkbookPart to the document.
WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
workbookpart.Workbook = new Workbook();

// Add a WorksheetPart to the WorkbookPart.
WorksheetPart worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
worksheetPart.Worksheet = new Worksheet(new SheetData());

// Add Sheets to the Workbook.
Sheets sheets = spreadsheetDocument.WorkbookPart.Workbook.
AppendChild<Sheets>(new Sheets());

// Append a new worksheet and associate it with the workbook.
Sheet sheet = new Sheet()
{
Id = spreadsheetDocument.WorkbookPart.
GetIdOfPart(worksheetPart),
SheetId = 1,
Name = fileName,

};
sheets.Append(sheet);


var firstChild = sheet.FirstChild;

//sheet.Append(row);

Worksheet worksheet = worksheetPart.Worksheet;
SheetData sheetData = worksheet.GetFirstChild<SheetData>();

UInt32 rowIndex = 1;
var row = new Row() { RowIndex = rowIndex };

var firstNameCell = new Cell() { CellReference = "A" + rowIndex };
firstNameCell.CellValue = new CellValue("FirstName");
firstNameCell.DataType = CellValues.String;

row.Append(firstNameCell);

Cell lastNameCell = new Cell() { CellReference = "B" + rowIndex };
lastNameCell.CellValue = new CellValue("LastName");
lastNameCell.DataType = new EnumValue<CellValues>(CellValues.String);

row.Append(lastNameCell);

sheetData.Append(row);

workbookpart.Workbook.Save();

// Close the document.
spreadsheetDocument.Close();

}

关于c# - 打开 XML : How to add rows and cell value after appending new sheet in existing Excel file using c#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41265668/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com