excel interop - how to run excel com add-in code - excel-interop

I have an excel add-in i created.
What i want to do is to open an excel document in c# (ASP.NET) and run some code in the add-in and then return the excel document as an html page.
Can this be done?

In the Com add-in i had to add:
public partial class ThisAddIn
{
...
protected override object RequestComAddInAutomationService()
{
if (addinUtilities == null)
{
addinUtilities = new AddinUtilities();
}
return addinUtilities;
}
...
}
You should return an object from this function, this object can later be used in interop as in the code snippet below. add any functionality you want to expose to this object.
Then use the addin as follows:
Application app = new Application();
var myAddin = app.COMAddIns;
var count = myAddin.Count;
COMAddIn addin;
for (int i = 1; i <= count ; i++) // not zero indexed
{
addin = myAddin.Item(i);
var ob = addin.Object;
var str = addin.ProgId;
if (ob != null)
{
ob.RunQuery(ws);
}
}
As you can see i have not found a good way of identifying my add-in (if anybody knows of one i would like to hear), but you can iterate over them and check the progId.
addin.Object is the object we returned from RequestComAddInAutomationService.

Related

Bulk Inserting Excel Data to Database Using EF Code First

I use asp.net mvc ef code first. I upload the file to the server all i need is inserting that excel data to code first database. What is the best way to bulk insert excel data to database? Would appreciate your consultation.
Thanks in advance.
You can user LinqToExcel to get data from the Excel file and map it in your entity class.
If you are looking for alternative methods, these are some:
OLEDB, see an example
OPEN XML 2.0
Some third party DLL like ExcelDataReader or EPPlus
Using an ORM like Entity Framework is not efficient to perform bulk operations. To bulk insert efficiently, the SqlBulkCopy class must be used.
To insert a generic list it must be converted to a DataTable:
To insert a generic list it must be converted to a DataTable:
public static DataTable ConvertToDataTable<T>(IList<T> list)
{
PropertyDescriptorCollection propertyDescriptorCollection = TypeDescriptor.GetProperties(typeof(T));
DataTable table = new DataTable();
for (int i = 0; i < propertyDescriptorCollection.Count; i++)
{
PropertyDescriptor propertyDescriptor = propertyDescriptorCollection[i];
Type propType = propertyDescriptor.PropertyType;
if (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Nullable<>))
{
table.Columns.Add(propertyDescriptor.Name, Nullable.GetUnderlyingType(propType));
}
else
{
table.Columns.Add(propertyDescriptor.Name, propType);
}
}
object[] values = new object[propertyDescriptorCollection.Count];
foreach (T listItem in list)
{
for (int i = 0; i < values.Length; i++)
{
values[i] = propertyDescriptorCollection[i].GetValue(listItem);
}
table.Rows.Add(values);
}
return table;
}
Then the SqlBulkCopy can be used. In the example the user table is bulk inserted:
DataTable dt = new DataTable();
using (SqlConnection connection = new SqlConnection(connectionString))
{
connection.Open();
using (SqlBulkCopy sqlBulkCopy = new SqlBulkCopy(connection))
{
sqlBulkCopy.ColumnMappings.Add("UserID", "UserID");
sqlBulkCopy.ColumnMappings.Add("UserName", "UserName");
sqlBulkCopy.ColumnMappings.Add("Password", "Password");
sqlBulkCopy.DestinationTableName = "User";
sqlBulkCopy.WriteToServer(dt);
}
}

I want to extract text from Ms word file in asp.net mvc?

I am new in Asp.net mvc and this is my first web based application in which i will extract text from word file and extract grammatical mistakes. but don't know how i can extract text from word file?
In ASP.NET a good way to work with Word documents is the Open XML SDK, because it doesnt need a word installation on the server.
You habe to install the Open XML SDK and add a references to DocumentFormat.OpenXml.dll and WindowsBase.dll to your project.
Then you can open your document and read/replace text like this:
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
class WordTest
{
public static void ReplaceTextInWordDoc()
{
string filepath = "C:\\Tmp\\MyWordDoc.docx";
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(filepath, true))
{
Body body = wordDoc.MainDocumentPart.Document.Body;
foreach (var paragraph in body.Elements<Paragraph>())
{
foreach (var run in paragraph.Elements<Run>())
{
foreach (var text in run.Elements<Text>())
{
if (text.Text.Contains("old"))
text.Text = text.Text.Replace("old", "new");
}
}
}
}
}
}

