Closed XML setting pivot table report layout to Tabular? - c#

Currently have a project where I am dumping a result set of data to an excel and creating pivot table with this raw data using Closed XML
I cannot seem to get the report Layout to set and persist as tabular
Below is a sample of the code I am using to implement this
var ptSheet = wb.Worksheets.Add("PivotTable");
var pt = ptSheet.PivotTables.Add("PivotTable", ptSheet.Cell(1, 1), range);
pt.SetLayout(XLPivotLayout.Tabular);
pt.Layout = XLPivotLayout.Tabular;
'SetLayout' & 'Layout' seem to make no changes to the layout of the pivot on save of the excel. Are other settings required to be toggled?

Just adding this solution in case someone has similar issue.
I found adding the following line of code to my above example made the pivot table layout appear as tabular
pt.ClassicPivotTableLayout = true;
Not the most elegant solution but works for my use case

Related

Creating a Map Chart in VSTO PowerPoint

I'm trying to add a map chart in PowerPoint.
As of now I can't see the option to do so in XlChartType, but if I insert one manually I can then examine the inserted chart and see that its XlChartType evaluates to 140.
If I try to insert a chart with this type I get a map chart as expected. However, if I try to access its workbook it throws an exception. These two lines of code should explain what I'm doing:
var chart = _slide.Shapes.AddChart((XlChartType)140).Chart;
var workbook = (Workbook)chart.ChartData.Workbook;
I assume this is related to the fact that it's not officially supported. Is there any way to work around this problem and edit the data of the chart?
I think you are missing call to Activate method.
Checkout remark from official source:
Note You must call the Activate method before referencing this property; otherwise, an error occurs.
Your code should be:
var chart = _slide.Shapes.AddChart((XlChartType)140).Chart;
chart.ChartData.Activate(); // missing piece
var workbook = (Workbook)chart.ChartData.Workbook;

Programmatically Set Attribute Table in new Shapefile

I have a program that loads a shapefile into memory, groups some of the features based on business logic, creates a shapefile out of each group of features, and then saves the files to a cloud location for use in other applications.
The one sticking point in this process has been the attribute table. I had wanted to be able to set custom attributes for the features in the new shapefiles. With the code below I am able to update the datatable with the desired information, but I can't make it persist once I save and dispose the shapefile object.
var table = shapefile.DataTable;
var i = 0;
foreach(var branchObject in branches)
{
shapefile.AddFeature(branchObject.Feature);
var row = table.Rows[i];
row.BeginEdit();
row["BranchName"] = branchObject.Name;
row.EndEdit();
i++;
}
table.AcceptChanges();
This gives me a properly-populated DataTable in the shapefile, but when I open the shapefile in MapWindow5, the only field in the attribute table is the auto-generated Id.
I'm clearly missing some sort of "Save Changes" step that I thought was encompassed in "AcceptChanges()" or "Being/EndEdit()"...what else needs to be called on the table to make it update?
I have a feeling this was covered in one of the tutorials that I can't find since Codeplex sunsetted, but as it is Google hasn't been very helpful.
As it turns out, my DataTable and DataRows were fine. One has to explicitly tell the shapefile to update it's attribute table after changes are made.
shapefile.Filename = $"{filePathWithName}.shp";
shapefile.UpdateAttributes();
These two lines of code just before saving the shapefile, and I can now see the attribute table of my dreams in MapWindow5.
Note:
Calling
shapefile.UpdateAttributes();
without first setting the shapefile.Filename property will throw an exception.
Updating the attributes evidently requires saving to the .dbf file of the shapefile package, and it can't do that without knowing where that .dbf file is supposed to go. This called for some refactoring for me as the output shapefile didn't exist outside memory till the end of the process.

Refresh Text Box (Document) in Excel using C#

I am writing a program which will run and refresh a bunch of Excel Files and Textbox Documents within the file. Using the .RefreshAll() Method, I can refresh the linked tables within the file, as well as the text documents that are linked. However, one function I need to add is to refresh the documents without updating the tables.
After searching on here, and MDSN, I can't seem to pinpoint the thing I need. Is anyone able to point me in the right direction?
Thanks!
If you want to update links to Excel files and not the Linked Data Tables (ListObjects), then you can just iterate through each link and refresh it individually:
using Excelx = Microsoft.Office.Interop.Excel;
Excelx.Workbook wb = xlApp.ActiveWorkbook;
object links = wb.LinkSources(Excelx.XlLink.xlExcelLinks);
Array linkz = (Array)links;
for (int i = 1; i <= linkz.Length; i++)
{
wb.UpdateLink(linkz.GetValue(i).ToString(), Excelx.XlLinkType.xlLinkTypeExcelLinks);
}
The initial part seems like it could theoretically be compressed, but I've never had much luck consolidating those statements.

Load XML into grid view

Is it possible to bind the contents of an xml file with a data grid view in c#? Maybe using... LINQ? can I do that? I want to display the contents of an xml file within a grid view, edit, add or delete them there and then save them in the xml file I loaded in the first place. I'd also like to be able to search through the grid and edit multiple items. I am creating a forms application. The xml file is simple it only:
<people>
<person name='John' email='John#email.com'/>
</people>
There can be lots of records of type person.
What's the best way to approach this problem?
The easiest way recommended by MSDN here http://msdn.microsoft.com/en-us/magazine/cc163669.aspx is to load it into a data set.
There is an entire set of code in Vb.Net over at DevX here and a tutorial that might help you with binding DataGridView to XML via data sets. http://www.devx.com/dotnet/Article/28678/1954
Hope this helps. It is in Vb.Net but you will get idea.
Assuming you have loaded your xml into "doc" XDocument
var persons = from item in doc.Descendants("person")
select new
{
Name = item.Element("name").Value,
Mail = item.Element("email").Value
};
myDataboundControl.DataSource = persons;
myDataboundControl.Databind();
first you have to get the path of the XML file .Then create a new data set then bind the data set with the data grid view as your wish. you can also use SQL query to update,delete the XML file.
{
Data Set dd = new Data Set();
dd.ReadXml ("XML Path");
DataTable xm = ds.Tables[0];
}

Using a BindingSource to link a DataSet to a DataGridView, but there's no data

This is my first time working with DataSets and BindingSources, so please be gentle on me.
As part of a more complicated reporting system (I've distilled it down to a basic incarnation, but it still won't run correctly), I'm trying to pull data from a database using a DataSet problematically (that is, not set up via the designer). Here is the code I have so far:
// pull the data set
var dsReportData = new Reports2.ReportTest2();
dsReportData.BeginInit();
dsReportData.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema;
// bind tha data set
BindingSource bsReportBinding = new BindingSource();
((ISupportInitialize)bsReportBinding).BeginInit();
bsReportBinding.DataMember = dsReportData.Tables[0].TableName;
bsReportBinding.DataSource = dsReportData;
bsReportBinding.ResetBindings(true);
// test this stuff
dgvTester.DataSource = bsReportBinding;
dsReportData.EndInit();
((ISupportInitialize)bsReportBinding).EndInit();
I based this on the code I saw in a .designer.cs file after setting up binding through the designer. dgvTester is just a DataGridView with the default properties created in the designer.
The ReportTest2 dataset has just one TableAdapter in it, added via designer.
Now, if I went to Data -> Preview Data in VS and previewed the ReportTest2.payments.Fill,GetData () object, it returns data just fine, same as if I ran the query I used to crate the TableAdapter in SQL Server Management Studio.
However, running the actual code results in the DataGridView getting the column names from the query result, but not the actual data. The debugger reveals that dsReportData.payments.Rows.Count == 0 (and that, yes, dsReportData.Tables[0] is payments).
I ultimately intend to use the BindingSource to provide data to a ReportViewer, but first things first is making sure there's no problems with retrieving the data before going onto debug the report.
Hopefully I'm missing something obvious here. I hope so...
Figured it out after some trial and error. This is the part I was missing:
var TableAdapter = new Reports2.ReportTest2TableAdapters.paymentsTableAdapter();
TableAdapter.Fill(dsReportData.payments);
I didn't see it in the code I was referencing because the designer snuck it into the .cs file instead of the .designer.cs file. This made the data appear.

Categories