Click here to Skip to main content
16,011,849 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi,

I want to create a pivot table Excel sheet from sql server database data in c# as well as create a pivot chart using that data.

thanking you.
Posted
Comments
ZurdoDev 7-Jun-13 8:51am    
It will take a lot of code. Were you expecting us to give you an entire project? Where are you stuck?

1 solution

Why Excel pivot table? I would suggest you to use SQL Pivots:

See these articles:
Walkthrough: Creating a Chart in Excel Based on SQL Server Data[^] -my favorite ;)
How to PIVOT Data Using T-SQL[^]
Using PIVOT and UNPIVOT[^]

[EDIT #1]
See questions about: How to export data from SQL SERVER to Excel[^]
 
Share this answer
 
v2
Comments
connect2manas 10-Jun-13 8:35am    
i have created the sample .but not displaying any error but not Export to Excel Happening.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Microsoft.Office.Interop.Excel;
//using Microsoft.Office.Interop;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;

using System.Collections;
using System.Text;
using System.Data;
using System.Reflection;





namespace WebApplication1
{
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
static object useDefault = Type.Missing;
protected void Button1_Click(object sender, EventArgs e)
{

//string workBookName = @"C:\temp\pivottablesample.xlsx";

string connection = @"OLEDB;Provider=SQLOLEDB.1;Integrated Security=SSPI;Data Source=178.10.10.200;Initial Catalog=Employee";

string command = "select * from EMP";

Microsoft.Office.Interop.Excel.Application app = new Microsoft.Office.Interop.Excel.Application();

Microsoft.Office.Interop.Excel.Workbook workbook = (Microsoft.Office.Interop.Excel.Workbook)app.Workbooks.Add(Type.Missing);


Microsoft.Office.Interop.Excel.PivotCache pivotCache = workbook.PivotCaches().Add(Microsoft.Office.Interop.Excel.XlPivotTableSourceType.xlExternal, Type.Missing);

pivotCache.Connection = connection;

pivotCache.MakeConnection();

pivotCache.MaintainConnection = true;

pivotCache.CommandText = command;

Microsoft.Office.Interop.Excel.Worksheet sheet = (Microsoft.Office.Interop.Excel.Worksheet)workbook.ActiveSheet;

pivotCache.CommandType = Microsoft.Office.Interop.Excel.XlCmdType.xlCmdSql;

Microsoft.Office.Interop.Excel.PivotTables pivotTables = (Microsoft.Office.Interop.Excel.PivotTables)sheet.PivotTables(Type.Missing);

Microsoft.Office.Interop.Excel.PivotTable pivotTable = pivotTables.Add(pivotCache, app.ActiveCell, "PivotTable1", Type.Missing, Type.Missing);

pivotTable.SmallGrid = false;
pivotTable.ShowTableStyleRowStripes = true;
pivotTable.TableStyle2 = "PivotStyleLight1";

Microsoft.Office.Interop.Excel.PivotField pageField = (Microsoft.Office.Interop.Excel.PivotField)pivotTable.PivotFields("EName");

pageField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlPageField;

Microsoft.Office.Interop.Excel.PivotField rowField = (Microsoft.Office.Interop.Excel.PivotField)pivotTable.PivotFields("EName");

rowField.Orientation = Microsoft.Office.Interop.Excel.XlPivotFieldOrientation.xlRowField;

pivotTable.AddDataField(pivotTable.PivotFields("ESalary"), "Sum of EmpSalary", Microsoft.Office.Interop.Excel.XlConsolidationFunction.xlSum);


//workbook.SaveAs(workBookName, useDefault, useDefault,
// useDefault, useDefault, useDefault,
// XlSaveAsAccessMode.xlNoChange, useDefault, useDefault,
// useDefault, useDefault, useDefault);


}


}
}
Member 10316578 9-Jan-14 9:48am    
it is not showing any response after button click
connect2manas 10-Jun-13 8:35am    
Plz find the error and reply me.
Member 10316578 10-Jan-14 3:40am    
I have written the same code in asp.net but is is not working plz help:
Maciej Los 12-Jun-13 7:29am    
Please, update your question using "Improve question" widget ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900