标识电子表格中的单元格 OpenXML /SpreadsheetML/
我创建了一个电子表格 .xlsx 在 C#, 使用 OpenXML SDK, 但我无法理解如何制作细胞样式。 我研究了创建的文件 Excel, 我无法理解这是如何完成的。
现在我创建填充,创建
, 哪个指向填充,创建
, 这表示索引
, 然后创造
, 这表明了
.
以下是我用来创建文档的代码:
这里是生成的 XML:
workbook.xml
styles.xml
worksheets/sheet.xml
最后一行最后一行小区 - 这是我正在尝试添加风格的地方。
当我通过生产力工具运行时,正确检查所有这些。 OpenXML SDK, 但是当我尝试打开文件时 Excel, 我收到以下错误:
恢复的记录:格式 / xl/styles.xml 部分 /风格/
然后显示电子表格,但填充不适用。
有想法如何解决它?
</workbookstylespart></worksheetpart>
现在我创建填充,创建
CellStyleFormat
, 哪个指向填充,创建
CellFormat
, 这表示索引
CellStyleFormat
, 然后创造
CellStyle
, 这表明了
CellFormat
.
以下是我用来创建文档的代码:
Console.WriteLine/"Creating document"/;
using /var spreadsheet = SpreadsheetDocument.Create/"output.xlsx", SpreadsheetDocumentType.Workbook//
{
Console.WriteLine/"Creating workbook"/;
spreadsheet.AddWorkbookPart//;
spreadsheet.WorkbookPart.Workbook = new Workbook//;
Console.WriteLine/"Creating worksheet"/;
var wsPart = spreadsheet.WorkbookPart.AddNewPart<worksheetpart>//;
wsPart.Worksheet = new Worksheet//;
var stylesPart = spreadsheet.WorkbookPart.AddNewPart<workbookstylespart>//;
stylesPart.Stylesheet = new Stylesheet//;
stylesPart.Stylesheet.Fills = new Fills//;
// create a solid red fill
var solidRed = new PatternFill// { PatternType = PatternValues.Solid };
solidRed.AppendChild/new BackgroundColor { Rgb = HexBinaryValue.FromString/"FF00FF00"/ }/;
stylesPart.Stylesheet.Fills.AppendChild/new Fill { PatternFill = new PatternFill// { PatternType = PatternValues.None } }/;
stylesPart.Stylesheet.Fills.AppendChild/new Fill { PatternFill = solidRed }/;
stylesPart.Stylesheet.CellStyleFormats = new CellStyleFormats//;
stylesPart.Stylesheet.CellStyleFormats.AppendChild/new CellFormat { FillId = 0, ApplyFill = false }/;
stylesPart.Stylesheet.CellStyleFormats.AppendChild/new CellFormat { FillId = 1, ApplyFill = true }/;
stylesPart.Stylesheet.CellFormats = new CellFormats//;
stylesPart.Stylesheet.CellFormats.AppendChild/new CellFormat { FormatId = 0 }/;
stylesPart.Stylesheet.CellFormats.AppendChild/new CellFormat { FormatId = 1 }/;
stylesPart.Stylesheet.CellStyles = new CellStyles//;
stylesPart.Stylesheet.CellStyles.AppendChild/new CellStyle { Name = "None", FormatId = 0 }/;
stylesPart.Stylesheet.CellStyles.AppendChild/new CellStyle { Name = "Solid Red", FormatId = 1 }/;
stylesPart.Stylesheet.Save//;
Console.WriteLine/"Creating sheet data"/;
var sheetData = wsPart.Worksheet.AppendChild/new SheetData///;
Console.WriteLine/"Adding rows / cells..."/;
var row = sheetData.AppendChild/new Row///;
row.AppendChild/new Cell// { CellValue = new CellValue/"This"/, DataType = CellValues.String }/;
row.AppendChild/new Cell// { CellValue = new CellValue/"is"/, DataType = CellValues.String }/;
row.AppendChild/new Cell// { CellValue = new CellValue/"a"/, DataType = CellValues.String }/;
row.AppendChild/new Cell// { CellValue = new CellValue/"test."/, DataType = CellValues.String }/;
sheetData.AppendChild/new Row///;
row = sheetData.AppendChild/new Row///;
row.AppendChild/new Cell// { CellValue = new CellValue/"Value:"/, DataType = CellValues.String }/;
row.AppendChild/new Cell// { CellValue = new CellValue/"123"/, DataType = CellValues.Number }/;
row.AppendChild/new Cell// { CellValue = new CellValue/"Formula:"/, DataType = CellValues.String }/;
row.AppendChild/new Cell// { CellFormula = new CellFormula/"B3"/, StyleIndex = 1 }/; //
Console.WriteLine/"Saving worksheet"/;
wsPart.Worksheet.Save//;
Console.WriteLine/"Creating sheet list"/;
var sheets = spreadsheet.WorkbookPart.Workbook.AppendChild/new Sheets///;
sheets.AppendChild/new Sheet// { Id = spreadsheet.WorkbookPart.GetIdOfPart/wsPart/, SheetId = 1, Name = "Test" }/;
Console.WriteLine/"Saving workbook"/;
spreadsheet.WorkbookPart.Workbook.Save//;
Console.WriteLine/"Done."/;
}
这里是生成的 XML:
workbook.xml
<?xml version="1.0" encoding="utf-8"?>
<x:workbook xmlns:x="[url=http://schemas.openxmlformats.org/spreadsheetml/2006/main">]http://schemas.openxmlformats. ... gt%3B[/url]
<x:sheets>
<x:sheet name="Test" r:id="Rbad86b8c80844a16" sheetid="1" xmlns:r="[url=http://schemas.openxmlformats.org/officeDocument/2006/relationships"></x:sheet>]http://schemas.openxmlformats. ... gt%3B[/url]
</x:sheets>
</x:workbook>
styles.xml
<?xml version="1.0" encoding="utf-8"?>
<x:stylesheet xmlns:x="[url=http://schemas.openxmlformats.org/spreadsheetml/2006/main">]http://schemas.openxmlformats. ... gt%3B[/url]
<x:fills>
<x:fill>
<x:patternfill patterntype="none"></x:patternfill>
</x:fill>
<x:fill>
<x:patternfill patterntype="solid">
<x:bgcolor rgb="FF00FF00"></x:bgcolor>
</x:patternfill>
</x:fill>
</x:fills>
<x:cellstylexfs>
<x:xf applyfill="0" fillid="0"></x:xf>
<x:xf applyfill="1" fillid="1"></x:xf>
</x:cellstylexfs>
<x:cellxfs>
<x:xf xfid="0"></x:xf>
<x:xf xfid="1"></x:xf>
</x:cellxfs>
<x:cellstyles>
<x:cellstyle name="None" xfid="0"></x:cellstyle>
<x:cellstyle name="Solid Red" xfid="1"></x:cellstyle>
</x:cellstyles>
</x:stylesheet>
worksheets/sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<x:worksheet xmlns:x="[url=http://schemas.openxmlformats.org/spreadsheetml/2006/main">]http://schemas.openxmlformats. ... gt%3B[/url]
<x:sheetdata>
<x:row>
<x:c t="str"><x:v>This</x:v></x:c>
<x:c t="str"><x:v>is</x:v></x:c>
<x:c t="str"><x:v>a</x:v></x:c>
<x:c t="str"><x:v>test.</x:v></x:c>
</x:row>
<x:row></x:row>
<x:row>
<x:c t="str"><x:v>Value:</x:v></x:c>
<x:c t="n"><x:v>123</x:v></x:c>
<x:c t="str"><x:v>Formula:</x:v></x:c>
<x:c s="1"><x:f>B3</x:f></x:c>
</x:row>
</x:sheetdata>
</x:worksheet>
最后一行最后一行小区 - 这是我正在尝试添加风格的地方。
当我通过生产力工具运行时,正确检查所有这些。 OpenXML SDK, 但是当我尝试打开文件时 Excel, 我收到以下错误:
恢复的记录:格式 / xl/styles.xml 部分 /风格/
然后显示电子表格,但填充不适用。
有想法如何解决它?
</workbookstylespart></worksheetpart>
没有找到相关结果
已邀请:
2 个回复
卫东
赞同来自:
事实证明 excel 储备风格 0 和 1 对于正常细胞和 "Gray125" pattern fill 分别。 上面的大多数代码可以删除,因为实际上我们只需要
.
工作代码:
几个提示:
使用 ClosedXML, 如果你想避免这种疯狂。
我不能推荐
https://github.com/closedxml/closedxml
如果你做出这样的工作,足够高。
格式 OpenXML API 对自己的工作非常繁琐,有各种无证案件。 ClosedXML 为你制作这么多的腿。 它们在快速纠正时也非常擅长。
</workbookstylespart></worksheetpart>
冰洋
赞同来自:
安装集合后
在风格的表中 Excel 跑得更深的支票。
不能是空的,它应该至少一个
.
只要你添加
, Excel 如果收藏品会抱怨
,
或者
空的。
第一的
默认情况下用于整本书,以及列标题/线B. Excel.
Excel 将忽略第一个
,所以只需添加空“。
如果你需要
或者
在你的格式 Excel 也忽略了第一
和
, 因此,还将空插入第一次后代
和
.
最后,从第二个开始
/
/, 你可以走了。
测试B. Excel 2010 年。