I've been trying for a while to run the following code with Microsoft.Excel.Interop on a new sheet which I add to an existing workbook and fill dynamically, then for display purpose I set these 3 properties:
worksheet.Application.ActiveWindow.SplitRow = 4;
worksheet.Application.ActiveWindow.SplitColumn = 1;
worksheet.Application.ActiveWindow.FreezePanes = true;
Everytime I have the same bug in the resulting workbook : all the other worksheets which were already with frozen panes, become unstructured (half of column B overlaps column C, and other interesting graphical details...).
What I have found out while checking other libraries of Excel automation (ClosedXML for example) is that others modify directly the XML code, so they don't use the ActiveWindow, but directly the worksheet itself.
Is it possible that my problem comes from the fact that I modify a property of the ActiveWindow improperly?
If yes, does somebody know whether it's possible to freeze panes without doing it through the ActiveWindow ?
If no, does somebody have an idea where it can come from ?
Thanks in advance
Roddhes
Have You activated appropriate worksheet before setting the properties? I mean something like:
Worksheet.Activate();
Worksheet.Application.ActiveWindow.SplitRow = 4;
Worksheet.Application.ActiveWindow.SplitColumn = 1;
Worksheet.Application.ActiveWindow.FreezePanes = true;
As a help, You can find some code samples for freezing the top row here (be sure to read the comments there for additional code improvements): https://stackoverflow.com/a/5060792/232530
It seems that freezing panes can be done using OpenXML as well. It's described in another SO thread: Freeze Panes in OpenXml SDK 2.0 for Excel document
Related
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.
I have one workbook with three sheets. Each sheet got 3 pages. What I want to reach is: I want to save only the first page of each sheet.
I can only count those pages with
int numberOfPages = 0;
foreach(Excel.Worksheet sheet in excelWorkbook.Sheets)
{
numberOfPages += sheet.PageSetup.Pages.Count;
}
But I cant find a way how to save these pages. Is there a way?
Here is how to copy a worksheet:
Excel.Worksheet worksheet1 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[1]);
Excel.Worksheet worksheet3 = ((Excel.Worksheet)Application.ActiveWorkbook.Worksheets[3]);
worksheet1.Copy(worksheet3);
Hope that helps.
I'd suggest using the Macro Recorder in such cases (which is available in Excel). The required VBA code can generated automatically in the background for you. Most probably you will need to correct it because an auto-generated code is not well-optimized, but at least you will have an idea what properties and methods should be used to get the job done. See Create or delete a macro for more information.
Im creating an xlsx-file using EPPlus and want to have autofilters for all headers. I set the autofilters like this:
worksheet.Cells["A3:G" + (3 + data.Count).ToString()].AutoFilter = true;
This works smooth for just one table of content. However, I have two separate tables in the same worksheet, and when trying to set autofilters for the second one, the autofilters for the first one disappears.
Any known workarounds for this, or other suggestions?
This is a very good question!
A traditional limitation of AutoFilter is that it can be applied only once on a worksheet.
Starting with Excel 2007 you can create several Tables on a worksheet...........each with filtering capabilities.
See Pieterse's Article
i need to create and download a excel sheet in c# asp.net. I used writing Range. because it is fast. but i need to format the excel sheet. According to a property(usercolor) of the user object, i need to color the row. but when writing to range how can i do that?
i am using this code to write
var startCell = (Range)sheet.Cells[2, 1];
var endCell = new object();
endCell = (Range)sheet.Cells[(usersList.Count + 2), noofcolums];
var writeRange = sheet.get_Range(startCell, endCell);
writeRange.Value2 = data;
data is a TwoDimensionalObject. it is created by user object.
As an additional comment: Do NOT use Excel in a server inevironment. It is slow and Excel might spawn error-windows at any time, causing a hang. This cannot be circumvented in a clean way - even Microsoft agrees and does not support office in a server model.
You might try epplus, a free reading / writing library for excel. It is fast , supports formatting and is way nicer to program than excel interop.
To color any Row in Excel
oRange.get_Range("A1","X1").Interior.Color = System.Drawing.ColorTranslator.ToWin32(Color.Orange);
Hope it helps
I could not find any way to use Object property to map with row color in range writing way. I need to write cell by cell. but it is very slow. So i used creating html file(html table) and convert it to excel document. it is not slow also. Thank you very much for all replies
please refer this Export HTML Table to Excel using ASP.NET
I have a requirement to create read-only merge fields in Word.
I've already tried using the Locked property which can be seen below.The description of this property states - When a field is locked you cannot update field results which sounds like a perfect fit for my problem but this doesn't seem to be working
Below is the code i use to add the merge field to MS Word:
using Word = Microsoft.Office.Interop.Word;
Word.Range currentRange = Globals.ThisAddIn.Application.Selection.Range;
Word.MailMerge merge = Globals.ThisAddIn.Application.ActiveDocument.MailMerge;
merge.Fields.Add(currentRange, selectedNode.LocalName).Locked = true;
Once I run the code above and the field is created in Word, I am still able to right click it and select "Edit Field" where I can potentially rename the field or perform other changes without getting any errors or preventions from Word.
If anyone implemented something like this before, please share your knowledge.
Here is some insight on the technologies:
The solution is targeted at MS Word Office 2010
It must be written in .NET C# 3.5
Cannot use Open Xml SDK, the fix must be performed using Office Interop
The solution must achieve the desired goals without making the whole document read-only
Thank you for the replies, however I needed to only make the merge fields read-only the rest of the document should still stay as is.
A colleague of mine found a nice way to achieve what I was looking for, just sharing it in case someone else may need this functionality:
All you need to do is create a ContentControl object and add your merge field to the content control.
Set the LockContents property to true. This property is used to determine whether a user is allowed to edit the contents of a content control.
using Word = Microsoft.Office.Interop.Word;
object missing = System.Type.Missing;
Word.Selection PosRange = Globals.ThisAddIn.Application.Selection;
Microsoft.Office.Interop.Word.ContentControl cntCtrl;
cntCtrl = PosRange.Range.ContentControls.Add(Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText, ref missing);
object fldType = Microsoft.Office.Interop.Word.WdFieldType.wdFieldMergeField;
object fldText = "Employee";
Microsoft.Office.Interop.Word.Field fld = cntCtrl.Range.Fields.Add(cntCtrl.Range, ref fldType, ref fldText);
cntCtrl.LockContents = true;
In the image below a merge field is hosted inside a content control, note users are now unable to edit the field
Using the Locked property will only prevent the value of the field from changing, which I'm assuming is not what you want.
The only way I know to prevent the field code from changing is to protect the document. The fields themselves should still be updatable.