open xmlを使用して行を複製して挿入しようとすると、現在問題が発生しています。openxmlを使用して特定の行の前にクローン行を追加することができません。VS2017のC#
私は、srcRowIdxとrefRowが同じインデックスである場合、挿入された行でその行をクローンし、Excelシートを正常に保存できることに気付きました。ただし、srcRowIdxとrefRowが異なる場合は、Excelシートが破損します。なぜこれがそうだと私に啓発できる人?
public Row CopyLine(SpreadsheetDocument excelDoc, string sheetName, int srcRowIdx, int refRow)
{
var worksheetPart = GetWorksheetPart(excelDoc, sheetName);
var sheetData = worksheetPart.Worksheet.Descendants<SheetData>().First();
var srcRow = GetRow(worksheetPart, srcRowIdx);
var clonedRow = (Row)srcRow.CloneNode(true);
var rowRef = GetRow(worksheetPart, refRow);
//Update all indexes to index+1
IEnumerable<Row> rows = sheetData.Descendants<Row>().Where(r => r.RowIndex.Value >= refRow);
foreach (Row row in rows)
{
var clonedRowIndex = Convert.ToUInt32(row.RowIndex.Value + 1);
foreach (Cell cell in row.Elements<Cell>())
{
// Update the references for reserved cells.
string cellReference = cell.CellReference.Value;
cell.CellReference =
new StringValue(cellReference.Replace(row.RowIndex.Value.ToString(), clonedRowIndex.ToString()));
}
// Update the row index.
row.RowIndex = new UInt32Value(clonedRowIndex);
}
sheetData.InsertBefore(clonedRow, rowRef);
worksheetPart.Worksheet.Save();
return clonedRow;
}
スプレッドシートへのリンクを投稿すると、より良い結果が得られます。 – Taterhead
これは、https://drive.google.com/file/d/0BxwJ_UygiRkKTlR4ZGM3VHdteDQ/view?usp=sharing –
を使用しているテンプレートのリンクです。検証エラー:https://drive.google.com/file/d/ 0BxwJ_UygiRkKOHBEWHcwZXkyUlk/view?usp = sharing –