office-interop

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

蓝咒 提交于 2019-11-27 07:46:11
问题 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

microsoft.interop.excel Formatting cells

五迷三道 提交于 2019-11-27 07:39:33
问题 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. 回答1: see MSDN Microsoft.Office.Tools.Excel.NamedRange namedRange1 = this.Controls.AddNamedRange(this.Range["A1", "A5"], "namedRange1");

C# - How to add an Excel Worksheet programmatically - Office XP / 2003

馋奶兔 提交于 2019-11-27 07:17:21
I am just starting to fiddle with Excel via C# to be able to automate the creation, and addition to an Excel file. I can open the file and update its data and move through the existing worksheets. My problem is how can I add new sheets? I tried: Excel.Worksheet newWorksheet; newWorksheet = (Excel.Worksheet)excelApp.ThisWorkbook.Worksheets.Add( Type.Missing, Type.Missing, Type.Missing, Type.Missing); But I get below COM Exception and my googling has not given me any answer. Exception from HRESULT: 0x800A03EC Source is: "Interop.Excel" I am hoping someone maybe able to put me out of my misery.

How to open Outlook new mail window c#

雨燕双飞 提交于 2019-11-27 06:53:46
I'm looking for a way to open a New mail in Outlook window. I need programically fill: from, to, subject, body information, but leave this new mail window open so user can verify content / add something then send as normal Outlook msg. Found that: Process.Start(String.Format( "mailto:{0}?subject={1}&cc={2}&bcc={3}&body={4}", address, subject, cc, bcc, body)) But there is no "From" option (my users have more than one mailbox...) Any advice(s) ? Maciej I've finally resolved the issue. Here is piece of code resolving my problem (using Outlook interops) Outlook.Application oApp = new Outlook

Office documents to PDF

戏子无情 提交于 2019-11-27 06:06:39
问题 I have seen a couple threads about this, but am not getting very straight answers in my searching. I have a web application that needs to take in doc, docx, xls, xlsx files and convert them into PDF. Right now we have a process that uses the Microsoft.Office.Interop.Word library which opens up the document, prints it to a PS file, then GPL GhostScript converts the PS file into a PDF. This process works OKish, but overall there are several steps in there, and this was originally developed

How to write an image from byte[] into MS WORD using C# Windows application

混江龙づ霸主 提交于 2019-11-27 05:22:06
I have tried to write data from FileStream , StreamWriter to a word file. It's working fine when the data is text format(using StreamWriter ). But when I tried the same with Binarywriter (for writing image to word doc) it's writing incorrectly to the word document. In below code lstOrderedSpares[index].SparePicture is the byte array containing the image data. I need a solution without OPENXML. This is what I have tried so far : Here is my button click where i am initializing the data to classes and List private void btnCreateQuotation_Click(object sender, EventArgs e) { try { Quotation

WinWord.exe won't quit after calling Word.Documents.Add - Word .NET Interop

我是研究僧i 提交于 2019-11-27 04:24:15
I'm running into the classic scenario where, when creating Word COM objects in .NET (via the Microsoft.Office.Interop.Word assembly), the WinWord process won't exit even though I'm properly closing and releasing the objects . I've narrowed it down to the use of the Word.Documents.Add() method. I can work with Word in other ways without a problem (opening documents, modifying contents, etc) and WinWord.exe quits when I tell it to. It's once I use the Add() method (and only when adding a template ) that the process is left running. Here is a simple example which reproduces the problem: Dim word

Importing Excel into a DataTable Quickly

梦想的初衷 提交于 2019-11-27 03:45:32
I am trying to read an Excel file into a list of Data.DataTable, although with my current method it can take a very long time. I essentually go Worksheet by Worksheet, cell by cell, and it tends to take a very long time. Is there a quicker way of doing this? Here is my code: List<DataTable> List = new List<DataTable>(); // Counting sheets for (int count = 1; count < WB.Worksheets.Count; ++count) { // Create a new DataTable for every Worksheet DATA.DataTable DT = new DataTable(); WS = (EXCEL.Worksheet)WB.Worksheets.get_Item(count); textBox1.Text = count.ToString(); // Get range of the worksheet

Adding html text to Word using Interop

余生颓废 提交于 2019-11-27 03:38:16
问题 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? 回答1: After spending several hours the solutions is to use this excellent class http://blogs.msdn.com/jmstall/pages/sample-code-html-clipboard.aspx 回答2: This worked for me on Windows 7 and Word 2007: public static void pasteHTML(this Range range, string html) { Clipboard

How to access an already opened Excel file in C#?

为君一笑 提交于 2019-11-27 03:32:31
问题 I have an excel workbook opened via double-clicking it in windows explorer but cannot access it in code Excel.Application xlApp = (Application)Marshal.GetActiveObject("Excel.Application"); Excel.Workbooks xlBooks = xlApp.Workbooks; xlBooks.Count equals 0, why isn't it referencing my opened workbook? EDIT Here are the various scenarios and what is happening: Scenario 1: If the file is not already open Code opens workbook, I am happy. Scenario 2: If the file is initially opened from code and I