ClosedXML and C#: How to collapse rows by Default? - c#

I am trying to write code which produces excel report with pivot table. For accomplishing this task I am using ClosedXML library. The output looks like this:
The problem is that I have to get all groups of data collapsed by default, i.e. in the output I should see the following:
In other words, my output should contain collapsed rows and only summary should be displayed. How can I achieve this in code? Which method should I use?
pt.ShowRowStripes = true;
secondWorksheet.FirstRow().Hide();
secondWorksheet.TabActive = true;
secondWorksheet.CollapseRows(1);
secondWorksheet.Rows().Collapse();
pt.EnableShowDetails = false;
pt.ShowValuesRow = false;
secondWorksheet.PageSetup.ShowGridlines = true;
secondWorksheet.ShowGridLines = true;
workbook.PageOptions.ShowGridlines = true;
secondWorksheet.PivotTables.First().EnableShowDetails = false;

This is not currently supported by ClosedXML. Pivot tables are still very much work in progress.

Using ClosedXML.Signed version 0.94.2, this worked for me:
IXLPivotTable pivotTable = workbook.Worksheet("SheetContainingPivotTable").PivotTables.First();
pivotTable.ColumnLabels.ToList().ForEach(x => x.SetCollapsed(true));
pivotTable.RowLabels.ToList().ForEach(x => x.SetCollapsed(true));

Related

ClosedXML Template not setting data into excel sheet

I am trying to put data into cells using closedxml and according to their site we have to set parameters like this on the cells {{TestName}} but it won't work for me.
This is my code and I am not sure where I am going wrong.
const string outputFile = #"\\Reports\Output\Test.xlsx";
var template = new XLTemplate(#"\\Reports\Test.xlsx");
var test = new models.Test();
test.id = 1;
test.TestName = "ASDF";
test.TestScore = "303";
template.AddVariable(test) //also tried doing it like this.
template.AddVariable("TestScore", "232");
template.AddVariable("TestName", "TESTNAME");
template.SaveAs(outputFile);
Process.Start(new ProcessStartInfo(outputFile) { UseShellExecute = true });
And my excel template looks like this.
Solved it!
Just add template.Generate(); after adding the variables into the template.

Remove text between two occurrences in Word with Interop

I have a piece of text tagged with ##ABC, so it looks like this:
Some text ##ABCtext to be found##ABC some text
I need to find and remove the ##ABCtext to be found##ABC with interop. So far, I've come up wit the following code, which, however, seems to do nothing:
Microsoft.Office.Interop.Word.Range rng = document.Range();
rng.Find.ClearFormatting();
rng.Find.Replacement.ClearFormatting();
rng.Find.MatchWildcards = true;
rng.Find.Text = "##ABC(.*?)##ABC";
rng.Find.Replacement.Text = "";
rng.Find.Forward = true;
rng.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindStop;
rng.Find.Execute(Replace: Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll);
What am I missing?

How to dynamically add watermark to report in Stimulsoft

I would like to dynamically add watermark to a report that is generated in Stimulsoft. The watermark can not be hard-coded and only appear if the report was generated in TEST environment.
I have a variable that checks if the report was created in test environment:
isTestEnv
Which means that if the watermark was added to the page the old fashioned way I would use:
if(isTestEnv == true) {
Page1.Watermark.Enabled = true;
} else {
Page1.Watermark.Enabled = false;
}
But this is not the case. I have to add the watermark when generating the report. Does anyone know how to?
The text is same on all pages it simply says "TEST". But how to push that into a report is the mystery.
you can use this code and set your water mark image in your report
Stimulsoft.Base.StiLicense.loadFromFile("../license.key");
var options = new Stimulsoft.Viewer.StiViewerOptions({showTooltips:false});
var viewer = new Stimulsoft.Viewer.StiViewer(options, "StiViewer", false);
var report = new Stimulsoft.Report.StiReport({isAsyncMode: true});
report.loadFile("Backgroundimg.mrt");
var page = report.pages.getByIndex(0);
page.watermark.image = Stimulsoft.System.Drawing.Image.fromFile('test.jpg');
page.watermark.aspectRatio = true;
page.watermark.imageStretch = true;
page.watermark.imageShowBehind= true;
report.renderAsync(function () {
viewer.report = report;
viewer.renderHtml("viewerContent");
});
You can set the report page watermark to some Report variable at design time and in your code set the value for the report variable.
Something like this:
StiReport report = new StiReport();
report.Load("REPORT_TEMPLATE_PATH");
//You can check if this variable exists or not using an if condition
report.Dictionary.Variables["WATERMARK_VARIABLE_NAME"] = "YOUR_TEXT";
report.Show();//or report.ShowWithWpf();

How to allow user to edit ranges in Protected Excel using EPPlus and c#?

I am exporting protected excel sheet using EPPlus in my winform(C#) project. Now I want functionality to allow user to edit ranges in that protected excel sheet using same plugin.
It would be great if you provide Code snippet.
Thanks in advance.
var fileName = "sample.xlsx";
var fileInfo = new FileInfo(fileName);
using (var excel = new ExcelPackage(fileInfo))
{
var ws = excel.Workbook.Worksheets.Add("sheet1");
ws.Protection.IsProtected = true;
ws.ProtectedRanges.Add("editable", new ExcelAddress("C:N"));
excel.Save();
}
I know its very late to reply but may help others. I had a similar issue and was struggling to get sorting and auto-filtering in protected worksheet.
After protecting the worksheet, I have added below two settings that allow sorting and auto-filtering.
ws.Protection.IsProtected = True
ws.Protection.AllowSort = True
ws.Protection.AllowAutoFilter = True
In my case however the next requirement was to unlock some columns to allow editing, I achieved that using:
ws.Column(12).Style.Locked = False
If you have a range however you can try something like this:
For Each cell In ws.Cells("B1:C8")
cell.Style.Locked = False
Next

Microsoft.SqlServer.Management.Smo.Transfer() losing contraints

I'm trying to copy SQL database from one server to another.
Please tell me how to use Transfer() method without loosing constraints in the target database.
I've tried different parameters including
CopySchema = true; // and
CopyAllSchemas = true; //and even
CopyAllObjects = true;
and still this damn thing is losing all the constraints.
Help me, pls!
Ok...
transfer.Options.DriAll = true;
helped. But now It's leaving all triggers behind
In options some flags for select the object to copy
transfer.Options.Triggers = true;
transfer.Options.Indexes = true;
transfer.Options.Statistics = true;

Categories