如果当前excel表具有记录,我们如何插入excel表的下一行?

时间:2016-09-01 12:48:33

标签: c# c#-4.0 c#-3.0

如果在从c#导出数据期间记录了大量记录,我们如何才能编写excel表的下一行? 它是我用于导出数据的代码。脱颖而出 。我无法通过此代码将数据导出到下一行

 private void ExportToOxml(bool firstTime)
    {
        if (count == 0)
        {
            ResultsData.Columns.Remove("TotalRecords");
            ResultsData.Columns.Remove("PageIndex");
            // fileName = @"C:\MyExcel.csv";

            //Delete the file if it exists. 
            if (firstTime && File.Exists(xsfd.FileName))
            {
                File.Delete(fileName);
            }
            count++;
        }

        if (firstTime)
        {
            //This is the first time of creating the excel file and the first sheet.
            // Create a spreadsheet document by supplying the filepath.
            // By default, AutoSave = true, Editable = true, and Type = xlsx.
            SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.
                Create(fileName, SpreadsheetDocumentType.Workbook);

            // Add a WorkbookPart to the document.
            WorkbookPart workbookpart = spreadsheetDocument.AddWorkbookPart();
            workbookpart.Workbook = new Workbook();

            // Add a WorksheetPart to the WorkbookPart.
            var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            var sheetData = new SheetData();
            worksheetPart.Worksheet = new Worksheet(sheetData);
            var bold1 = new System.Windows.Documents.Bold();
            CellFormat cf = new CellFormat();

            // Add Sheets to the Workbook.
            Sheets sheets;
            sheets = spreadsheetDocument.WorkbookPart.Workbook.
                AppendChild<Sheets>(new Sheets());

            // Append a new worksheet and associate it with the workbook.
            var sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.
                    GetIdOfPart(worksheetPart),
                SheetId = sheetId,
                Name = "Sheet" + sheetId
            };
            sheets.Append(sheet);

            //Add Header Row.
            var headerRow = new Row();
            foreach (DataColumn column in ResultsData.Columns)
            {
                var cell = new Cell { DataType = CellValues.String, CellValue = new CellValue(column.ColumnName) };
                headerRow.AppendChild(cell);
            }
            sheetData.AppendChild(headerRow);

            foreach (DataRow row in ResultsData.Rows)
            {
                var newRow = new Row();
                foreach (DataColumn col in ResultsData.Columns)
                {
                    var cell = new Cell
                    {
                        DataType = CellValues.String,
                        CellValue = new CellValue(row[col].ToString())
                    };
                    newRow.AppendChild(cell);
                }

                sheetData.AppendChild(newRow);
            }
            workbookpart.Workbook.Save();

            spreadsheetDocument.Close();
        }
        else
        {
            // Open the Excel file that we created before, and start to add sheets to it.
            var spreadsheetDocument = SpreadsheetDocument.Open(fileName, true);

            var workbookpart = spreadsheetDocument.WorkbookPart;
            if (workbookpart.Workbook == null)
                workbookpart.Workbook = new Workbook();

            var worksheetPart = workbookpart.AddNewPart<WorksheetPart>();
            var sheetData = new SheetData();
            worksheetPart.Worksheet = new Worksheet(sheetData);
            var sheets = spreadsheetDocument.WorkbookPart.Workbook.Sheets;

            if (sheets.Elements<Sheet>().Any())
            {
                //Set the new sheet id
                sheetId = sheets.Elements<Sheet>().Max(s => s.SheetId.Value) + 1;
            }
            else
            {
                sheetId = 1;
            }

            // Append a new worksheet and associate it with the workbook.
            var sheet = new Sheet()
            {
                Id = spreadsheetDocument.WorkbookPart.
                    GetIdOfPart(worksheetPart),
                SheetId = sheetId,
                Name = "Sheet" + sheetId
            };
            sheets.Append(sheet);

            //Add the header row here.
            var headerRow = new Row();

            foreach (DataColumn column in ResultsData.Columns)
            {
                var cell = new Cell { DataType = CellValues.String, CellValue = new CellValue(column.ColumnName) };
                headerRow.AppendChild(cell);
            }
            sheetData.AppendChild(headerRow);

            foreach (DataRow row in ResultsData.Rows)
            {
                var newRow = new Row();

                foreach (DataColumn col in ResultsData.Columns)
                {
                    var cell = new Cell
                    {
                        DataType = CellValues.String,
                        CellValue = new CellValue(row[col].ToString())
                    };
                    newRow.AppendChild(cell);
                }

                sheetData.AppendChild(newRow);
            }

            workbookpart.Workbook.Save();

            // Close the document.
            spreadsheetDocument.Close();
        }
    }

0 个答案:

没有答案