Monday, October 17, 2016
Tuesday, December 1, 2015
Review of some products for working with MS Word documents using .NET / C#
Open XML Word Processing SDK
This is my first choice for completely free solutions because is relatively easy for basic things and it will likely always be supported by Microsoft. This is the Microsoft SDK (released as Open Source as of version 2.5) for creating and editing MS Word documents in DOCX format. It takes a bit to get used since it mirrors the internal DOCX format. The advantage is that pretty much everything you need to be done here. The disadvantage is that it may take more code than using other packages. The link above has sample code on how to do many common tasks. Here is a good place to start to familiarize yourself with the file formats, what resources are available, etc. To understand the DOCX file format, check this out. The DOCX format is much simpler than the XLSX format. I recommend giving it a try. The learning curve is pretty small for the DOCX file format and there is a lot of examples available. In the end it isn't so different than NPOI (see section below) for DOCX. Here is a list of how to do's.
It is unofficially distributed on NuGet as DocumentFormat.OpenXml, but it is unofficially distributed by someone other than Microsoft (even though it shows the author as Microsoft). If you want the official binaries they can be downloaded here. If you want the source code you can get it on GitHub. This video shows how to build the source code if you want to go that route. If you want to use 2.6 it has been released on NuGet as OpenXMLSDK-MOT. This is the same as official binaries and source code in GitHub (from what I can tell). All options install an assembly called DocumentFormat.OpenXml and if done through NuGet it is added a reference to your project.
If you want to validate that the document you create is valid, check out this code example.
Here is an example of how to do a search and replace in a DOCX file.
Here is the link to the Open Xml Developer website.
There are also tons of example on the Open Xml Power Tools project on GitHub and from NuGet.
Open-Xml-Power-Tools - DocumentAssembler
This is an Open Source module in Open Xml Power Tools that allows you to create templates using .DOCX files as templates, a xml data source, and generating a well formatted DOCX file with the data merged in to the template. The template allows such things as tables, conditions, etc using XPATH-like syntax. Here is a video on how it works, but not a step by step tutorial. Here is a getting started video / tutorial that walks you through using the product. It is important to watch the ending where he talks using <# #> instead of Content Controls in Word as the placeholders. It works similar to most reporting tools, but using MS Word as report definition (template) file and the output being a MS Word document as well. You can download the entire Open-Xml-Power-Tools suite that includes DocumentAssembler from here. It is not meant to be used to create DOCX documents from scratch. It always uses a template file to generate new DOCX files. However, Open-Xml-Power-Tools suite does have the ability to help work with DOCX files. NOTE: It is built on top of (requires it) the Open XML Word Processing SDK (above).
DOCX
If you really don't want to learn anything about the DOCX format and want a more inuitive way to interact with the DOCX files then this may be a good option if you are okay with alpha software. A simple Open Source project available from codeplex.com or NuGet. It let's you interact with the Word document in an intuitive manor without understanding how Word documents internally work. The examples on his blog are good and good examples in source code. It is easy to use. It is still alpha, but has been around since 2009. It has growing popularity.
Free Spire.Doc
This is a free version with limitations for a professional package called Spire.Doc. It is quite powerful and also does conversions to PDF and many other formats. It does mail merges, etc. Is is nice for small projects that are below the limitations. The API is well thought out and works nicely.
NPOI
This is the .NET implementation of the popular POI Java Project. It is Open Source and totally free and also does MS Excel. The API is a bit low level at time, but works well. I recommend downloaded from github to work with examples. Also, you can install binaries via NuGet. The documentation and examples for XDOC are not really there yet though. Look for XWPF if you want to use DOCX file format. The problem is that it is not the exact same API as the POI Java Project, but it is similar. It is also not as mature and is thus missing some features. It is a bit more abstract that just using the Microsoft packages, but still quite a bit of DOCX format knowledge is needed. In general I found the experience a bit frustrating because of how the project is organized and the poor documentation and very few DOCX examples.
Monday, May 13, 2013
Microsoft Business Intelligence (BI) Offering
I have summarized (not pretty diagrams or anything) the basics below for easy consumption. I also have a PowerPoint slide with nice architctural diagrams, etc. Click here to view the PowerPoint slides.
Below is an overview of what they offer:
SharePoint
- PerformancePoint
- Excel Services
- Power view
- PowerPivot for SharePoint
- SQL Server
- Reporting Services
- Analysis Services
- Data Mining
- Master Data Services
- Data Quality Services
- Integration Services
- Data Warehousing
- SQL Server Data Tools (formerly BI Development Studio)
- Excel
- Data Explorer for Excel
- PowerPivot for Excel
- Power View for Excel
- Data Mining for Excel
Below are some more details on the specific products
Power View
- For both SharePoint and Excel
- Part of Microsoft SQL Server Reporting Services 2012
- It provides an ad-hoc visualization experience in SQL Server Reporting Services
- Power View provides intuitive data visualization of PowerPivot models and SQL Server Analysis Services (SSAS) tabular mode databases
- Users can edit views or create new ones
- Very interactive
- Very easy to use
- Encourages Data Exploration and Visualization
- Also available in Excel as an Add-in
- Click to filter technology
- Automatically creates data model
PowerPivot
- For both SharePoint and Excel
- PowerPivot is an add-in that lets end users gather, store, model, and analyze large amounts of data in Excel
- Use Excel to create a PowerPivot
- Save to SharePoint for others to access.
- Viewable in the SharePoint Gallery
- In-Memory technologies allows working with Millions of rows of data
- Mashup data from different data sources
- Create Pivot tables, Charts, and PKIs on millions of rows of data.
PerformancePoint
- Aimed at Advanced PowerUsers, but more likely will be used by developers
- Integrated into SharePoint
- Provides designer as well
- Left-click Drill down into data
- Types of visualizations
- Dashboard
- ScorecardReports
- Highly Interactive
- Limited customization of the look and feel of report or dashboard
- Uses OLAP data sources
- Special designer accessible via SharePoint to design everything
Excel Services
- Excel Services on the other hand is a very power user-friendly technology. Those familiar with Excel and PivotTables should take very little time to be able to build very sophisticated reports. SharePoint 2010 renders Excel reports and dashboards as web pages which makes this technology very easy to deploy.
- Good choice for self-service BI scenarios
- Show an Excel sheet or workbook on a SharePoint site in a web part.
- Can show Power View Excel sheets on the web and keep the high level of interactivity.
Data Mining add-in for Excel
- Uses Analysis Services on the backend
- Excel User interface
- Supports:
- Classify
- Estimate
- Cluster
- Associate
- Forecast
- Other advanced algorithms
Data Explorer add-in for Excel
- Still in preview status
- Explore data
- Brings new data sources to Excel import options.
- Like SSIS, but for Excel
- ETL tool for Excel
- Extract: DB, Excel, Text file, web, OData, SharePoint lists, Active Directory, Multiple data source support
- Transform: Cleanse, apply business rules, aggregate, merge, append, mashups, Navigate through data (even joined data)
- Load: Load data into Excel once massage it to what we want
Master Data add-in for Excel
- Connects to MDS data sources / models
- Allows end users to create and maintain data in MDS using Excel
For lots of resource on the above click here.
Friday, October 19, 2012
Troubleshooting Microsoft Excel cannot access the file
Here’s the scenario. I am running SQL Server (on Windows Server 2008 R2) and I have a created a Job that has among other steps a CmdExec step. This CmdExec step calls a C# console application that I wrote. My C# console application calls the Microsoft.Office.Interop.Excel.Application. The code throws the following exception / message.
Microsoft Excel cannot access the file 'c:\myDir\MyFile.xlsx'. There are several possible reasons:
• The file name or path does not exist.
• The file is being used by another program.
• The workbook you are trying to save has the same name as a currently open workbook.
The last line of the code below is the line that throws the exception
Microsoft.Office.Interop.Excel.Application xlApp = new Microsoft.Office.Interop.Excel.Application();
string filename = @"c:\myDir\MyFile.xlsx"
Console.WriteLine("Exists: " + File.Exists(filename).ToString());
var wb = xlApp.Workbooks.Open(filename);
The message suggests that it can’t find my file that I am trying to open, so I made sure none of these were the issue. However, the actual problem is not anything to do with my file. Instead I just had to create a directory called Desktop at the following path:
C:\Windows\SysWOW64\config\systemprofile\Desktop
It is worth noting I am running Windows Server 2008 R2 and is a 64-bit operating system. If you are running a 32-bit OS you would need to change make sure there is a desktop directory here:
C:\Windows\System32\config\systemprofile\Desktop
I noticed that I needed to be admin to follow the path above, but it didn’t seem to matter from a code perspective. However, if this solution doesn’t work, I would suggest playing with the permissions to that Desktop directory just to make sure since that is what I did to start with and then un-did my changes to the permissions. Also, I am running Windows 7 64-bit on my laptop where I am developing and I did not have any issues if when that directory did NOT exist. I don’t know why this seems to be particular to the server environment.
Thanks to this discussion that gave me this solution.
Wednesday, May 30, 2012
How to add another person’s mailbox to Outlook 2007
If you regularly need to access another person’s Exchange mailbox you can do so in Outlook quite easily by going to the File menu | Open | Other User’s Folder… However, this becomes quite tiresome because it does not show in the list of Mail Folders in Outlook.
Adding Mailbox to Outlook 2007
If you would like for the mailbox to show up in the Mail Folder list then do the following.
- Open Outlook 2007
- Open Tools menu | Account Settings…| Data Files
- Select your Exchange mailbox.
- Click the Settings… button.
- Click the Advanced tab.
- Click the Add… button
- Type in the name of the mailbox in the textbox
- Click OK to close out open windows and that should do it.
Owner Permissions
The above should work, but the owner of the mailbox may not have granted you the rights to the mailbox. To grant you permissions the owner of the mailbox just need to:
- Open Outlook 2007
- Right-click on the mailbox you need access to.
- Select the Properties for “<name of mailbox here>”
- Click the Permissions tab.
- Click the Add… button
- Locate your user in the directory add them. Change the permissions as desired.
- Click OK to close out open windows and that should do it.
NOTE: The permissions may need to be set on each folder including Deleted Items or any folders the owner has created.
Replying or Sending on Behalf of the owner
In order for you to reply to any messages that are received in this mailbox or compose new emails from this mailbox you need to be added as a delegate for that person. Please note, if you create a new message or click the reply button in Outlook the Message window will act as you would normally see. However, when you actually click send the Exchange server will send back a message saying you don’t have the rights. Specifically, it will say something like
“You are not allowed to send this message because you are trying to send on behalf of another sender without permission to do so. Please verify that you are sending on behalf of the correct sender, or ask your system administrator to help you get the required permissions.”
To fix this, the owner needs to add you as a delegate. To do that, the owner needs to:
- Open Outlook 2007
- Go to Tools menu | Options… | Delegates tab
- Click the Add… button
- Find you in the list.
- The owner will now be presented with a bunch of options. The default settings are to allow access to the Calendar. They can leave it, or select the appropriate level of access such as None (if you don’t need access to the calendar) for the Calendar. In order to get access to the mailbox (Inbox), they need to change the Inbox drop down list to Editor (can read, create, and modify items).
- Click OK buttons.
More Info
For more instructions and background info on this topic, just type additional mailbox into the help box in Outlook 2007. Open the one called Manage another person's mail and calendar items. The help document is quite good and cover everything in depth.
Wednesday, September 21, 2011
Reading a MS Word 2007 Document in .docx format using C#
If you want need to read (or write) from a MS Word 2007 Document that has been saved in the Open XML format (.docx) then you can use the Open XML SDK 2.0 for Microsoft Office to do just. The first thing you will need to do is download and install the SDK. In particular, you must download and install the OpenXMLSDKv2.msi. In addition, you can download the OpenXMLSDKTool.msi if you want. It has some VERY nice features like generating code from an existing .docx file.
Now that you have the files you need, open Visual Studio (2008 or 2010 works fine), open the project you want to use, and add a reference to the DocumentFormat.OpenXml (I had to browse to it in the adding references windows by going to C:\Program Files (x86)\Open XML SDK\V2.0\lib) and WindowsBase (mine was located in the list of .NET tab when adding references). Please note, this code does not require MS Word be installed and is safe to run on the server such as with ASP.NET.
Now that you have the api, the rest is just working with the document. To get a better understand on how to work with the parts (structure) of the Word Document, click here. For a list of “How do I…” code samples, click here.
Here is example code on how to get the body of the document.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;
…
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(Filename, false))
{
var body = wordDocument.MainDocumentPart.Document.Body;
}
Here is an example of a more complex line of code that can be used to navigate the structure of the document using LINQ. In this case the document has a table in it and we are getting the first row and first cell of that row and the second element.
wordDocument.MainDocumentPart.Document.Body.ChildElements.First<Table>().ChildElements.First<TableRow>().ChildElements.First<TableCell>().ElementAt(2).InnerText
I hope this gives you an idea of how to get started. There are lots of good links, examples, etc here.