office-interop

C#: How can I open and close an Excel workbook?

心不动则不痛 提交于 2019-11-28 13:41:50
Does anyone know how to simply open and close an Excel workbook? I don't need to read any data from the file, I just need to open and close it. (*) I'm guessing that I'll need to reference the Microsoft.Office.Interop.Excel assembly. *Reason: I've already configured pivot table information with a 3rd party library (Aspose). Now I need to read the generated pivot table. Unfortunately, the Aspose library can't generate the pivot table at runtime. It needs someone to open the file with Excel so that Excel can generate the pivot table values. after referencing Microsoft.Office.Interop.Excel also

How to create Microsoft.Office.Interop.Word.Document object from byte array, without saving it to disk?

 ̄綄美尐妖づ 提交于 2019-11-28 13:30:06
How can I create a Microsoft.Office.Interop.Word.Document object from byte array, without saving it to disk using C#? public static int GetCounterOfCharacter(byte[] wordContent) { Application objWord = new Application(); Microsoft.Office.Interop.Word.Document objDoc = new Document(); //objDoc = objWord.Documents.Open(("D:/Test.docx"); i want to create document from byte array "wordContent" return objDoc.Characters.Count; } Anders Arpi There is no straight-forward way of doing this as far as I know. The Word interop libs are not able to read from a byte stream. Unless you are working with huge

microsoft.interop.excel Formatting cells

不打扰是莪最后的温柔 提交于 2019-11-28 13:25:55
I am building a report using the microsoft.interop.excel library in C#. I have something like this: Range rangeTarget; . . . rangeTarget = worksheet.get_Range("C" + row, "N" + row); I want the range to display its values as whole numbers i.e. with no decimal places. I've tried rangeTarge.AutoFormat, but have no idea how to use it. Any Ideas ? Thanks. see MSDN Microsoft.Office.Tools.Excel.NamedRange namedRange1 = this.Controls.AddNamedRange(this.Range["A1", "A5"], "namedRange1"); namedRange1.NoteText("This is a Formatting test", missing, missing); namedRange1.Value2 = "Martha"; namedRange1.Font

different format into one single line Interop.word

吃可爱长大的小学妹 提交于 2019-11-28 12:19:37
I've been trying to figure out how to insert 2 different formats into the same paragraph using interop.word in c# like this: hello planet earth here's what I want to do Assuming you have your document defined as oDoc, the following code should get you the desired result: Word.Paragraph oPara = oDoc.Content.Paragraphs.Add(ref oMissing); oPara.Range.Text = "hello planet earth here's what I want to do"; object oStart = oPara.Range.Start + 13; object oEnd = oPara.Range.Start + 18; Word.Range rBold = oDoc.Range(ref oStart, ref oEnd); rBold.Bold = 1; I had to modify Dennis' answer a little to get it

How to implement column self-naming from its index?

落爺英雄遲暮 提交于 2019-11-28 12:05:13
问题 I wish to be able to instantiate my Cell class while naming the cell instance with such name as "A", "B", "C", etc. just like in an Excel spreadsheet. I have my Cell class like so: public class Cell { public Cell(Range nativeCell) { NativeCell = nativeCell; } public Range NativeCell { get; private set; } } And my Sheet class: public class Sheet { private IDictionary<string, Cell> _cells; public Sheet(Worksheet nativeSheet) { NativeSheet = nativeSheet; _cells = new Dictionary<string, Cell>();

Office automation (Interop) on Windows Server 2012

安稳与你 提交于 2019-11-28 11:47:23
I'm successfully using Office automation on Windows Server 2008 R2 with Office 2007 in order to convert Office documents to PDFs. The code is rather simple: public class WordConvert { /// <summary> /// Converts a word file to PDF /// </summary> /// <param name="sourceFilePath">The path of the word file to convert</param> /// <param name="targetFilePath">The path of the PDF output file</param> public static void ConvertWord(string sourceFilePath, string targetFilePath) { object objTragetFileName = targetFilePath; Word.Application wordDocument = new Word.Application(); try { OpenWord

How can fill I cells A1:A5 with a color using C#?

拜拜、爱过 提交于 2019-11-28 11:41:41
问题 I have the following code: Excel.Range chartRange; chartRange = xlWorkSheet.get_Range("A3", "R3"); I want to fill this range of cells with a color. I have already tried with: System.Drawing.Color = "yellow" but it is throwing an exception that an object reference is required. How can I fix my code to fill these cells with a color? 回答1: Try this: chartRange.Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Yellow); 回答2: The problem with your code shown above is that

Adding html text to Word using Interop

我只是一个虾纸丫 提交于 2019-11-28 10:28:06
I'm trying to add some HTML formatted text to Word using Office Interop. My code looks like this: Clipboard.SetText(notes, TextDataFormat.Html); pgCriteria.Range.Paste(); but it's throwing a Command Failed exception. Any idea? After spending several hours the solutions is to use this excellent class http://blogs.msdn.com/jmstall/pages/sample-code-html-clipboard.aspx This worked for me on Windows 7 and Word 2007: public static void pasteHTML(this Range range, string html) { Clipboard.SetData( "HTML Format", string.Format("Version:0.9\nStartHTML:80\nEndHTML:{0,8}\nStart" + "Fragment:80

Fastest way to get an Excel Range of Rows

余生颓废 提交于 2019-11-28 10:11:56
In a VSTO C# project I want to get a range of rows from a set of row indexes. The row indexes can be for example like "7,8,9,12,14". Then I want the range "7:9,12,14" rows. I now do this: Range rng1 = sheet.get_Range("A7:A9,A12,A14", Type.Missing); rng1 = rng1.EntireRow; But it's a bit inefficient due to string handling in range specification. sheet.Rows["7:9"] works but I can't give this sheet.Rows["7:9,12,14"] // Fails Try this: Sheet.Range("7:9,12:12,14:14") EDIT: Sorry if using VSTO in C# it should have been: sheet.get_Range("7:9,12:12,14:14", Type.Missing) Here is the code you are looking

Accessing an open Excel Workbook in C#

孤人 提交于 2019-11-28 09:31:29
I need to access an excel file that is already open. I thought just inspecting the .Workbooks property that it would be there but it isn't. What is the right way to get a reference to the open workbook? var app = new Microsoft.Office.Interop.Excel.Application(); // the count is 0 =( app.Workbooks.Count == 0; EDIT I can get a reference to the Excel Application via... app = (Excel.Application)Marshal.GetActiveObject("Excel.Application"); but app.Workbooks.Count is still 0 why isn't it able to get a reference to the opened workbook? Instead of instantiating a new instance, check for an existing