Creating pivot table using ClosedXML - c#

I'm trying to create a Pivot table using ClosedXML V0.91.1, but I keep getting problems with my excel file having unreadable content and then the excel workbook removing my pivot table when clicking Yes below.
Below is that shows when I hit Yes. It's removing my pivot table.
My Pivot Table is getting data from a table that is created from a history of TFS Changesets. The changesets are set into a IEnumerable<Changeset> which is then converted into a DataTable object which include the column headings.
A table is then created from the DataTable which is the source of the PivotTable. This is the code that I'm using:
public bool CreateReport(IEnumerable<Changeset> changesets)
{
workbook = new XLWorkbook();
var sumSheet= workbook.Worksheets.Add("Summary");
// Converting IEnumerable<Changeset> into DataTable
DataTable changesetTable = ConvertToDataTable(changesets);
// Table
var sumTable = sumSheet.Cell(1, 1).InsertTable(changesetTable.AsEnumerable(), "SummaryTable", true);
// Table - Formatting table
tableWithData.Column("A").Cells(firstRow, lastRow).DataType = XLDataType.Number;
tableWithData.Column("C").Cells(firstRow, lastRow).DataType = XLDataType.DateTime;
tableWithData.Column("C").Cells(firstRow, lastRow).Style.DateFormat.Format = "d-MMM-yyy";
sumSheet.Columns().AdjustToContents();
// Pivot Table
var header = sumTable.Range(1, 1, 1, 6); // A1 to F1
var range = sumTable.DataRange;
var dataRange = sumSheet.Range(header.FirstCell(), range.LastCell());
var pivotSheet = workbook.Worksheets.Add("History Report");
var pivotTable = pivotSheet.PivotTables.AddNew("PivotTable", pivotSheet.Cell(1, 1), dataRange);
// Pivot Table - Formatting table
pivotTable.ShowPropertiesInTooltips = false;
pivotTable.RepeatRowLabels = false;
pivotTable.ShowGrandTotalsColumns = false;
pivotTable.ShowGrandTotalsRows = false;
pivotTable.ShowEmptyItemsOnRows = true;
pivotTable.ShowEmptyItemsOnColumns = true;
pivotTable.ShowExpandCollapseButtons = false;
pivotTable.Layout = XLPivotLayout.Tabular;
pivotTable.RowLabels.Add("Changeset");
pivotTable.RowLabels.Add("Committer");
pivotTable.RowLabels.Add("Date");
pivotTable.RowLabels.Add("Comment");
pivotTable.RowLabels.Add("File Changes");
pivotTable.RowLabels.Add("Source");
// Go off and save the workbook.
bool saved = SaveFile();
return saved;
}
I believe the problem is with how I am selecting the dataRange for the source of the Pivot Table.
var header = sumTable.Range(1, 1, 1, 6); // A1 to F1
var range = sumTable.DataRange;
var dataRange = sumSheet.Range(header.FirstCell(), range.LastCell());
I followed the example found on their wiki, but it gives those errors with my implementation. The only difference between my problem and the example, is that I am getting my source for the Pivot Table from a DataTable, and I am only inserting values into RowLabels in my Pivot Table.
If it helps, this is how I convert the IEnumerable<Changeset> to a DataTable
private DataTable ConvertToDataTable(IEnumerable<Changeset> changesets)
{
DataTable table = new DataTable();
table.Columns.Add("Changeset", typeof(int));
table.Columns.Add("Committer", typeof(string));
table.Columns.Add("Date", typeof(DateTime));
table.Columns.Add("Comment", typeof(string));
table.Columns.Add("File Changes", typeof(string));
table.Columns.Add("Source", typeof(string));
foreach(Changeset changeset in changesets) // Loop over all changesets
{
int changesetID = changeset.ChangesetId;
string committer = changeset.CommitterDisplayName;
DateTime creationDate = changeset.CreationDate;
string comment = changeset.Comment;
foreach(Change change in changeset.Changes) // Loop over all Changes in changeset
{
string filename = change.Item.ServerItem.Substring(change.Item.ServerItem.LastIndexOf("/") + 1);
table.Rows.Add(changesetID, committer, creationDate, comment, filename, change.Item.ServerItem);
}
}
return table;
}

If I recall correctly, a pivot table in ClosedXML should have at least one value field.
pivotTable.Values.Add("somefield");

