Created
December 19, 2013 21:38
-
-
Save zippy1981/8046650 to your computer and use it in GitHub Desktop.
Example of a pivot table problem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using OfficeOpenXml; | |
using OfficeOpenXml.Table.PivotTable; | |
namespace ConsoleApplication1 | |
{ | |
internal static class Program | |
{ | |
private const string INPUT_XLS_URL = "https://nycopendata.socrata.com/api/views/spgx-ssye/rows.xlsx?accessType=DOWNLOAD"; | |
static void Main(string[] args) | |
{ | |
var webClient = new System.Net.WebClient(); | |
Console.Write("Downloading {0} . . .", INPUT_XLS_URL); | |
var xls = webClient.DownloadData(INPUT_XLS_URL); | |
Console.WriteLine("Done!"); | |
using (var stream = new MemoryStream(xls)) | |
using (var excelPackage = new ExcelPackage(stream)) | |
{ | |
var worksheet = excelPackage.Workbook.Worksheets["DCA Current Licensees"]; | |
var dataRange = worksheet.Cells[1, 1, worksheet.Dimension.End.Row, worksheet.Dimension.End.Column]; | |
var pivotWorksheet = excelPackage.Workbook.Worksheets.Add("Pivot"); | |
var pivotRange = pivotWorksheet.Names.Add("PIVOT_RANGE", pivotWorksheet.Cells[1, 1]); | |
var pivot = pivotWorksheet.PivotTables.Add(pivotRange, dataRange, "DCA_PIVOT"); | |
pivot.Compact = false; | |
pivot.CompactData = false; | |
pivot.Outline = false; | |
pivot.DataOnRows = false; | |
pivot.ColumGrandTotals = true; | |
pivot.UseAutoFormatting = false; | |
pivot.EnableDrill = false; | |
pivot.ShowDrill = false; | |
//pivot.RowHeaderCaption = null; // This is what appears there. | |
pivot.RowFields.Add(pivot.Fields["Borough"]); | |
pivot.RowFields.Add(pivot.Fields["Industry"]); | |
pivot.DataFields.Add(pivot.Fields["ZIP Code"]).Function = DataFieldFunctions.Count; | |
pivot.DataFields.Add(pivot.Fields["City"]).Function = DataFieldFunctions.Count; | |
foreach (var field in pivot.Fields) | |
{ | |
field.Outline = false; // This is what makes the peril and region render as a table. | |
} | |
var outfile = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),"DCA-pivox" + DateTime.Now.ToString("s").Replace(":", "") + ".xlsx"); | |
Console.Write("Saving {0} . . .", outfile); | |
using (var outStream = File.OpenWrite(outfile)) | |
{ | |
excelPackage.SaveAs(outStream); | |
} | |
Console.WriteLine("Done!"); | |
Console.Write("Press any key con continue . . . "); | |
Console.ReadKey(false); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment