I am working with the following code snippet details of which can be found at
here.
I am trying to assign the code to a button handler but several of the key words from the assemblies I have added show up as “does not exist in the current contex”. This is true of “Imports” which causes several errors with “SpreadsheetDocument” and its assigned variable “spreadsheet”. Also InsertSharedStringItem, InsertWorksheet and InsertCellInWorksheet show up as “does not exist in the current contex” all of which come from the added assemblies in DocumentFormat.OpenXml .
That is unless I change the code to not include “public class GeneratedClass” and make the code directly follow “namespace Inserting_cells_into_spreadsheet”. This however results in an error with the cellvalue and enumValue. It states “expected Class, delegate, enum, interface, struct”.
How do I clean this code up to allow me to assign it to a button handler.
Code Block below followed by a screen shot of the error underlined in the IDE
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; //first "Add Reference" "Assemblies" named "DocumentFormat.OpenXml" and "WindowsBase" //needed for using the open xml sdk to insert cells into spreadsheet using DocumentFormat.OpenXml; using DocumentFormat.OpenXml.Packaging; using DocumentFormat.OpenXml.Spreadsheet; namespace Inserting_cells_into_spreadsheet { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button1_Click(object sender, EventArgs e) { GeneratedClass gc = new GeneratedClass(); gc.InsertText(@"C:\Projects\MAR.xlsx", "InsertText"); } } } namespace Inserting_cells_into_spreadsheet { public class GeneratedClass { // Given a document name and text, // inserts a new worksheet and writes the text to cell "A1" of the new worksheet. public void InsertText(string docName, string text) { // Open the document for editing. Imports (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) { // Get the SharedStringTablePart. If it does not exist, create a new one. SharedStringTablePart shareStringPart; if (spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().Count() > 0) { shareStringPart = spreadSheet.WorkbookPart.GetPartsOfType<SharedStringTablePart>().First(); } else { shareStringPart = spreadSheet.WorkbookPart.AddNewPart<SharedStringTablePart>(); } // Insert the text into the SharedStringTablePart. int index = InsertSharedStringItem(text, shareStringPart); // Insert a new worksheet. WorksheetPart worksheetPart = InsertWorksheet(spreadSheet.WorkbookPart); // Insert cell A1 into the new worksheet. Cell cell = InsertCellInWorksheet("A", 1, worksheetPart); // Set the value of cell A1. cell.CellValue = new CellValue(index.ToString()); cell.DataType = new EnumValue<CellValues>(CellValues.SharedString); // Save the new worksheet. worksheetPart.Worksheet.Save(); } } } }