This command requires at least two rows of source data. You cannot use the command on a selection in only one row. Try the following:
• If you're using an advanced filter, select a range of cells that contains at least two rows of data. Then click the Advanced Filter command again.
• If you're creating a PivotTable report or PivotChart report, type a cell reference or select a range that includes at least two rows of data.
I am creating Pivot Table in C# and I am getting this error while executing (Excel.PivotTable)oSheet.PivotTables().Add method
To fix it, replace the following code
string fileTest = @"C:\Temp\ExcelTest\test.xlsx";
if (File.Exists(fileTest))
{
File.Delete(fileTest);
}
Excel.Application oApp;
Excel.Worksheet oSheet;
Excel.Workbook oBook;
oApp = new Excel.Application();
oBook = oApp.Workbooks.Add();
oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
oSheet.Cells[1, 1] = "Name";
oSheet.Cells[1, 2] = "Salary";
oSheet.Cells[2, 1] =2;
oSheet.Cells[2, 2] = 4;
oSheet.Cells[3, 1] = 5;
oSheet.Cells[3, 2] = 10;
Excel.Range oRange;
oRange = oSheet.Cells[3, 1];
if (oApp.Application.Sheets.Count < 2 ) {
oSheet = (Excel.Worksheet)oBook.Worksheets.Add();
}
else{
oSheet = oApp.Worksheets[2];
}
oSheet.Name = "output";
// specify first cell for pivot table on second sheet
Excel.Range oRange2 = oSheet.Cells[1,1];
Excel.Range oRange3 = oSheet.Cells[2, 1];
Excel.Range oRange4 = oSheet.get_Range(oRange2, oRange3);
// create pivot cache and table
Excel.PivotCache oPivotCache = (Excel.PivotCache)oBook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlDatabase,oRange);
Excel.PivotTable oPivotTable = (Excel.PivotTable)oSheet.PivotTables().Add(PivotCache: oPivotCache, TableDestination: oSheet.Range["A1","A2"], TableName: "Summary");
with
string fileTest = @"C:\Temp\ExcelTest\test.xlsx";
if (File.Exists(fileTest))
{
File.Delete(fileTest);
}
Excel.Application oApp;
Excel.Worksheet oSheet;
Excel.Workbook oBook;
oApp = new Excel.Application();
oBook = oApp.Workbooks.Add();
oSheet = (Excel.Worksheet)oBook.Worksheets.get_Item(1);
oSheet.Cells[1, 1] = "Name";
oSheet.Cells[1, 2] = "Salary";
oSheet.Cells[2, 1] =2;
oSheet.Cells[2, 2] = 4;
oSheet.Cells[3, 1] = 5;
oSheet.Cells[3, 2] = 10;
Excel.Range oRange;
oRange = oSheet.Range["A1", "B3"];
if (oApp.Application.Sheets.Count < 2 ) {
oSheet = (Excel.Worksheet)oBook.Worksheets.Add();
}
else{
oSheet = oApp.Worksheets[2];
}
oSheet.Name = "output";
// specify first cell for pivot table on second sheet
Excel.Range oRange2 = oSheet.Cells[1,1];
Excel.Range oRange3 = oSheet.Cells[2, 1];
Excel.Range oRange4 = oSheet.get_Range(oRange2, oRange3);
// create pivot cache and table
Excel.PivotCache oPivotCache =
(Excel.PivotCache)oBook.PivotCaches().Add(Excel.XlPivotTableSourceType.xlDatabase,oRange);
Excel.PivotTable oPivotTable =
(Excel.PivotTable)oSheet.PivotTables().Add(PivotCache: oPivotCache,
TableDestination: oSheet.Range["A1","A2"], TableName: "Summary");
No comments:
Post a Comment