void frmdrill_FormClosed(object sender, FormClosedEventArgs e) { if (frmdrill.DialogResult == System.Windows.Forms.DialogResult.OK) { System.Data.DataTable dt = frmdrill._dataTable; Excel.DataTable dtEx; Excel.Workbook _wbook = (Excel.Workbook)ExcelApp.ActiveWorkbook; _wbook.Sheets.Add(Type.Missing, Type.Missing, 1, Type.Missing); Excel.Worksheet _wsheet = (Excel.Worksheet)ExcelApp.ActiveSheet; for (int i = 0; i < dt.Columns.Count; i++) { Excel.Range _range = (Excel.Range)_wsheet.Cells[1, i + 1]; _range.Font.Bold = true; _range.Value = dt.Columns[i].ColumnName; } for (int i = 0; i < dt.Rows.Count; i++) { for (int j = 0; j < dt.Columns.Count; j++) { Excel.Range _range = (Excel.Range)_wsheet.Cells[i + 2, j + 1]; _range.Value = dt.Rows[i][j]; } } string add = _wsheet.Name + "!R1C1:R" + (dt.Rows.Count + 1) + "C" + dt.Columns.Count; _wbook.Sheets.Add(Type.Missing, Type.Missing, 1, Type.Missing); Excel.Worksheet _wpivotsheet = (Excel.Worksheet)ExcelApp.ActiveSheet; string des = _wpivotsheet.Name + "!R3C1"; _wbook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlDatabase, add).CreatePivotTable(des, "PivotTable1", Type.Missing);//, Excel. Excel.XlPivotTableVersionList.xlPivotTableVersion10); } }