Breeze.js: how to transform a Breeze entity to a plain JSON object

I have an entity instance created using the metadata coming from the server:
breeze.NamingConvention.camelCase.setAsDefault();
...
var applicationType = metadataStore.getEntityType('Application');
var application = applicationType.createEntity();
None of the objects in this particular model has circular dependencies.
After the user has made some changes to the corresponding object, I need to perform some custom validation on that entity, so I need to convert that object back to its JSON simple form and send it back to a validation Controller (ASP.NET MVC4).
The question is how to transform the entity to JSON such that:
The resulting object reflects the naming conventions used in the server side.
The object contains simple properties and not knockout observables.
And does not contain any other additional property or function used internally by breeze.
I was expecting to find something like:
var json = application.toJS();
But such method does not exist. Using ko.toJS(application) also doesn't work because (1), (2) and (3) are not fulfilled.
I'm sure this should be trivially easy to do, but I just can't find anything even remotely related on the documentation.
UPDATE: You'll forgive me for the horrible hack, but because I'm in a hurry what I did that temporarily solved my problem was just exposing the unwrapEntities internal function of entityManager. I also changed the function definition a little bit (just to exclude that annoying entityAspect):
function unwrapEntities(entities, metadataStore, includeEntityAspect) {
var rawEntities = entities.map(function(e) {
var rawEntity = unwrapInstance(e);
if (includeEntityAspect !== undefined && includeEntityAspect === false) {
return rawEntity;
}
...
});
}
And because I have the entityManager available at all times in my services, I was able to extend my types definition to do something like the following:
function createApplicant(initialValues) {
var applicant = applicantType.createEntity(initialValues);
applicant.toJS = function () {
var unwrappedEntities = entityManager.unwrapEntities([applicant], entityManager.metadataStore, false);
return unwrappedEntities[0];
};
return applicant;
}
And that's precisely what I need:
var json = application.toJS();
This is a good idea! Could you please add it to the Breeze User Voice. It makes a lot of sense to expose the "unwrapping" of a Breeze entity.
Just a small side note, the unwrap "hack" that you wrote may not work in a future version of Breeze because we are currently in process of refactoring some of this code, but I will try to expose a "cleaner" version as part of the Breeze api.
This may be outdated solution but I did some hack in my typescript class to do this without help from the breeze core.
I did this hack since my breeze version and typing file did not include the unwrap method and I did not want to upgrade it just now.
Another benefit is that the result class is absolutely clean of any extra properties not included in the metadata as well as you can control if you want just the plain entity or if you want the related entities embedded in the result as well.
Just include this code in your typescript class to get this done:
public unwrapEntity(entity: breeze.Entity, includeRefs: boolean): any {
var refs = [];
return this.unwrapEntityInner(entity, refs, includeRefs);
}
private objInArray(obj: any, refs: Array<any>): boolean {
var ret = false;
for (var i = 0; i < refs.length; i++) {
if (obj === refs[i]) {
ret = true;
break;
}
}
if (!ret)
refs.push(obj);
return ret;
}
private unwrapEntityInner(entity: breeze.Entity, refs: Array<any>, includeRefs: boolean): any {
var data = {};
for (var prop in entity) {
if (entity.hasOwnProperty(prop) && ko.isObservable(entity[prop])) {
data[prop] = entity[prop]();
if (typeof data[prop] !== 'undefined' && data[prop] != null) {
if (typeof data[prop].entityAspect !== 'undefined') {
data[prop] = (includeRefs && !this.objInArray(data[prop], refs)) ? this.unwrapEntityInner(data[prop], refs, includeRefs ) : null;
}
else if ($.isArray(data[prop])) {
if (!includeRefs || this.objInArray(data[prop], refs))
data[prop] = [];
else {
var tmp = data[prop];
data[prop] = [];
for (var i = 0; i < tmp.length; i++) {
data[prop].push(this.unwrapEntityInner(tmp[i], refs, includeRefs));
}
}
}
}
}
}
return data;
}

EF4 Strip table prefixes on import