I know I am a bit late on this one, but in case anyone else tries to do this in future, my solution was as follows.
IXLPivotTable xlPvTable = TargetWorksheet.PivotTables.Add(
"PivotTable123",
TargetWorksheet.Cell(1, 1),
SourceWorksheet.Range(1, 1, MainData.Rows.Count, MainData.Columns.Count)
);
Then add the ColumnLabel and RowLabels etc..

Related

Hiding filter in the pivot table worksheet programatically

I'm trying to find a way to hide the filter field from the worksheet pivot table.
This is my code
// Format the Pivot Table.
pivotTable[1].Format(IExcel.XlPivotFormatType.xlReport2);
pivotTable[1].InGridDropZones = false;
pivotTable[1].SmallGrid = false;
pivotTable[1].ShowTableStyleRowStripes = true;
pivotTable[1].TableStyle2 = "PivotStyleLight1";
// Page Field
FilterPivotField[1] = (IExcel.PivotField)pivotTable[1].PivotFields(2);
FilterPivotField[1].Orientation = IExcel.XlPivotFieldOrientation.xlPageField;
FilterPivotField[1].Position = 1;
FilterPivotField[1].CurrentPage = "0";
Any idea?
Thanks,
Y

I need helping adding two different rows(with same columns) to the same datatable

