gpt4 book ai didi

c# - Open XML SDK 2.0 - 如何更新电子表格中的单元格?

转载 作者:IT王子 更新时间:2023-10-29 04:02:08 27 4
gpt4 key购买 nike

我想使用 Open XML SDK 2.0 (CTP) 更新图表使用的电子表格中的单元格。我发现的所有代码示例都插入了新单元格。我正在努力检索正确的工作表。

public static void InsertText(string docName, string text, uint rowIndex, 
string columnName)
{
// Open the document for editing.
using (SpreadsheetDocument spreadSheet =
SpreadsheetDocument.Open(docName, true))
{
Workbook workBook = spreadSheet.WorkbookPart.Workbook;

WorksheetPart worksheetPart = workBook.WorkbookPart.
WorksheetParts.First();

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

// If the worksheet does not contain a row with the specified
// row index, insert one.
Row row;

if (sheetData.Elements<Row>().Where(
r => r.RowIndex == rowIndex).Count() != 0)
// At this point I am expecting a match for a row that exists
// in sheet1 but I am not getting one

当我在 Visual Studio 中浏览树时,我看到了三张纸,但它们都没有任何 child 。我错过了什么?

最佳答案

这是工作代码。这是一个原型(prototype)。对于大量更改,可能只打开文档一次。此外,还有一些硬编码的东西,例如工作表名称和单元格类型,必须先对其进行参数化,然后才能称为生产就绪。 http://openxmldeveloper.org/forums/4005/ShowThread.aspx很有帮助。

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
using System.Xml;
using System.IO;
using System.Diagnostics;

namespace OpenXMLWindowsApp
{
public class OpenXMLWindowsApp
{
public void UpdateSheet()
{
UpdateCell("Chart.xlsx", "20", 2, "B");
UpdateCell("Chart.xlsx", "80", 3, "B");
UpdateCell("Chart.xlsx", "80", 2, "C");
UpdateCell("Chart.xlsx", "20", 3, "C");

ProcessStartInfo startInfo = new ProcessStartInfo("Chart.xlsx");
startInfo.WindowStyle = ProcessWindowStyle.Normal;
Process.Start(startInfo);
}

public static void UpdateCell(string docName, string text,
uint rowIndex, string columnName)
{
// Open the document for editing.
using (SpreadsheetDocument spreadSheet =
SpreadsheetDocument.Open(docName, true))
{
WorksheetPart worksheetPart =
GetWorksheetPartByName(spreadSheet, "Sheet1");

if (worksheetPart != null)
{
Cell cell = GetCell(worksheetPart.Worksheet,
columnName, rowIndex);

cell.CellValue = new CellValue(text);
cell.DataType =
new EnumValue<CellValues>(CellValues.Number);

// Save the worksheet.
worksheetPart.Worksheet.Save();
}
}

}

private static WorksheetPart
GetWorksheetPartByName(SpreadsheetDocument document,
string sheetName)
{
IEnumerable<Sheet> sheets =
document.WorkbookPart.Workbook.GetFirstChild<Sheets>().
Elements<Sheet>().Where(s => s.Name == sheetName);

if (sheets.Count() == 0)
{
// The specified worksheet does not exist.

return null;
}

string relationshipId = sheets.First().Id.Value;
WorksheetPart worksheetPart = (WorksheetPart)
document.WorkbookPart.GetPartById(relationshipId);
return worksheetPart;

}

// Given a worksheet, a column name, and a row index,
// gets the cell at the specified column and
private static Cell GetCell(Worksheet worksheet,
string columnName, uint rowIndex)
{
Row row = GetRow(worksheet, rowIndex);

if (row == null)
return null;

return row.Elements<Cell>().Where(c => string.Compare
(c.CellReference.Value, columnName +
rowIndex, true) == 0).First();
}


// Given a worksheet and a row index, return the row.
private static Row GetRow(Worksheet worksheet, uint rowIndex)
{
return worksheet.GetFirstChild<SheetData>().
Elements<Row>().Where(r => r.RowIndex == rowIndex).First();
}
}
}

关于c# - Open XML SDK 2.0 - 如何更新电子表格中的单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/527028/

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