Solved

Problem to event

Posted on 2016-10-07
3
50 Views
Last Modified: 2016-10-07
Hi,
How to correct

        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();
            }
        }

Open in new window

due to these errors?
Error	1	) expected	C:\App\WindowsFormsApplication9_4_0_1\WindowsFormsApplication2\Form1.cs	451	40	WindowsFormsApplication2
Error	2	Invalid expression term ')'	C:\App\WindowsFormsApplication9_4_0_1\WindowsFormsApplication2\Form1.cs	451	93	WindowsFormsApplication2
Error	3	; expected	C:\App\WindowsFormsApplication9_4_0_1\WindowsFormsApplication2\Form1.cs	451	93	WindowsFormsApplication2
Error	4	; expected	C:\App\WindowsFormsApplication9_4_0_1\WindowsFormsApplication2\Form1.cs	451	94	WindowsFormsApplication2

Open in new window

0
Comment
Question by:HuaMinChen
  • 2
3 Comments
 
LVL 49

Assisted Solution

by:Ryan Chong
Ryan Chong earned 500 total points
ID: 41833129
Using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))

Open in new window

change Using  to using

using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(docName, true))

Open in new window

0
 
LVL 10

Author Comment

by:HuaMinChen
ID: 41833169
Many thanks.
I encounter this
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

Open in new window

due to last line below
        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);
                ...

Open in new window

while the codes are from
https://msdn.microsoft.com/en-us/library/office/cc861607.aspx?f=255&MSPPError=-2147217396
0
 
LVL 49

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41833250
I amended method: InsertWorksheet as follows and it should work now for you:

// Inserts a new worksheet.
        public static WorksheetPart InsertWorksheet(WorkbookPart wbpart)
        {
            // Add a blank WorksheetPart.
            WorksheetPart newWorksheetPart = wbpart.AddNewPart<WorksheetPart>();
            newWorksheetPart.Worksheet = new Worksheet(new SheetData());

            Sheets sheets = wbpart.Workbook.GetFirstChild<Sheets>();
            string relationshipId = wbpart.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" + sheetId;

            // Append the new worksheet and associate it with the workbook.
            Sheet sheet = new Sheet() { Id = relationshipId, SheetId = sheetId, Name = sheetName };
            sheets.Append(sheet);

            return newWorksheetPart;
        }

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The viewer will learn how to implement Singleton Design Pattern in Java.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now