OpenXML创建Excel表定义

时间:2018-08-20 16:51:04

标签: c# openxml

我是DocumentFormat.OpenXml.Spreadsheet的新手,并使用2.8.1版。我创建了Excel文档Tabs,并在行和列中添加了数据。我只需要添加Excel表定义。我认为Table Class将添加该定义,但是我无法确定在何处附加该定义,因此Table1.xml返回空并且Excel抱怨文件已损坏。下面是我创建此数据的功能代码。

           int TID = TableList.Count + 1;
            string NewTID = TID.ToString();
            //Create TableParts section in Worksheet.xml
            tableParts1 = new TableParts() { Count = (uint)1U };
            worksheetPart.Worksheet.Append(tableParts1);

            string NewTableID = "rId" + NewTID;
            TablePart NewTablePart = new TablePart() { Id = NewTableID };
            tableParts1.Append(NewTablePart);
            TablePartList.Add(NewTableID, NewTablePart);

            string TableName = T.ExcelTableName;
            string tableAddress = T.TableAddressRange();

            TableDefinitionPart tableDefinitionPart1 = worksheetPart.Worksheet.WorksheetPart.AddNewPart<TableDefinitionPart>(NewTableID);
            TableDefinitionPartList.Add(NewTID, tableDefinitionPart1);

            Table table1 = new Table() { Id = (UInt32Value)2U, Name = TableName, DisplayName = TableName, Reference = tableAddress, TotalsRowShown = false };
            TableList.Add(NewTID, table1);

            AutoFilter autoFilter1 = new AutoFilter() { Reference = tableAddress };

            UInt32Value ColumnCount =(uint)T.ColumnCount();
            TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)(ColumnCount) };
            uint cellCount = 1;
            for (uint idxColumns = 1; idxColumns < ColumnCount; idxColumns++)
            {
                //CellValue cell = GetCellValue(document, worksheet, idxColumns, rowStart);
                TableColumn tableColumn1 = new TableColumn() { Id = (UInt32Value)cellCount, Name = "Test" };
                tableColumns1.Append(tableColumn1);
                cellCount++;
            }

            TableStyleInfo tableStyleInfo1 = new TableStyleInfo()
            {
                Name = "TableStyleMedium2",
                ShowFirstColumn = false,
                ShowLastColumn = false,
                ShowRowStripes = true,
                ShowColumnStripes = false
            };

            table1.Append(autoFilter1);
            table1.Append(tableColumns1);
            table1.Append(tableStyleInfo1);
            workbookpart.Workbook.Save();
            return NewTID;

0 个答案:

没有答案