
Note, as the comment mentions, if I use a simple function like NOW() it does not insert the @.
How do I get it to stop inserting that @?
// First run Install-Package DocumentFormat.OpenXml
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;
string filePath = "temp.xlsx";
// 1. Create Excel file
using (SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
{
WorkbookPart workbookPart = document.AddWorkbookPart();
workbookPart.Workbook = new Workbook();
WorksheetPart worksheetPart = workbookPart.AddNewPart();
SheetData sheetData = new SheetData();
// A1: Insert formula
Row headerRow = new Row();
var formula = "STOCKHISTORY(\"AMZN\", \"6/1/2006\", \"6/22/2025\", 0, 1, 0, 1, 2, 3,4, 5)";
// If I use this next line the NOW() does not gat an @ added in front
//formula = "NOW()";
Cell formulaCell = new Cell
{
CellReference = "A1",
CellFormula = new CellFormula(formula),
DataType = CellValues.Number
};
headerRow.Append(formulaCell);
sheetData.Append(headerRow);
worksheetPart.Worksheet = new Worksheet(sheetData);
// Add Sheets to Workbook
Sheets sheets = document.WorkbookPart.Workbook.AppendChild(new Sheets());
Sheet sheet = new Sheet()
{
Id = document.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1,
Name = "Sheet1"
};
sheets.Append(sheet);
workbookPart.Workbook.Save();
}
Подробнее здесь: https://stackoverflow.com/questions/796 ... want-it-to
Мобильная версия