Click here to Skip to main content
15,896,153 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hi
I want to save my sql query result as excel(pivot) file.
I use from below code:


SQL
insert into OPENROWSET
('Microsoft.ACE.OLEDB.12.0',
'EXCEL 12.0 Xml;Database=C:\Test.xlsx;HDR=YES;','SELECT * FROM [sheet1$]')
select * from SalesItem


Before this, every time i need to create excel file and add columns in it.which works fine but creates normal Excel file.
But I need to create excel pivot file automatically in a specified folder and at the beginning of every month(separate file for every month).How can i pass month and year for my query.
I am using SQL server 2008 R2 32-bit and Office 2010 standard.
Posted
Updated 5-Sep-15 22:43pm
v2
Comments
Tomas Takac 6-Sep-15 8:24am    
Export the data to one tab and have the pivot in a second tab referencing the data in first tab.
Member 11584553 6-Sep-15 21:45pm    
Thank you Tomas Takac. Actually creation of Excel file and conversion into pivot table should be done automatically at the beginning of every month. How should i do that

Create a table in SQL server that holds your pivoted data and export that to excel.
 
Share this answer
 
Comments
Member 11584553 7-Sep-15 0:11am    
Actually SalesItem is a view name(which i used above) . In that view i joined 3 different tables. when i execute view i get 8 Columns.That data should be automatically exported as pivot table every month.
 
Share this answer
 
Comments
Member 11584553 8-Sep-15 22:49pm    
I came out with a different approach...

I need to write a batch file that copies a file to existing folder and renames it with current month and year. I used the below code

At the moment, my batch file consists of only this command:

**@ECHO OFF
copy /y D:\SalesMixExp\Sales_Mix_v1.2_082015.xlsx D:\SalesMixExp\Sales_Mix_v1.2_092015.xlsx**

It works fine. But every time i need to change the newly created filename with current month and year. i.e for ex: 092015 in my filename is **sep2015** and for next month it should be automatically renamed as **Sales_Mix_v1.2_102015.xlsx**...Please help me in resolving my problem

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