Working on a windows form application that reads in data from csv files and adds the data to a Datagridview. I ran into an issue with all of the rows being added to the datable and being displayed on the datagridview. The datagridview displays the datarows from the first two if conditions and OneRow if condition only. It will not add the rows from the twoRow if condition if the datable and datagridview rows are populated with the OneRow if condition rows. But i want the rows from both OneRow and TwoRow to be displyed. Also the rows from TwoRow do populate the datatable and datagridview when I comment(/**/) out the OneRow if condition. But I need both to populate the table. Thanks in advance!
Construct.MainDataTable.Columns.Add("Date", typeof(DateTime));
Construct.MainDataTable.Columns.Add("Time");
Construct.MainDataTable.Columns.Add("Serial");
Construct.MainDataTable.Columns.Add("Type");
Construct.MainDataTable.Columns.Add("level");
Construct.MainDataTable.Columns.Add("price");
Construct.MainDataTable.Columns.Add(" Limit");
Construct.MainDataTable.Columns.Add("last Limit");
Construct.MainDataTable.Columns.Add("Data");
..........................
...............................................
DataRow oneRow = Construct.MainDataTable.NewRow();
DataRow twoRow = Construct.MainDataTable.NewRow();
dataGridView2.AllowUserToAddRows = false;
if (line.Split(',')[2].Equals("Time"))
{
time = line.Split(',')[3];
date = line.Split(',')[1];
}
if (line.Split(',')[2].Equals("Level"))
{
level = line.Split(',')[3];
}
//OneROw(IF condition)
if ((Convert.ToDecimal(line.Split(',')[8])) < (Convert.ToDecimal (line.Split(',')[12])))
{
type = line.Split(',')[1];
serial = line.Split(',')[7];
price = line.Split(',')[3];
Limit = line.Split(',')[8];
lastLimit = line.Split(',')[10];
Data = line.Split(',')[12];
oneRow["Date"] = date;
oneRow["Time"] = time;
oneRow["Serial"] = serial;
oneRow["Type"] = type;
oneRow["level"] = level;
oneRow["price"] = price;
oneRow[" Limit"] = Limit;
oneRow["last Limit"] = lastlimit;
oneRow["Data"] = Data;
Construct.MainDataTable.Rows.Add(oneRow);
}
//TwoROw(IF condition)
if ((line.Contains('"')) && ((line.Contains("NG"))))
{
price = line.Split(',')[3];
type = line.Split(',')[1];
serial = line.Split(',')[7];
Limit = line.Split('"')[7];
var valLimit = Limit.Split(',').Select(a => Convert.ToInt32(a, 16));
var limitJoin = String.Join(",", valLimit);
lastlimit = line.Split('"')[1];
var vallastLimit = lastlimit.Split(',').Select(d => Convert.ToInt32(d, 16));
var lastJoin = String.Join(",", vallastLimit);
Data = line.Split('"')[5];
var valDatas = Data.Split(',').Select(s => Convert.ToInt32(s, 16));
var dataJoin = String.Join(",", valDatas);
twoRow["Date"] = date;
twoRow["Time"] = time;
twoRow["Serial"] = serial;
twoRow["Type"] = type;
twoRow["level"] = level;
twoRow["price"] = price;
twoRow["Limit"] = limitJoin;
twoRow["last Limit"] = lastJoin;
twoRow["Data"] = dataJoin;
Construct.MainDataTable.Rows.Add(twoRow);
}
dataGridView2.DataSource = Construct.MainDataTable;
Can't add a comment because I don't have enough karma so I ask my questions here: So, if I understood your problem you can't add data from one .csv file if it have more then one row? Why are you using 2 different if conditions for row in .csv file?
If you have empty data in row never mind you can still place them to your DataTable column, so you can use loop to add data from .csv to your DataTable. Try some thing like this:
public static DataTable CsvToDataTable(string csv)
{
DataTable dt = new DataTable();
string[] lines = csv.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
Regex onlyDeimiterComma = new Regex(",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
for (int i = 0; i < lines.Length; i++)
{
DataRow row = dt.NewRow();
string[] cells = onlyDeimiterComma.Split(lines[i]);
for (int j = 0; j < cells.Length; j++)
{
if (i == 0)
{
if (j == 0)
{
dt.Columns.Add(cells[j], typeof(DateTime));
}
else
{
dt.Columns.Add(cells[j]);
}
}
else
{
row[j] = cells[j];
}
}
dt.Rows.Add(row);
}
return dt;
}
Just call this method anywhere in your code and give it string read from your .csv file.
You can try to compile this code here and see how it works on .csv data with different data (empty columns, quoted text, quoted commas)
UPD: If you need to fill DataTable from two different .csv files you can still use code above. Just call it twice for both files and then Merge two DataTable, like this:
DataTable dt = CsvToDataTable(csvFileOne);
DataTable dtTwo = CsvToDataTable(csvFileTwo);
dt.Merge(dtTwo);

change table style text in openxml for powerpoint generatron

I have this code using openxml sdk which generates table in PPT report.
This line is the reason for table style.
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
Style is :
I need to change the style and colors but I couldn't find anything for that. Please help.
private D.Table GenerateTable(int projectID, string reportType)
{
// Declare and instantiate table
D.Table table = new D.Table();
// Specify the required table properties for the table
D.TableProperties tableProperties = new D.TableProperties() { FirstRow = true, BandRow = false };
D.TableStyleId tableStyleId = new D.TableStyleId();
tableStyleId.Text = "{5C22544A-7EE6-4342-B048-85BDC9FD1C3A}";
tableProperties.Append(tableStyleId);
D.TableGrid tableGrid1 = new D.TableGrid();
System.Data.DataTable dtData = new System.Data.DataTable();
if (reportType == "projcharter")
{
//tblXBenefit
dtData = GetBenefit(projectID);
// Declare and instantiate tablegrid and colums
//D.TableGrid tableGrid1 = new D.TableGrid();
D.GridColumn gridColumn1 = new D.GridColumn() { Width = 1848000L };
D.GridColumn gridColumn2 = new D.GridColumn() { Width = 648000L };
D.GridColumn gridColumn3 = new D.GridColumn() { Width = 648000L };
D.GridColumn gridColumn4 = new D.GridColumn() { Width = 648000L };
tableGrid1.Append(gridColumn1);
tableGrid1.Append(gridColumn2);
tableGrid1.Append(gridColumn3);
tableGrid1.Append(gridColumn4);
}
table.Append(tableProperties);
table.Append(tableGrid1);
// Instantiate the table header row
D.TableRow tableHRow = new D.TableRow() { Height = 0L };
for (int column = 0; column < dtData.Columns.Count; column++)
{
tableHRow.Append(CreateTextCell(dtData.Columns[column].ToString()));
}
table.Append(tableHRow);
// Instantiate the table data row
for (int row = 0; row < dtData.Rows.Count; row++)
{
// Instantiate the table row
D.TableRow tableRow = new D.TableRow() { Height = 0L };
for (int column = 0; column < dtData.Columns.Count; column++)
{
tableRow.Append(CreateTextCell(dtData.Rows[row][column].ToString()));
}
table.Append(tableRow);
}
return table;
}
Links to previous questions:
create dynamic table in powerpoint using openXML with c# and ASP.net
unable to generate second table in PPT report using openxml
How to ignore/solve "Repair" message from Powerpoint report generated by OpenXML with ASP.net
Solved it.
So what I did was, I created a new ppt > inserted tables with required design manually in it and then saved it as ppt xml (you will see this option in save as).
Open that xml search for tablestyleid and use that value in the code.

C# Script LDAP not returning All Groups

Having a bit of problem with some C# LDAP Queries. The immediately most concerting one is that I appear to be missing approximately 1/3rd of the expected data set.
Have two screen shots attached of the result set.
In regards to the C# Filter
I am generating the filter here
public string GenerateFilter()
{
var LastRunDateTime = Variables.LastRunDateTime;
var filter = "(ObjectClass=group)";
/*
string filter = string.Format(
"(&(ObjectClass=group)(whenChanged>={0:yyyyMMddHHmmss.0Z}))",//This is the DateTime format it takes.
LastRunDateTime.AddHours(-11) // Always use UTC to make life easy. Otherwise you need to change the above time formatting.
); */
return filter;
}
I have commented out the initial code which is returning the same count for the first run
In Regards to the work horse part of the code I can't see any reason why it isn't returning all the values.
I have been checking out the missing values (managed to track them down with a bit of logic) and there is literally no configuration difference between them.
public override void CreateNewOutputRows()
{
/*
Add rows by calling the AddRow method on the member variable named "<Output Name>Buffer".
For example, call MyOutputBuffer.AddRow() if your output was named "MyOutput".
*/
DataTable workTable = new DataTable("Ad_Users");
DataColumn workColumn = workTable.Columns.Add("SID", typeof(string));
workTable.Columns.Add("ObjectCategory", typeof(string));
workTable.Columns.Add("ObjectGUID", typeof(string));
workTable.Columns.Add("CanonicalName", typeof(string));
workTable.Columns.Add("SAMAccount", typeof(string));
workTable.Columns.Add("distinguishedName", typeof(string));
workTable.Columns.Add("DisplayName", typeof(string));
workTable.Columns.Add("Description", typeof(string));
workTable.Columns.Add("WhenCreated", typeof(DateTime));
workTable.Columns.Add("WhenChanged", typeof(DateTime));
// workTable.Columns.Add("MemberOf", typeof(string));
var domainController = "[REDACTED]";
using (var domain = new System.DirectoryServices.DirectoryEntry("LDAP://" + domainController))
{
using (var searcher = new DirectorySearcher(domain, GenerateFilter()))
{
searcher.PropertiesToLoad.Add("ObjectSID");
searcher.PropertiesToLoad.Add("ObjectCategory");
searcher.PropertiesToLoad.Add("ObjectGuid");
searcher.PropertiesToLoad.Add("CN");
searcher.PropertiesToLoad.Add("SAMAccountName");
searcher.PropertiesToLoad.Add("DisplayName");
searcher.PropertiesToLoad.Add("distinguishedName");
searcher.PropertiesToLoad.Add("Description");
searcher.PropertiesToLoad.Add("WhenCreated");
searcher.PropertiesToLoad.Add("WhenChanged");
// searcher.PropertiesToLoad.Add("MemberOf");
foreach (SearchResult result in searcher.FindAll())
{
var de = result.GetDirectoryEntry();
var sidInBytes = (byte[])de.Properties["ObjectSID"].Value;
var GUID = (byte[])de.Properties["ObjectGuid"].Value;
Guid guid = new Guid(GUID);
//INSERT VALUES INTO DATATABLE
DataRow workRow = workTable.NewRow();
workRow["SID"] = new System.Security.Principal.SecurityIdentifier(sidInBytes, 0);
workRow["ObjectCategory"] = de.Properties["ObjectCategory"].Value;
workRow["ObjectGUID"] = guid;
workRow["CanonicalName"] = de.Properties["CN"].Value;
workRow["SAMAccount"] = de.Properties["SAMAccountName"].Value;
workRow["DisplayName"] = de.Properties["DisplayName"].Value;
workRow["distinguishedName"] = de.Properties["distinguishedName"].Value;
workRow["Description"] = de.Properties["Description"].Value;
workRow["WhenCreated"] = Convert.ToDateTime(de.Properties["WhenCreated"].Value);
workRow["WhenChanged"] = Convert.ToDateTime(de.Properties["WhenChanged"].Value);
Output0Buffer.AddRow();
Output0Buffer.ObjectSID = workRow["SID"].ToString();
Output0Buffer.ObjectCategory = workRow["ObjectCategory"].ToString();
Output0Buffer.ObjectGUID = workRow["ObjectGUID"].ToString();
Output0Buffer.CanonicalName = workRow["CanonicalName"].ToString();
Output0Buffer.SamAccountName = workRow["SAMAccount"].ToString();
Output0Buffer.DisplayName = workRow["DisplayName"].ToString();
Output0Buffer.DistinguishedName = workRow["distinguishedName"].ToString();
Output0Buffer.Description = workRow["Description"].ToString();
Output0Buffer.WhenCreated = Convert.ToDateTime(workRow["WhenCreated"]);
Output0Buffer.WhenChanged = Convert.ToDateTime(workRow["WhenChanged"]);
}
}
}
}
}
If anyone would be able to assist it would be greatly appreciated
To get comparable results you should use
Get-ADGroup -LDAPFilter "(objectClass=group)"

DataRows coming out null despite assigning values to them

I am trying to learn how to join two data tables into one using Linq. My linq query is working correctly and I can see expected values in it. However, when I loop the linq results, and assign the values to a newly created data row and add that row to a new data table, the rows come out empty.
Here is my code:
private void btnCombine_Click(object sender, EventArgs e)
{
var innerJoinQuery =
from strRow in StrDataTable.AsEnumerable()
join intRow in IntDataTable.AsEnumerable()
on strRow.Field<int>("IntID") equals intRow.Field<int>("ID")
select new {
IntOne = intRow.Field<int>("FirstNum"),
IntTwo = intRow.Field<int>("SecondNum"),
StrOne = strRow.Field<string>("FirstStr"),
StrTwo = strRow.Field<string>("SecondStr"),
StrThree = strRow.Field<string>("SecondStr")
};
DataTable newTable = new DataTable();
newTable.Columns.Add("IntOne");
newTable.Columns.Add("IntTwo");
newTable.Columns.Add("FirstStr");
newTable.Columns.Add("SecondStr");
newTable.Columns.Add("ThirdStr");
newTable.Columns["IntOne"].DataType = System.Type.GetType("System.String");
newTable.Columns["IntTwo"].DataType = System.Type.GetType("System.String");
newTable.Columns["FirstStr"].DataType = System.Type.GetType("System.String");
newTable.Columns["SecondStr"].DataType = System.Type.GetType("System.String");
newTable.Columns["ThirdStr"].DataType = System.Type.GetType("System.String");
foreach (var row in innerJoinQuery)
{
DataRow rowToAdd = newTable.NewRow();
rowToAdd.ItemArray[0] = row.IntOne.ToString();
rowToAdd.ItemArray[1] = row.IntTwo.ToString();
rowToAdd.ItemArray[2] = row.StrOne.ToString();
rowToAdd.ItemArray[3] = row.StrTwo.ToString();
rowToAdd.ItemArray[4] = row.StrThree.ToString();
newTable.Rows.Add(rowToAdd);
}
dataGridView3.DataSource = newTable;
}
Using DataRow.ItemArray property with individual values doesn't work - instead, create the object[] array and then set the whole thing to the .ItemArray property. See this MSDN page for additional examples.
foreach (var row in innerJoinQuery)
{
DataRow rowToAdd = newTable.NewRow();
object[] items = new object[] {
row.IntOne.ToString(),
row.IntTwo.ToString(),
row.StrOne.ToString(),
row.StrTwo.ToString(),
row.StrThree.ToString()
};
rowToAdd.ItemArray = items;
newTable.Rows.Add(rowToAdd);
}
Alternately, use the DataRow indexer directly, which works with individual columns:
rowToAdd[0] = row.IntOne.ToString();
rowToAdd[1] = row.IntTwo.ToString();
rowToAdd[2] = row.StrOne.ToString();
rowToAdd[3] = row.StrTwo.ToString();
rowToAdd[4] = row.StrThree.ToString();
Additionally, when creating columns, there is a constructor that takes the type which can save you some code. Your first two column types are mismatched.
newTable.Columns.Add("IntOne", typeof(int));
newTable.Columns.Add("FirstStr", typeof(string));
The first two values appear to be Integers:
IntOne = intRow.Field<int>("FirstNum"),
IntTwo = intRow.Field<int>("SecondNum"),
But the DataType you assign the columns to is String:
newTable.Columns["IntOne"].DataType = System.Type.GetType("System.String");
newTable.Columns["IntTwo"].DataType = System.Type.GetType("System.String");
Update those to int's and see if that resolves it:
newTable.Columns["IntOne"].DataType = System.Type.GetType("System.Int32");
newTable.Columns["IntTwo"].DataType = System.Type.GetType("System.Int32");
rowToAdd.ItemArray[0] = row.IntOne;
rowToAdd.ItemArray[1] = row.IntTwo;
You may also need to supply the DataPropertyName for the columns:
newTable.Columns["IntOne"].DataPropertyName = "FirstNum";
newTable.Columns["IntTwo"].DataPropertyName = "SecondNum";
...
And ensure that the AutoGenerateColumns value is set to false
dataGridView3.AutoGenerateColumns = false;

Categories

Resources