简体   繁体   English

向背后的Excel数据透视表代码添加数据

[英]Add data to excel pivot table code behind

I have some excel files with some pivot tables. 我有一些带有一些数据透视表的Excel文件。 Now i need to add records into pivot table source from code behind. 现在,我需要将记录从后面的代码添加到数据透视表源中。 Can any one guide me how to do that. 谁能指导我做到这一点。

Finally i got a solution. 终于我找到了解决方案。 Problem is solved using bellow code 使用波纹管代码解决了问题

    public class PivotSheetProp
    {
        public int ID { get; set; }
        public string SheetName { get; set; }
        public int StartColumnIndex { get; set; }
        public int StartRowIndex { get; set; }
    }

    public static void ReadExistingExcel(DataSet ds, List<PivotSheetProp> accPivotList, string path)
    {
        Microsoft.Office.Interop.Excel.Application oXL;
        Microsoft.Office.Interop.Excel.Workbook mWorkBook;
        Microsoft.Office.Interop.Excel.Sheets mWorkSheets;
        Microsoft.Office.Interop.Excel.Worksheet mWSheet1;

        oXL = new Microsoft.Office.Interop.Excel.Application();
        oXL.Visible = false;
        oXL.DisplayAlerts = false;
        mWorkBook = oXL.Workbooks.Open(path, 0, false, 5, "", "", false, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", true, false, 0, true, false, false);
        try
        {
            //Get all the sheets in the workbook
            mWorkSheets = mWorkBook.Worksheets;
            int tblCount = 0;
            foreach (var v in accPivotList)
            {
                //Get the allready exists sheet
                mWSheet1 = (Microsoft.Office.Interop.Excel.Worksheet)mWorkSheets.get_Item(v.SheetName);

                //Set alignment and font size
                mWSheet1.Cells.HorizontalAlignment = XlHAlign.xlHAlignCenter;
                mWSheet1.Cells.VerticalAlignment = XlVAlign.xlVAlignCenter;
                mWSheet1.Cells.Font.Size = 11;

                Microsoft.Office.Interop.Excel.Range range = mWSheet1.UsedRange;
                int colCount = range.Columns.Count;
                int rowCount = range.Rows.Count;
                int startRowCount = v.StartRowIndex;
                if (startRowCount != 0)
                {
                    rowCount = startRowCount;
                }
                for (int index = 1; index <= ds.Tables[tblCount].Rows.Count; index++)
                {
                    int cellIndex = v.StartColumnIndex;
                    foreach (object tc in ds.Tables[tblCount].Rows[index - 1].ItemArray)
                    {
                        mWSheet1.Cells[rowCount + index, cellIndex++] = tc.ToString();
                    }
                }

                tblCount++;
            }

            foreach (Microsoft.Office.Interop.Excel.Worksheet pivotSheet in mWorkSheets)
            {
                Microsoft.Office.Interop.Excel.PivotTables pivotTables = pivotSheet.PivotTables();
                int pivotTablesCount = pivotTables.Count;
                if (pivotTablesCount > 0)
                {
                    for (int i = 1; i <= pivotTablesCount; i++)
                    {
                        pivotTables.Item(i).RefreshTable(); //The Item method throws an exception
                    }
                }
            }

            ///For 97-03
            //mWorkBook.SaveAs(path, Microsoft.Office.Interop.Excel.XlFileFormat.xlWorkbookNormal,
            //Missing.Value, Missing.Value, Missing.Value, Missing.Value, Microsoft.Office.Interop.Excel.XlSaveAsAccessMode.xlExclusive,
            //Missing.Value, Missing.Value, Missing.Value,
            //Missing.Value, Missing.Value);
            //mWorkBook.Close(Missing.Value, Missing.Value, Missing.Value);

            ///For 2007
            mWorkBook.SaveAs(path);
            mWorkBook.Close();
        }
        catch (Exception ex)
        {
            System.Windows.Forms.MessageBox.Show("Error in updating excel file.\r\nFile name [" + path + "].\r\n\r\nMessage :- " + ex.Message, 
                "Repeat report", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Error);
        }
        mWSheet1 = null;
        mWorkBook = null;
        oXL.Quit();
        GC.WaitForPendingFinalizers();
        GC.Collect();
        GC.WaitForPendingFinalizers();
        GC.Collect();
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM