openXML(C#) 是否支持生成格式化的数字字段(如 1,000,000)
我有使用 OpenXML 从 Excel 生成电子表格的现有代码。它工作正常,但我总是必须随后进入电子表格并添加格式。
无论如何,在我的 C# 代码中,我可以指定我想要一个使用特定数字格式(逗号、小数等)格式化的列吗?
任何示例的链接都会很棒。
这是我现在保存数据的方式:
public void SetCell<T>(string column, uint rowIndex, T value)
{
SheetData sheetData = _positionSheet;
Cell cell = GetCell(sheetData, column, rowIndex);
string stringValue = value == null ? "" : value.ToString();
cell.CellValue = new CellValue(stringValue);
switch (typeof(T).Name)
{
case "Date":
case "DateTime":
cell.DataType = CellValues.Date;
break;
case "Int32":
cell.DataType = CellValues.Number;
break;
default:
cell.DataType = CellValues.String;
break;
}
}
下面是 GetCell() 方法(尽管我猜这与问题并不真正相关:
static private Cell GetCell(SheetData sheet, string column, uint rowIndex)
{
Cell cell;
Row row = GetRow(sheet, rowIndex);
if (row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).Any())
{
cell = row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).First();
}
else
{
Cell refCell = null;
var comparer = new CellComparer();
foreach (Cell existingCell in row.Elements<Cell>())
{
if (comparer.Compare(existingCell.CellReference.Value, column + rowIndex) > 0)
{
refCell = existingCell;
break;
}
}
cell = new Cell { CellReference = column + rowIndex };
row.InsertBefore(cell, refCell);
}
return cell;
}
i have existing code that generates a spreadsheet from excel using OpenXML. It works fine but i always have to go into the spreadsheet afterwards and add formatting.
Is there anyway in my C# code I can specify that i want a column formatted with a certain number formatting) commas, decimals, etc?
any links to examples would be great.
here is how i am saving data now:
public void SetCell<T>(string column, uint rowIndex, T value)
{
SheetData sheetData = _positionSheet;
Cell cell = GetCell(sheetData, column, rowIndex);
string stringValue = value == null ? "" : value.ToString();
cell.CellValue = new CellValue(stringValue);
switch (typeof(T).Name)
{
case "Date":
case "DateTime":
cell.DataType = CellValues.Date;
break;
case "Int32":
cell.DataType = CellValues.Number;
break;
default:
cell.DataType = CellValues.String;
break;
}
}
below is the GetCell() method (even though i guess thats not really relevant for the question:
static private Cell GetCell(SheetData sheet, string column, uint rowIndex)
{
Cell cell;
Row row = GetRow(sheet, rowIndex);
if (row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).Any())
{
cell = row.Elements<Cell>().Where(c => c.CellReference.Value == column + rowIndex).First();
}
else
{
Cell refCell = null;
var comparer = new CellComparer();
foreach (Cell existingCell in row.Elements<Cell>())
{
if (comparer.Compare(existingCell.CellReference.Value, column + rowIndex) > 0)
{
refCell = existingCell;
break;
}
}
cell = new Cell { CellReference = column + rowIndex };
row.InsertBefore(cell, refCell);
}
return cell;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是可能的,但是它都深深地埋藏在风格中。考虑使用 ClosedXML 它的对象模型类似于旧的优秀 Excel 对象模型,并在底层使用 OpenXML。有了它,一切都变得简单多了。
Yes it's possible, however it's all buried deep into styles. Consider using ClosedXML it have object model similar to old good Excel Object Model and uses OpenXML under the hood. Everything is much simplier with it.
您可以使用 ExtremeML,它基于 OpenXML。
如果使用它,您可以定义一个模板,并通过模板生成excel文件。
book1.xlsx 是模板,book2.xlsx 是最终文件。 tabb1 是 excel 2007 中的一个表。
You can use ExtremeML, it's based on OpenXML.
If use it, you can define a template, and generate excel file by template.
book1.xlsx is template, book2.xlsx is finally file. tabb1 is a table in excel 2007.