Quantcast
Channel: SharePoint 2010 - Development and Programming forum
Viewing all articles
Browse latest Browse all 11571

SharePoint Excel sheet generation

$
0
0

Hey everyone,

I am working on creating a workflow that will take a select portion of a document library, write each item in a row on an excel sheet, and save that excel in a different library once a week. I know that SharePoint can export lists and libraries already with the capability to refresh data, however my requirements for this workflow is that the information from the past week is preserved with out any user input.

I've borrowed the general premise of the workflow from here: http://msdn.microsoft.com/en-us/library/office/gg317441(v=office.14).aspx

However my code gives back a 'System.InvalidOperationException: Sequence contains no elements' error when I use a blank test excel sheet as the template. When I do have data or have changed something such as the background color of the cells the excel is generated, but when attempting to open the file it is corrupted and no data is there. If I attempt no data input, then a blank sheet (which the template is) is generated fine.

Below is my code that I am using. If anyone has tips, recommendations, or helpful links please post them! Thanks for all your help!

try
            {
                using (SPSite sourceSite = new SPSite(workflowProperties.SiteUrl))
                {
                    SPFile sourceFile = sourceSite.RootWeb.GetFile(sourceDocumentPath);

                    using (SPSite destinationSite = new SPSite(destinationSitePath))
                    {
                        using (SPWeb web = destinationSite.OpenWeb())
                        {
                            if (sourceFile != null)
                            {
                                SPFolder destinationDocumentLibrary = web.Folders[destinationDocumentLibraryName];

                                using (Stream sourceFileStream = sourceFile.OpenBinaryStream())
                                {
                                    using (SpreadsheetDocument spreadSheet = SpreadsheetDocument.Open(sourceFileStream, true))
                                    {
                                        WorksheetPart worksheetPart = ReturnWorksheetPart(spreadSheet, "Sheet1");
                                        if (worksheetPart != null)
                                        {
                                            Worksheet worksheet = worksheetPart.Worksheet;
                                            SPList list = web.Lists["Status Report Library"];
                                            SPView currentWeekView = list.Views["Current Week"];
                                            SPListItemCollection listCollection = list.Items;
                                            DateTime dt = DateTime.Today.AddDays(-7);

                                            foreach (SPListItem i in listCollection)
                                            {
                                                var v = i["Created"];
                                                DateTime created = Convert.ToDateTime(v);
                                                if (created > dt)
                                                {
                                                    InsertTextCellValue(worksheet, "A", 1, "Test");
                                                    InsertNumberCellValue(worksheet, "A", 2, "1");
                                                    destinationDocumentLibrary.Files.Add("test.xls", sourceFileStream, true);
                                                }
                                            }                                           
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                WriteToHistoryList("Error", ex.ToString());
            }
        }//end code1

        private static WorksheetPart ReturnWorksheetPart(SpreadsheetDocument document, string sheetName)
        {
            IEnumerable<Sheet> sheets =document.WorkbookPart.Workbook.GetFirstChild<Sheets>().Elements<Sheet>().Where(s => s.Name == sheetName);
            if (sheets.Count() == 0)
            {
                return null;
            }
            string id = sheets.First().Id.Value;
            WorksheetPart worksheetPart =(WorksheetPart)document.WorkbookPart.GetPartById(id);
            return worksheetPart;
        }//end ReturnWorksheetPart

        private void InsertTextCellValue(Worksheet worksheet, string column, uint row, string value)
        {
            Cell cell = ReturnCell(worksheet, column, row);
            CellValue v = new CellValue();
            v.Text = value;
            cell.AppendChild(v);
            cell.DataType = new EnumValue<CellValues>(CellValues.String);
            worksheet.Save();
        }//end InsertTextCellValue

        private void InsertNumberCellValue(Worksheet worksheet, string column, uint row, string value)
        {
            Cell cell = ReturnCell(worksheet, column, row);
            CellValue v = new CellValue();
            v.Text = value;
            cell.AppendChild(v);
            cell.DataType = new EnumValue<CellValues>(CellValues.Number);
            worksheet.Save();
        }//end InsertNumberCellValue

        private static Cell ReturnCell(Worksheet worksheet, string columnName, uint row)
        {
            Row targetRow = ReturnRow(worksheet, row);

            if (targetRow == null)
                return null;

            return targetRow.Elements<Cell>().Where(c =>string.Compare(c.CellReference.Value, columnName + row,true) == 0).First();
        }//end ReturnCell

        private static Row ReturnRow(Worksheet worksheet, uint row)
        {
            return worksheet.GetFirstChild<SheetData>().Elements<Row>().Where(r => r.RowIndex == row).First();
        }//end ReturnRow

        void WriteToHistoryList(string comment, string outcome)//allows for programmatically writing to workflow history
        {
            SPWorkflow.CreateHistoryEvent(workflowProperties.Web, this.WorkflowInstanceId, 0, workflowProperties.Web.CurrentUser, new TimeSpan(),outcome, comment, "");
        }

    }//end class


Viewing all articles
Browse latest Browse all 11571

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>