I am attempting to have table names automatically renamed to strip off a leading prefix in EF4. I know it can be done in the GUI, however, my company created the DB schema in Visio and use that to create the DB creation script in SQL. We do this often, and sometimes have a lot of tables, so using the GUI is not an ideal solution.
Is there a way to modify the properties on the .edmx file to strip off a defined prefix from the DB table so the Entity class is how we desire it?
The simplest solution we came up with was to make a new console application that did the low level work of renaming everything in the edmx file; the application can be added to the tools menu of Visual Studio (add external tool) with 'arguments' = $(ItemPath), 'initial directory' = $(ItemDir), 'prompt for arguments' = true and 'use output window' = true, then can be executed with the EDMX file selected. In our case, we needed to strip underscores and transform the names to camel case (interpreting the underscore as a word separator), in addition to stripping the prefix.
I'll omit the rest of the code (like the Transform.Standard method, error handling and the stripping of additional parameters) because it's too badly written to share and it's too late at night for refactoring :P; but it's easy enough to interpret the rest of the arguments after the first as strings to be stripped from names and so on - this main code is only about the modifications needed on the EDMX file. In case you use this as an External Tool from VS, you can specify the rest of the strings after $(ItemPath) in 'arguments'.
Please note this wasn't tested extensively, and there may be additional information in other EDMX files that we failed to account for (but I doubt it); also, I got part of the code from somewhere else on the net but failed to write down where exactly, so sorry! Credit should have been given accordingly. Also, naturally this would have been much better as an extension for VS2010, but I simply did not have the time to do it - if you do link it somewhere and I'll use that instead ;)
if (_args.Count < 1) return;
string file = _args.First();
if (!File.Exists(file))
{
wait("Could not find specified file.");
return;
}
if (Path.GetExtension(file) != ".edmx")
{
wait("This works only on EDMX files.");
return;
}
//processing:
Console.WriteLine("Creating backup: " + Path.ChangeExtension(file, ".bak"));
File.Copy(file, Path.ChangeExtension(file, ".bak"), true);
Console.WriteLine("Reading target document...");
XDocument xdoc = XDocument.Load(file);
const string CSDLNamespace = "http://schemas.microsoft.com/ado/2008/09/edm";
const string MSLNamespace = "http://schemas.microsoft.com/ado/2008/09/mapping/cs";
const string DiagramNamespace = "http://schemas.microsoft.com/ado/2008/10/edmx";
XElement csdl = xdoc.Descendants(XName.Get("Schema", CSDLNamespace)).First();
XElement msl = xdoc.Descendants(XName.Get("Mapping", MSLNamespace)).First();
XElement designerDiagram = xdoc.Descendants(XName.Get("Diagram", DiagramNamespace)).First();
//modifications for renaming everything, not just table names:
string[] CSDLpaths = new string[]
{
"EntityContainer/EntitySet.Name",
"EntityContainer/EntitySet.EntityType",
"EntityContainer/AssociationSet/End.EntitySet",
"EntityType.Name",
"EntityType/Key/PropertyRef/Name",
"EntityType/Property.Name",
"EntityType/NavigationProperty.Name",
"Association/End.Type",
"Association//PropertyRef.Name",
};
#region CSDL2
Console.WriteLine("Modifying CSDL...");
Console.WriteLine(" - modifying entity sets...");
foreach (var entitySet in csdl.Element(XName.Get("EntityContainer", CSDLNamespace)).Elements(XName.Get("EntitySet", CSDLNamespace)))
{
entitySet.Attribute("Name").Value = Transform.Standard(entitySet.Attribute("Name").Value);
entitySet.Attribute("EntityType").Value = Transform.Standard(entitySet.Attribute("EntityType").Value);
}
Console.WriteLine(" - modifying association sets...");
foreach (var associationSet in csdl.Element(XName.Get("EntityContainer", CSDLNamespace)).Elements(XName.Get("AssociationSet", CSDLNamespace)))
{
foreach (var end in associationSet.Elements(XName.Get("End", CSDLNamespace)))
{
end.Attribute("EntitySet").Value = Transform.Standard(end.Attribute("EntitySet").Value);
}
}
Console.WriteLine(" - modifying entity types...");
foreach (var entityType in csdl.Elements(XName.Get("EntityType", CSDLNamespace)))
{
entityType.Attribute("Name").Value = Transform.Standard(entityType.Attribute("Name").Value);
foreach (var key in entityType.Elements(XName.Get("Key", CSDLNamespace)))
{
foreach (var propertyRef in key.Elements(XName.Get("PropertyRef", CSDLNamespace)))
{
propertyRef.Attribute("Name").Value = Transform.Standard(propertyRef.Attribute("Name").Value);
}
}
foreach (var property in entityType.Elements(XName.Get("Property", CSDLNamespace)))
{
property.Attribute("Name").Value = Transform.Standard(property.Attribute("Name").Value);
}
foreach (var navigationProperty in entityType.Elements(XName.Get("NavigationProperty", CSDLNamespace)))
{
navigationProperty.Attribute("Name").Value = Transform.Standard(navigationProperty.Attribute("Name").Value);
}
}
Console.WriteLine(" - modifying associations...");
foreach (var association in csdl.Elements(XName.Get("Association", CSDLNamespace)))
{
foreach (var end in association.Elements(XName.Get("End", CSDLNamespace)))
{
end.Attribute("Type").Value = Transform.Standard(end.Attribute("Type").Value);
}
foreach (var propref in association.Descendants(XName.Get("PropertyRef", CSDLNamespace)))
{
//propertyrefs are contained in constraints
propref.Attribute("Name").Value = Transform.Standard(propref.Attribute("Name").Value);
}
}
#endregion
#region MSL2
Console.WriteLine("Modifying MSL...");
Console.WriteLine(" - modifying entity set mappings...");
foreach (var entitySetMapping in msl.Element(XName.Get("EntityContainerMapping", MSLNamespace)).Elements(XName.Get("EntitySetMapping", MSLNamespace)))
{
entitySetMapping.Attribute("Name").Value = Transform.Standard(entitySetMapping.Attribute("Name").Value);
foreach (var entityTypeMapping in entitySetMapping.Elements(XName.Get("EntityTypeMapping", MSLNamespace)))
{
entityTypeMapping.Attribute("TypeName").Value = Transform.Standard(entityTypeMapping.Attribute("TypeName").Value);
foreach
(var scalarProperty in
(entityTypeMapping.Element(XName.Get("MappingFragment", MSLNamespace))).Elements(XName.Get("ScalarProperty", MSLNamespace))
)
{
scalarProperty.Attribute("Name").Value = Transform.Standard(scalarProperty.Attribute("Name").Value);
}
}
}
Console.WriteLine(" - modifying association set mappings...");
foreach (var associationSetMapping in msl.Element(XName.Get("EntityContainerMapping", MSLNamespace)).Elements(XName.Get("AssociationSetMapping", MSLNamespace)))
{
foreach (var endProperty in associationSetMapping.Elements(XName.Get("EndProperty", MSLNamespace)))
{
foreach (var scalarProperty in endProperty.Elements(XName.Get("ScalarProperty", MSLNamespace)))
{
scalarProperty.Attribute("Name").Value = Transform.Standard(scalarProperty.Attribute("Name").Value);
}
}
}
#endregion
#region Designer
Console.WriteLine("Modifying designer content...");
foreach (var item in designerDiagram.Elements(XName.Get("EntityTypeShape", DiagramNamespace)))
{
item.Attribute("EntityType").Value = Transform.Standard(item.Attribute("EntityType").Value);
}
#endregion
Console.WriteLine("Writing result...");
using (XmlTextWriter writer = new XmlTextWriter(args[0], Encoding.Default))
{
writer.Formatting = Formatting.Indented;
xdoc.WriteTo(writer);
}
Edit: adding the Transform class used by the code above. Also, please note that this works for Entity Framework 4.0 - later versions might have slightly different EDMX structure (I'm not sure) so the code might need to be modified to account for that.
public class Transform
{
public static string Standard(string initial, IEnumerable<string> eliminations = null)
{
Regex re = new Regex(#"(\w+)(\W*?)$", RegexOptions.Compiled);
Regex camelSplit = new Regex(#"(?<!^)(?=[A-Z])", RegexOptions.Compiled);
return re.Replace(initial, new MatchEvaluator((Match m) =>
{
string name = m.Groups[1].Value;
var parts = name.Split('_').AsEnumerable();
if (parts.Count() == 1 && IsMixedCase(name))
{
string result = string.Concat(camelSplit.Split(name).Except(eliminations, StringComparer.CurrentCultureIgnoreCase));
return result + m.Groups[2];
}
else
{
parts = parts.Select(s => CultureInfo.CurrentCulture.TextInfo.ToTitleCase(s.ToLower()));
parts = parts.Except(eliminations, StringComparer.CurrentCultureIgnoreCase);
return string.Concat(parts) + m.Groups[2];
}
}));
}
public static bool IsMixedCase(string name)
{
int lower = 0, total = 0;
for (int i = 0; i < name.Length; i++)
{
if (char.IsLower(name, i)) lower++;
if (char.IsLetter(name, i)) total++;
}
return lower != 0 && lower != total;
}
}
Now you could write a little bit more code in the Main method that could take the arguments (except the first one which will be the name of the file) and pass them onward to the eliminations parameter; these could be strings that need to be erased from the names, in my case the prefixes. You can then specify these strings from the visual studio tools interface when invoking the tool. Or I suppose you can just hardcode them if you don't care that much about reusing the tool :)
I do not know about any build in feature for stripping table prefixes but you can open .edmx file as XML (use Open With in VS) and use simple replacing to replace prefix with nothing.

How do I activate a sharepoint 2007 feature on a specific document library

I have created a custom feature for sharepoint 2007 using visual studio 2010. When I activate the feature it of course fires on all document libraries in the site collection. can someone give me an example of how to make the feature fire on a specific document library/list instance.
First you'll have to add an EventReceiver to your feature and then in your Feature's xml add a ReceiverClass, like this:
<Feature Id="f68efad8-ea0a-42a2-9994-db3b74aa67f8"
Title="My features title"
Description="Blah blah blah"
Version="12.0.0.0"
Hidden="FALSE"
Scope="Web"
DefaultResourceFile="core"
ReceiverAssembly="MyProject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c4f34f956cd0552b"
ReceiverClass="MyProject.FeatureCode.EventHandler" <!-- This is where you set the EventReceiver -->
xmlns="http://schemas.microsoft.com/sharepoint/">
EventHandler being the EventReceiver when you're feature is activated.
My example
First of, my eventreceiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
var assembly = typeof(PermissionHandler).Assembly.ToString();
var classList = typeof(PermissionHandler).FullName;
var web = SPContext.Current.Web;
web.AllowUnsafeUpdates = true;
try
{
var list = web.Lists["MyList"];
list.EventReceivers.Add(SPEventReceiverType.ItemAdded, assembly, classList);
list.EventReceivers.Add(SPEventReceiverType.ItemUpdated, assembly, classList);
}
catch (Exception ex)
{
EventLogger.LogError("Sample feature failed to run.", this, ex);
}
}
In the above example I want to add some permissions to the elements in MyList.
As you can see I make 2 variables which is the typeof(PermissionHandler), which is a public class I've created to do the job.
I have added 5 items to the list before activating this feature, so I want the already existing items to also get the permissions I'm setting for the new items. This is how I do it:
private void updateItemPermissions(SPItemEventProperties properties)
{
DisableEventFiring();
SPListItem listItem = properties.ListItem;
SPSecurity.RunWithElevatedPrivileges(() =>
{
SPSite site = new SPSite(listItem.ParentList.ParentWeb.Site.ID);
SPWeb web = site.OpenWeb(listItem.ParentList.ParentWeb.ID);
SPList list = web.Lists[listItem.ParentList.ID];
SPListItem item = list.Items.GetItemById(properties.ListItem.ID);
item.BreakRoleInheritance(true);
if (item.RoleAssignments.Count > 0)
{
for (var i = item.RoleAssignments.Count - 1; i >= 0; i--)
item.RoleAssignments.Remove(i);
}
var group = item.Web.Site.RootWeb.Groups["Visitors"];
AddPermissions(item, web, SPRoleType.Reader, group);
});
EnableEventFiring();
}
private static void AddPermissions(SPListItem curItem, SPWeb web, SPRoleType roleType, SPPrincipal principal)
{
SPRoleDefinition roleDefinition = web.RoleDefinitions.GetByType(roleType);
SPRoleAssignment roleAssignment = new SPRoleAssignment(principal);
roleAssignment.RoleDefinitionBindings.Add(roleDefinition);
curItem.RoleAssignments.Add(roleAssignment);
curItem.Update();
}
I hope this helped you :)

Resources