目标:生成一个电子表格,其中工作表命名为每个工作表第一行列出的表和列名。这些实际上将基于从这些表构建的类。
问题:打开电子表格后,它声称发现“不可读的内容”并询问我是否要恢复。如果我选择是,它确实可以完美地恢复电子表格,但用户不必这样做。
观察:使用 SDK,我能够看到电子表格的内部工作原理,并且我注意到工作表的 xml 名称是真正的问题。在最初下载的版本中,它们被命名为 Sheet1 到 Sheet9,但随后它们进入到 Sheet1a 到 Sheet1f。所以基本上它用十六进制1-f对它们进行编号。在我对其进行修复后,他们将名称更正为纯数字。
不确定您是否可以看到图片,看起来我的工作阻止了该网站:/
注意:我查看了我的代码,但没有看到任何可以具体命名这些内部 xml 工作表的地方,因此我不确定要与您分享哪些代码,但我发布了创建这些工作表的代码部分。
构建电子表格的方法:
public MemoryStream CreatePartsFromAssembly(MemoryStream spreadsheetStrem, Type typeInAssembly)
{
using (SpreadsheetDocument document = SpreadsheetDocument.Create(spreadsheetStrem, SpreadsheetDocumentType.Workbook))
{
//List<Type> types = null;
var types = ExcelTypeHelper.GetTableTypes(typeInAssembly);
ExtendedFilePropertiesPart extendedFilePropertiesPart1 = document.AddNewPart<ExtendedFilePropertiesPart>("rId" + (types.Count).ToString());
GenerateExtendedFilePropertiesPart1Content(extendedFilePropertiesPart1, types);
WorkbookPart workbookPart1 = document.AddWorkbookPart();
GenerateWorkbookPart1Content(workbookPart1, types);
var sharedStringOffsets = new List<int>();
foreach (var type in types)
{
sharedStringOffsets.Add(ExcelTypeHelper.GetPropertyCount(type));
}
//int j = 0;
int sharedStringOffset = 0;
int sharedStringSum = sharedStringOffsets.Sum();
int sharedStringRunningSum = 0;
for (int i = types.Count; i > 0; i--)
{
var type = types[i - 1];
sharedStringRunningSum += sharedStringOffsets[i - 1];
sharedStringOffset = sharedStringSum - sharedStringRunningSum;
WorksheetPart worksheetPart = workbookPart1.AddNewPart<WorksheetPart>("rId" + (i).ToString());
GenerateWorksheetPartContent(worksheetPart, types[i - 1], sharedStringOffset);
//j++;
}
SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId" + (types.Count + 3).ToString());
GenerateSharedStringTablePartContent(sharedStringTablePart1, types);
WorkbookStylesPart workbookStylesPart1 = workbookPart1.AddNewPart<WorkbookStylesPart>("rId" + (types.Count + 2).ToString());
GenerateWorkbookStylesPart1Content(workbookStylesPart1);
ThemePart themePart1 = workbookPart1.AddNewPart<ThemePart>("rId" + (types.Count + 1).ToString());
GenerateThemePart1Content(themePart1);
SetPackageProperties1(document);
}
return spreadsheetStrem;
}
构建工作表的方法:
private void GenerateWorksheetPartContent(WorksheetPart worksheetPart, Type type, int sharedStringOffset)
{
var propertyNames = ExcelTypeHelper.GetPropertyNames(type);
string referenceRange = "A1:" + ColumnHeaderFromRowColumn((uint)1, (uint)propertyNames.Count);
Worksheet worksheet = new Worksheet();
worksheet.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
SheetDimension sheetDimension = new SheetDimension() { Reference = referenceRange };
SheetViews sheetViews = new SheetViews();
SheetView sheetView = new SheetView() { WorkbookViewId = (UInt32Value)0U };
Selection selection = new Selection() { ActiveCell = "A2", SequenceOfReferences = new ListValue<StringValue>() { InnerText = "A2" } };
sheetView.Append(selection);
sheetViews.Append(sheetView);
SheetFormatProperties sheetFormatProperties2 = new SheetFormatProperties() { DefaultRowHeight = 15D };
SheetData sheetData = new SheetData();
Row row = new Row() { RowIndex = (UInt32Value)1U, Spans = new ListValue<StringValue>() { InnerText = "1:" + propertyNames.Count } };
for (int i = 0; i < propertyNames.Count; i++)
{
Cell cell = new Cell() { CellReference = ColumnHeaderFromRowColumn((uint)1, (uint)i + 1), DataType = CellValues.SharedString };
CellValue cellValue = new CellValue();
cellValue.Text = (i + sharedStringOffset).ToString(); //Shared string reference
cell.Append(cellValue);
row.Append(cell);
}
sheetData.Append(row);
PageMargins pageMargins = new PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.75D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D };
worksheet.Append(sheetDimension);
worksheet.Append(sheetViews);
worksheet.Append(sheetFormatProperties2);
worksheet.Append(sheetData);
worksheet.Append(pageMargins);
worksheetPart.Worksheet = worksheet;
}