excel-interop

C# Proper Way to Close Multiple Excel Objects With Excel Process Destroyed

半腔热情 提交于 2020-01-02 08:18:30
问题 I have a C# console program. When it runs, it instantiates some Excel objects such as: (openExcelO). When the program is done running, I have a method to close down Excel (closeExcel) and that is supposed to properly clean up the Excel process. In task manager I can see the remnants of Excel that remain. The shut down routines were cobbled together from internet examples: private void openExcelO (string dir) { try { xlApp = new Excel.Application(); xlWorkBook = xlApp.Workbooks.Open(dir +

Excel Interop - Cancel selection

∥☆過路亽.° 提交于 2019-12-31 02:42:07
问题 I'm copying and inserting rows in an Excel sheet, like so: while (rowsToAdd > 0) { // copy the existing row insertionCell.EntireRow.Copy(Type.Missing); // location of the new row Range newRow = insertionCell.EntireRow.get_Offset(1, 0).EntireRow; // insert the new row newRow.Insert(XlInsertShiftDirection.xlShiftDown, Type.Missing); rowsToAdd--; } The problem I have is that sometimes, I'm left with a selection marquee around the row I originally copied. Is there a way I can cancel the selection

Why is killing the Excel process a bad thing?

跟風遠走 提交于 2019-12-30 23:23:42
问题 I've seen a lot of articles and questions about how to be sure that Excel actually quits when you want it to and the process doesn't stay alive. Here is a knowledge Base article describing the problem and Microsoft's recommended solution. Essentially: 'close files 'Quit Excel xlApp.quit() 'Release and collect garbage System.Runtime.InteropServices.Marshal.FinalReleaseComObject(xlApp) GC.Collect() GC.WaitForPendingFinalizers() Many people don't recommend killing the process; See How to

Copy format from one row to another using c#

十年热恋 提交于 2019-12-29 08:43:29
问题 This question is quite similar to the one asked here. But the answer given suggests copying the format along with the data. I have a excel sheet (.xlsx) that I generate using SSIS. Now I have set the formatting in first row, which I want to copy to all the rows that are already filled in the worksheet. How can I do that using C#? I am using Excel interop. 回答1: You can use PasteSpecial with xlPasteFormats . Excel.Range R1 = (Excel.Range)oSheet.Cells[11, 11]; R1.Copy(Type.Missing); Excel.Range

Accessing an open Excel Workbook in C#

蹲街弑〆低调 提交于 2019-12-28 13:40:14
问题 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

How can I add labels onto the pie pieces that are large enough to contain them, and alongside those that aren't?

ε祈祈猫儿з 提交于 2019-12-25 19:01:57
问题 I've got simple bogus data that generates this Excel Chart: This is the code: object misValue = System.Reflection.Missing.Value; //add data _xlSheet.Cells[11, 11] = ""; _xlSheet.Cells[11, 12] = "Student1"; _xlSheet.Cells[11, 13] = "Student2"; _xlSheet.Cells[11, 14] = "Student3"; _xlSheet.Cells[12, 11] = "Term1"; _xlSheet.Cells[12, 12] = "80"; _xlSheet.Cells[12, 13] = "65"; _xlSheet.Cells[12, 14] = "45"; _xlSheet.Cells[13, 11] = "Term2"; _xlSheet.Cells[13, 12] = "78"; _xlSheet.Cells[13, 13] =

How can I prevent the pasted image from overflowing its boundaries?

℡╲_俬逩灬. 提交于 2019-12-25 09:13:13
问题 I insert an image into an Excel range like so: private System.Drawing.Image _logo; public ProduceUsageRpt(..., System.Drawing.Image logo) { . . . _logo = logo; } . . . var logoRange = _xlSheet.Range[ _xlSheet.Cells[LOGO_FIRST_ROW, _grandTotalsColumn], _xlSheet.Cells[LOGO_LAST_ROW, _grandTotalsColumn]]; Clipboard.SetDataObject(_logo, true); _xlSheet.Paste(logoRange, _logo); Unfortunately, the image is too large for that range (currently row 1 to row 4, column 16). Instead of doing the polite

Excel Comment truncated during reading

无人久伴 提交于 2019-12-25 04:06:17
问题 I have a very long comment in an Excel cell. I need to able to read this comment. Microsoft.Office.Interop.Excel.Comment comment = ws.get_Range(ws.Cells[1, Constants.HIDDEN_DATA_COL], ws.Cells[1, Constants.HIDDEN_DATA_COL]).Comment; if(comment!=null) { Microsoft.Office.Interop.Excel.Characters chars = comment.Shape.TextFrame.Characters(System.Type.Missing, System.Type.Missing); string theText = chars.Text; MessageBox.Show(theText); //**truncated!** } I read that getting loading the characters

Indication in Stacked chart for MIN, MAX (stuck here for a month ..)

倖福魔咒の 提交于 2019-12-25 03:14:39
问题 Stuck in trying to adjust an arrow (sort of) inside an Excel chart. I have a column chart with series of data. I want to point out minimum, maximum, and average value from the chart. What i was planning is, to mark it by an arrow. Is it possible via C#? For the below chart, I want to indicate MIN & MAX, like the ones I have drawn. I started by getting the charts by this: Excel.Application app = new Excel.Application(); Excel.Workbook book; Excel.Worksheet sheet; Excel.ChartObject chartObj;

How to add a checkbox control to an Excel cell programatically or check or uncheck an existing checkbox

别等时光非礼了梦想. 提交于 2019-12-25 01:17:40
问题 I am using the Excel COM object in C# and want to insert a checkbox dynamically to an Excel sheet and make it checked or unchecked based on a condition. OR how can i mark as checked an existing checkbox in the Excel sheet programatically. I have looked around and I didn't find any solution. 回答1: You can always record a macro in MS Excel and it will give you a good idea of what needs to be done with Excel object in order to achieve something. For example, when recording macro for your problem,