public static void InsertText(string docName, string text) { // Open the document for editing. Using (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(); } }
Error 8 The best overloaded method match for 'WindowsFormsApplication2.Form1.InsertWorksheet(string)' has some invalid arguments C:\App\WindowsFormsApplication9_4_0_1\WindowsFormsApplication2\Form1.cs 468 47 WindowsFormsApplication2
public static void InsertWorksheet(string docName) { // Open the document for editing. using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true)) { // Add a blank WorksheetPart. WorksheetPart newWorksheetPart = spreadSheet.WorkbookPart.AddNewPart<WorksheetPart>(); newWorksheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(new SheetData()); //DocumentFormat.OpenXml.Spreadsheet.Worksheet sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<Sheets>(); DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = spreadSheet.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>(); string relationshipId = spreadSheet.WorkbookPart.GetIdOfPart(newWorksheetPart); // Get a unique ID for the new worksheet. uint sheetId = 1; if (sheets.Elements<Sheet>().Count() > 0) { sheetId = sheets.Elements<Sheet>().Select(s => s.SheetId.Value).Max() + 1; } // Give the new worksheet a name. string sheetName = "Sheet Overall"; // Append the new worksheet and associate it with the workbook. Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName }; sheets.Append(sheet); } } public static void InsertText(string docName, string text) { // Open the document for editing. using (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); ...
I encounter this
Open in new window
due to last line belowOpen in new window
while the codes are fromhttps://msdn.microsoft.com/en-us/library/office/cc861607.aspx?f=255&MSPPError=-2147217396