PHPExcel how to generate excel file via controller of Laravel 5 - excel

I use the library PHPExcel included in Classes directory.
I do a simple test in my controller.
In my controller, i placed this code:
`include(app_path().'/Classes/PHPExcel.php');`
`include(app_path().'/Classes/PHPExcel/Writer/Excel2007.php');`
Then I instantiate the object PHPExcel :
`$workbook = new PHPExcel;`
I activate the sheet on which I work (the default form), with method ->getActiveSheet().
`$sheet = $workbook->getActiveSheet();`
I filled my first cell with the method ->setCellValue().
`$sheet->setCellValue('A1','MaitrePylos');`
Finally to create the file, I need to instantiate an object writer, specific to the type of picture I want to generate.
`$writer = new PHPExcel_Writer_Excel2007($workbook);`
I give a name to my file, and save :
`$records = 'storage/app/sampleData/fichier.xlsx';`
`$writer->save($records);`
Result: I have a generated error
Class 'App\Http\Controllers\PHPExcel_Writer_Excel2007' not found
Who can help me to better organize my code and fix my error problem?

Just use the Laravel Excel Package, behind the scenes it uses the PHPExcel class.
https://github.com/Maatwebsite/Laravel-Excel

Related

Distribution issues on Bloomberg-enabled spreadsheet

I've created a spreadsheet which optionally uses Bloomberg data pulled using the API COM 3.5 Type Library. I want to be able to distribute that spreadsheet to non-Bloomberg users, but they can't run it since they don't have the right libraries.
All blpapi-related code besides what's in the Class Module is behind if statements that should not be entered by the non-BB users. In the class module, I lazily define the session and Service so that the blpapi-specific definitions are delayed until the class initializes (see below):
Option Explicit
Private session As Object
Dim refdataservice As Object
Private Sub Class_Initialize()
' First create session.
Set session = New blpapicomLib2.session
session.QueueEvents = True
session.Start
' Then open service.
' A service provides the schemas needed to make requests.
session.OpenService "//blp/refdata"
Set refdataservice = session.GetService("//blp/refdata")
End Sub
In short - the code which appears to be causing the issues never runs. My (very limited) understanding of VBA is mostly functional, so I'm probably missing something obvious. Is this a compilation-related error? Is there a way for me to precompile the VBA so users don't experience this issue? Maybe some type of error handling method so the workbook doesn't hang?
Here was my solution for my own problem:
I had two functions with inputs that used library-specific types. I converted those to generic objects (using late binding on the session and refdataservice was not enough). Then, I unselected the bbcom library, and added a dynamic reference to the dll file in Workbook_Open(). Even if the load fails, the error is able to be caught, whereas before excel would have to be killed. This is still a problem if the user tries to use part of the workbook that uses the BB-related code, but this can be mitigated in a few different ways.

How to get caller file name in Node.js?

I have a Node class which I import in many of my projects modules Once imported into required module I create object of it and then call related methods of that object.
My question is how do I get the object caller's file name in my class file without explicitly passing so
I don't want to use module.parent.filename as it only gives the file name of parent file and not of other files. After some R&D I came to know that there is a module called stack-trace which could provide me similar functionality. But when I tried following code into my class I got the name of file itself in which the code was written and not the name of file which was calling the object of that class
const stackTrace = require('stack-trace');
var trace = stackTrace.get()
console.log(trace[0].getFileName())
The manual states, for stackTrace.get():
Returns an array of CallSite objects, where element 0 is the current call site.
You don't want the current call site, but the previous one, so you need to use trace[1].getFileName()

How do i instantiate a UIMap in Class file that i have created

I"m trying to instantiate a UIMAp (the reason i need this is currenly i'm having an error thats occuring and i think its because i need to instantiate it). I've read online about how to do it but my UI maps are named the same as my cs files that are created. and i cant seem to see if i'm actually instantiating it correctly since its just a class. I have reference in the file
which is
using Microsoft.VisualStudio.TestTools.UITest.Common.UIMap;
But i dont think i'm accessing it or am i and just dont know
I tried this code HomePage MyNewUIMap = new HomePage();
but i don't believe its correct here is my folder structure
For example my folder and file structure is
--> Home (folder)
---->HomePage.uiTest(UIfile)
------->HomePage.cs (file)
----------->HomePage.Designer.cs (file)
I normally keep a utility class that does nothing but instantiate my maps. Then, I can just call MyUtility.HomePage.objectOnHomePage when I need to interact with that object, and I don't need to instantiate each map on each test class. However, the actual method for instantiating my maps is done below:
public HomePage myHomePageMap
{
get
{
if (_homePage == null)
_homePage = new HomePage();
return _homePage;
}
}
private HomePage _homePage;
I do it this way to make sure that, if I've already instantiated the map, I don't create a duplicate instance of it.

How to auto-generate early bound properties for Entity specific (ie Local) Option Set text values?

After spending a year working with the Microsoft.Xrm.Sdk namespace, I just discovered yesterday the Entity.FormattedValues property contains the text value for Entity specific (ie Local) Option Set texts.
The reason I didn't discover it before, is there is no early bound method of getting the value. i.e. entity.new_myOptionSet is of type OptionSetValue which only contains the int value. You have to call entity.FormattedValues["new_myoptionset"] to get the string text value of the OptionSetValue.
Therefore, I'd like to get the crmsrvcutil to auto-generate a text property for local option sets. i.e. Along with Entity.new_myOptionSet being generated as it currently does, Entity.new_myOptionSetText would be generated as well.
I've looked into the Microsoft.Crm.Services.Utility.ICodeGenerationService, but that looks like it is mostly for specifying what CodeGenerationType something should be...
Is there a way supported way using CrmServiceUtil to add these properties, or am I better off writing a custom app that I can run that can generate these properties as a partial class to the auto-generated ones?
Edit - Example of the code that I would like to be generated
Currently, whenever I need to access the text value of a OptionSetValue, I use this code:
var textValue = OptionSetCache.GetText(service, entity, e => e.New_MyOptionSet);
The option set cache will use the entity.LogicalName, and the property expression to determine the name of the option set that I'm asking for. It will then query the SDK using the RetrieveAttriubteRequest, to get a list of the option set int and text values, which it then caches so it doesn't have to hit CRM again. It then looks up the int value of the New_MyOptionSet of the entity and cross references it with the cached list, to get the text value of the OptionSet.
Instead of doing all of that, I can just do this (assuming that the entity has been retrieved from the server, and not just populated client side):
var textValue = entity.FormattedValues["new_myoptionset"];
but the "new_myoptionset" is no longer early bound. I would like the early bound entity classes that gets generated to also generate an extra "Text" property for OptionSetValue properties that calls the above line, so my entity would have this added to it:
public string New_MyOptionSetText {
return this.GetFormattedAttributeValue("new_myoptionset"); // this is a protected method on the Entity class itself...
}
Could you utilize the CrmServiceUtil extension that will generate enums for your OptionSets and then add your new_myOptionSetText property to a partial class that compares the int value to the enums and returns the enum string
Again, I think specifically for this case, getting CrmSvcUtil.exe to generate the code you want is a great idea, but more generally, you can access the property name via reflection using an approach similar to the accepted answer # workarounds for nameof() operator in C#: typesafe databinding.
var textValue = entity.FormattedValues["new_myoptionset"];
// becomes
var textValue = entity.FormattedValues
[
// renamed the class from Nameof to NameOf
NameOf(Xrm.MyEntity).Property(x => x.new_MyOptionSet).ToLower()
];
The latest version of the CRM Early Bound Generator includes a Fields struct that that contains the field names. This allows accessing the FormattedValues to be as simple as this:
var textValue = entity.FormattedValues[MyEntity.Fields.new_MyOptionSet];
You could create a new property via an interface for the CrmSvcUtil, but that's a lot of work for a fairly simple call, and I don't think it justifies creating additional properties.

How to create control at runtime using app.config?

I would like to create simple objects at runtime (textbox, label, etc) and add them to a Grid in my WPF application. My problem is that I need to define these in the app.config file. I am reading in the config data by using the “ConfigurationManager.GetSection” method. Shown below is an example of the XML that defines two textboxes. The Key values are always defined as Labels so the following defines two labels called “ID:” and “Name:” and two associated TextBoxes
<HardwareControls>
<add key="ID:" value="System.Windows.Controls.TextBox"/>
<add key="Name:" value="System.Windows.Controls.TextBox"/>
</HardwareControls>
At the moment I use the following code to create a TextBox object but need to modify it so that the control types are defined by the config data and not hardcoded. Can anyone help in how I would go about doing this based on me knowing the control type as defined by a string?
TextBox tb1 = new TextBox();
tb1.Width = 100;
tb1.SetValue(Grid.ColumnProperty, 1);
tb1.SetValue(Grid.RowProperty, i);
I can also see a situation where I may want to define additional values such as the textbox width in the config file. Is there a better solution to store this in the app.config as it looks like the “GetSection” method only supports a key/value pair (I may be rog in that assumption as I haven’t read too much about this yet).
You can use Activator.CreateInstance
Example:
string typeName = "System.Windows.Controls.TextBox";
Type type = Type.GetType(typeName);
object control = Activator.CreateInstance(type); // control is your TextBox
You can use Reflection to create a type from a string name - e.g.
http://en.csharp-online.net/CSharp_FAQ:_How_create_an_instance_of_a_type_using_only_its_name
or
How can I pass an argument to a C# plug-in being loaded through Assembly.CreateInstance?

Resources