Advertisement
FoxTuGa

OpenXML Excel Generated Class

Aug 25th, 2014
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 14.35 KB | None | 0 0
  1. using DocumentFormat.OpenXml.Packaging;
  2. using Ap = DocumentFormat.OpenXml.ExtendedProperties;
  3. using Vt = DocumentFormat.OpenXml.VariantTypes;
  4. using DocumentFormat.OpenXml;
  5. using DocumentFormat.OpenXml.Spreadsheet;
  6. using X15ac = DocumentFormat.OpenXml.Office2013.ExcelAc;
  7. using X15 = DocumentFormat.OpenXml.Office2013.Excel;
  8. using X14 = DocumentFormat.OpenXml.Office2010.Excel;
  9. using A = DocumentFormat.OpenXml.Drawing;
  10. using Thm15 = DocumentFormat.OpenXml.Office2013.Theme;
  11.  
  12. namespace GeneratedCode
  13. {
  14.     public class GeneratedClass
  15.     {
  16.         // Creates a SpreadsheetDocument.
  17.         public void CreatePackage(string filePath)
  18.         {
  19.             using (SpreadsheetDocument document = SpreadsheetDocument.Create(filePath, SpreadsheetDocumentType.Workbook))
  20.             {
  21.                 WorkbookPart workbookPart1 = document.AddWorkbookPart();
  22.                 GenerateWorkbookPart1Content(workbookPart1);
  23.  
  24.                 WorksheetPart worksheetPart1 = workbookPart1.AddNewPart<WorksheetPart>("rId1");
  25.                 GenerateWorksheetPart1Content(worksheetPart1);
  26.  
  27.                 TableDefinitionPart tableDefinitionPart1 = worksheetPart1.AddNewPart<TableDefinitionPart>("rId1");
  28.                 GenerateTableDefinitionPart1Content(tableDefinitionPart1, worksheetPart1.Worksheet);
  29.  
  30.                 SharedStringTablePart sharedStringTablePart1 = workbookPart1.AddNewPart<SharedStringTablePart>("rId4");
  31.                 GenerateSharedStringTablePart1Content(sharedStringTablePart1);
  32.             }
  33.         }
  34.  
  35.         // Generates content of workbookPart1.
  36.         private void GenerateWorkbookPart1Content(WorkbookPart workbookPart1)
  37.         {
  38.             Workbook workbook1 = new Workbook() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x15" } };
  39.             workbook1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  40.             workbook1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
  41.             workbook1.AddNamespaceDeclaration("x15", "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main");
  42.             FileVersion fileVersion1 = new FileVersion() { ApplicationName = "xl", LastEdited = "6", LowestEdited = "6", BuildVersion = "14420" };
  43.             WorkbookProperties workbookProperties1 = new WorkbookProperties() { DefaultThemeVersion = (UInt32Value)153222U };
  44.  
  45.             AlternateContent alternateContent1 = new AlternateContent();
  46.             alternateContent1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
  47.  
  48.             AlternateContentChoice alternateContentChoice1 = new AlternateContentChoice() { Requires = "x15" };
  49.  
  50.             X15ac.AbsolutePath absolutePath1 = new X15ac.AbsolutePath() { Url = "C:\\Users\\leand_000\\Desktop\\" };
  51.             absolutePath1.AddNamespaceDeclaration("x15ac", "http://schemas.microsoft.com/office/spreadsheetml/2010/11/ac");
  52.  
  53.             alternateContentChoice1.Append(absolutePath1);
  54.  
  55.             alternateContent1.Append(alternateContentChoice1);
  56.  
  57.             BookViews bookViews1 = new BookViews();
  58.             WorkbookView workbookView1 = new WorkbookView() { XWindow = 0, YWindow = 0, WindowWidth = (UInt32Value)20490U, WindowHeight = (UInt32Value)8340U };
  59.  
  60.             bookViews1.Append(workbookView1);
  61.  
  62.             Sheets sheets1 = new Sheets();
  63.             Sheet sheet1 = new Sheet() { Name = "Sheet1", SheetId = (UInt32Value)1U, Id = "rId1" };
  64.  
  65.             sheets1.Append(sheet1);
  66.             CalculationProperties calculationProperties1 = new CalculationProperties() { CalculationId = (UInt32Value)152511U };
  67.  
  68.             WorkbookExtensionList workbookExtensionList1 = new WorkbookExtensionList();
  69.  
  70.             WorkbookExtension workbookExtension1 = new WorkbookExtension() { Uri = "{140A7094-0E35-4892-8432-C4D2E57EDEB5}" };
  71.             workbookExtension1.AddNamespaceDeclaration("x15", "http://schemas.microsoft.com/office/spreadsheetml/2010/11/main");
  72.             X15.WorkbookProperties workbookProperties2 = new X15.WorkbookProperties() { ChartTrackingReferenceBase = true };
  73.  
  74.             workbookExtension1.Append(workbookProperties2);
  75.  
  76.             workbookExtensionList1.Append(workbookExtension1);
  77.  
  78.             workbook1.Append(fileVersion1);
  79.             workbook1.Append(workbookProperties1);
  80.             workbook1.Append(alternateContent1);
  81.             workbook1.Append(bookViews1);
  82.             workbook1.Append(sheets1);
  83.             workbook1.Append(calculationProperties1);
  84.             workbook1.Append(workbookExtensionList1);
  85.  
  86.             workbookPart1.Workbook = workbook1;
  87.         }
  88.  
  89.         // Generates content of worksheetPart1.
  90.         private void GenerateWorksheetPart1Content(WorksheetPart worksheetPart1)
  91.         {
  92.             Worksheet worksheet1 = new Worksheet() { MCAttributes = new MarkupCompatibilityAttributes() { Ignorable = "x14ac" } };
  93.             worksheet1.AddNamespaceDeclaration("r", "http://schemas.openxmlformats.org/officeDocument/2006/relationships");
  94.             worksheet1.AddNamespaceDeclaration("mc", "http://schemas.openxmlformats.org/markup-compatibility/2006");
  95.             worksheet1.AddNamespaceDeclaration("x14ac", "http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac");
  96.             SheetDimension sheetDimension1 = new SheetDimension() { Reference = "A2:C5" };
  97.  
  98.             SheetViews sheetViews1 = new SheetViews();
  99.  
  100.             SheetView sheetView1 = new SheetView() { TabSelected = true, WorkbookViewId = (UInt32Value)0U };
  101.             Selection selection1 = new Selection() { ActiveCell = "F6", SequenceOfReferences = new ListValue<StringValue>() { InnerText = "F6" } };
  102.  
  103.             sheetView1.Append(selection1);
  104.  
  105.             sheetViews1.Append(sheetView1);
  106.             SheetFormatProperties sheetFormatProperties1 = new SheetFormatProperties() { DefaultRowHeight = 15D, DyDescent = 0.25D };
  107.  
  108.             SheetData sheetData1 = new SheetData();
  109.  
  110.             Row row1 = new Row() { RowIndex = (UInt32Value)2U, Spans = new ListValue<StringValue>() { InnerText = "1:3" }, DyDescent = 0.25D };
  111.  
  112.             Cell cell1 = new Cell() { CellReference = "A2", DataType = CellValues.SharedString };
  113.             CellValue cellValue1 = new CellValue();
  114.             cellValue1.Text = "0";
  115.  
  116.             cell1.Append(cellValue1);
  117.  
  118.             Cell cell2 = new Cell() { CellReference = "B2", DataType = CellValues.SharedString };
  119.             CellValue cellValue2 = new CellValue();
  120.             cellValue2.Text = "1";
  121.  
  122.             cell2.Append(cellValue2);
  123.  
  124.             Cell cell3 = new Cell() { CellReference = "C2", DataType = CellValues.SharedString };
  125.             CellValue cellValue3 = new CellValue();
  126.             cellValue3.Text = "2";
  127.  
  128.             cell3.Append(cellValue3);
  129.  
  130.             row1.Append(cell1);
  131.             row1.Append(cell2);
  132.             row1.Append(cell3);
  133.  
  134.             Row row2 = new Row() { RowIndex = (UInt32Value)3U, Spans = new ListValue<StringValue>() { InnerText = "1:3" }, DyDescent = 0.25D };
  135.  
  136.             Cell cell4 = new Cell() { CellReference = "A3" };
  137.             CellValue cellValue4 = new CellValue();
  138.             cellValue4.Text = "22";
  139.  
  140.             cell4.Append(cellValue4);
  141.  
  142.             Cell cell5 = new Cell() { CellReference = "B3", DataType = CellValues.SharedString };
  143.             CellValue cellValue5 = new CellValue();
  144.             cellValue5.Text = "3";
  145.  
  146.             cell5.Append(cellValue5);
  147.  
  148.             Cell cell6 = new Cell() { CellReference = "C3", DataType = CellValues.SharedString };
  149.             CellValue cellValue6 = new CellValue();
  150.             cellValue6.Text = "6";
  151.  
  152.             cell6.Append(cellValue6);
  153.  
  154.             row2.Append(cell4);
  155.             row2.Append(cell5);
  156.             row2.Append(cell6);
  157.  
  158.             Row row3 = new Row() { RowIndex = (UInt32Value)4U, Spans = new ListValue<StringValue>() { InnerText = "1:3" }, DyDescent = 0.25D };
  159.  
  160.             Cell cell7 = new Cell() { CellReference = "A4" };
  161.             CellValue cellValue7 = new CellValue();
  162.             cellValue7.Text = "3";
  163.  
  164.             cell7.Append(cellValue7);
  165.  
  166.             Cell cell8 = new Cell() { CellReference = "B4", DataType = CellValues.SharedString };
  167.             CellValue cellValue8 = new CellValue();
  168.             cellValue8.Text = "4";
  169.  
  170.             cell8.Append(cellValue8);
  171.  
  172.             Cell cell9 = new Cell() { CellReference = "C4", DataType = CellValues.SharedString };
  173.             CellValue cellValue9 = new CellValue();
  174.             cellValue9.Text = "7";
  175.  
  176.             cell9.Append(cellValue9);
  177.  
  178.             row3.Append(cell7);
  179.             row3.Append(cell8);
  180.             row3.Append(cell9);
  181.  
  182.             Row row4 = new Row() { RowIndex = (UInt32Value)5U, Spans = new ListValue<StringValue>() { InnerText = "1:3" }, DyDescent = 0.25D };
  183.  
  184.             Cell cell10 = new Cell() { CellReference = "A5" };
  185.             CellValue cellValue10 = new CellValue();
  186.             cellValue10.Text = "45";
  187.  
  188.             cell10.Append(cellValue10);
  189.  
  190.             Cell cell11 = new Cell() { CellReference = "B5", DataType = CellValues.SharedString };
  191.             CellValue cellValue11 = new CellValue();
  192.             cellValue11.Text = "5";
  193.  
  194.             cell11.Append(cellValue11);
  195.  
  196.             Cell cell12 = new Cell() { CellReference = "C5", DataType = CellValues.SharedString };
  197.             CellValue cellValue12 = new CellValue();
  198.             cellValue12.Text = "8";
  199.  
  200.             cell12.Append(cellValue12);
  201.  
  202.             row4.Append(cell10);
  203.             row4.Append(cell11);
  204.             row4.Append(cell12);
  205.  
  206.             sheetData1.Append(row1);
  207.             sheetData1.Append(row2);
  208.             sheetData1.Append(row3);
  209.             sheetData1.Append(row4);
  210.             PageMargins pageMargins1 = new PageMargins() { Left = 0.7D, Right = 0.7D, Top = 0.75D, Bottom = 0.75D, Header = 0.3D, Footer = 0.3D };
  211.  
  212.            
  213.  
  214.             worksheet1.Append(sheetDimension1);
  215.             worksheet1.Append(sheetViews1);
  216.             worksheet1.Append(sheetFormatProperties1);
  217.             worksheet1.Append(sheetData1);
  218.             worksheet1.Append(pageMargins1);
  219.             //worksheet1.Append(tableParts1);
  220.  
  221.             worksheetPart1.Worksheet = worksheet1;
  222.         }
  223.  
  224.         // Generates content of tableDefinitionPart1.
  225.         private void GenerateTableDefinitionPart1Content(TableDefinitionPart tableDefinitionPart1, Worksheet worksheet1)
  226.         {
  227.             TableParts tableParts1 = new TableParts() { Count = (UInt32Value)1U };
  228.             TablePart tablePart1 = new TablePart() { Id = "rId1" };
  229.  
  230.             tableParts1.Append(tablePart1);
  231.             worksheet1.Append(tableParts1);
  232.  
  233.             Table table1 = new Table() { Id = (UInt32Value)2U, Name = "Table2", DisplayName = "Table2", Reference = "A2:C5", TotalsRowShown = false };
  234.             AutoFilter autoFilter1 = new AutoFilter() { Reference = "A2:C5" };
  235.  
  236.             TableColumns tableColumns1 = new TableColumns() { Count = (UInt32Value)3U };
  237.             TableColumn tableColumn1 = new TableColumn() { Id = (UInt32Value)1U, Name = "a" };
  238.             TableColumn tableColumn2 = new TableColumn() { Id = (UInt32Value)2U, Name = "b" };
  239.             TableColumn tableColumn3 = new TableColumn() { Id = (UInt32Value)3U, Name = "c" };
  240.  
  241.             tableColumns1.Append(tableColumn1);
  242.             tableColumns1.Append(tableColumn2);
  243.             tableColumns1.Append(tableColumn3);
  244.             TableStyleInfo tableStyleInfo1 = new TableStyleInfo() { Name = "TableStyleMedium2", ShowFirstColumn = false, ShowLastColumn = false, ShowRowStripes = true, ShowColumnStripes = false };
  245.  
  246.             table1.Append(autoFilter1);
  247.             table1.Append(tableColumns1);
  248.             table1.Append(tableStyleInfo1);
  249.  
  250.             tableDefinitionPart1.Table = table1;
  251.         }
  252.  
  253.         // Generates content of sharedStringTablePart1.
  254.         private void GenerateSharedStringTablePart1Content(SharedStringTablePart sharedStringTablePart1)
  255.         {
  256.             SharedStringTable sharedStringTable1 = new SharedStringTable() { Count = (UInt32Value)9U, UniqueCount = (UInt32Value)9U };
  257.  
  258.             SharedStringItem sharedStringItem1 = new SharedStringItem();
  259.             Text text1 = new Text();
  260.             text1.Text = "a";
  261.  
  262.             sharedStringItem1.Append(text1);
  263.  
  264.             SharedStringItem sharedStringItem2 = new SharedStringItem();
  265.             Text text2 = new Text();
  266.             text2.Text = "b";
  267.  
  268.             sharedStringItem2.Append(text2);
  269.  
  270.             SharedStringItem sharedStringItem3 = new SharedStringItem();
  271.             Text text3 = new Text();
  272.             text3.Text = "c";
  273.  
  274.             sharedStringItem3.Append(text3);
  275.  
  276.             SharedStringItem sharedStringItem4 = new SharedStringItem();
  277.             Text text4 = new Text();
  278.             text4.Text = "Mar";
  279.  
  280.             sharedStringItem4.Append(text4);
  281.  
  282.             SharedStringItem sharedStringItem5 = new SharedStringItem();
  283.             Text text5 = new Text();
  284.             text5.Text = "Terra";
  285.  
  286.             sharedStringItem5.Append(text5);
  287.  
  288.             SharedStringItem sharedStringItem6 = new SharedStringItem();
  289.             Text text6 = new Text();
  290.             text6.Text = "Agua";
  291.  
  292.             sharedStringItem6.Append(text6);
  293.  
  294.             SharedStringItem sharedStringItem7 = new SharedStringItem();
  295.             Text text7 = new Text();
  296.             text7.Text = "les";
  297.  
  298.             sharedStringItem7.Append(text7);
  299.  
  300.             SharedStringItem sharedStringItem8 = new SharedStringItem();
  301.             Text text8 = new Text();
  302.             text8.Text = "tic";
  303.  
  304.             sharedStringItem8.Append(text8);
  305.  
  306.             SharedStringItem sharedStringItem9 = new SharedStringItem();
  307.             Text text9 = new Text();
  308.             text9.Text = "rjc";
  309.  
  310.             sharedStringItem9.Append(text9);
  311.  
  312.             sharedStringTable1.Append(sharedStringItem1);
  313.             sharedStringTable1.Append(sharedStringItem2);
  314.             sharedStringTable1.Append(sharedStringItem3);
  315.             sharedStringTable1.Append(sharedStringItem4);
  316.             sharedStringTable1.Append(sharedStringItem5);
  317.             sharedStringTable1.Append(sharedStringItem6);
  318.             sharedStringTable1.Append(sharedStringItem7);
  319.             sharedStringTable1.Append(sharedStringItem8);
  320.             sharedStringTable1.Append(sharedStringItem9);
  321.  
  322.             sharedStringTablePart1.SharedStringTable = sharedStringTable1;
  323.         }
  324.     }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement