private void ThisAddIn_Startup(object sender, System.EventArgs e) { //Access vsto application Microsoft.Office.Interop.Excel.Application app = Globals.ThisAddIn.Application; //Access workbook Microsoft.Office.Interop.Excel.Workbook workbook = app.ActiveWorkbook; //Access worksheet Microsoft.Office.Interop.Excel.Worksheet m_sheet = workbook.Worksheets[1]; //Access vsto worksheet Microsoft.Office.Tools.Excel.Worksheet sheet = Globals.Factory.GetVstoObject(m_sheet); //Place some text in cell A1 without wrapping Microsoft.Office.Interop.Excel.Range cellA1 = sheet.Cells.get_Range("A1"); cellA1.Value = "Sample Text Unwrapped"; //Place some text in cell A5 with wrapping Microsoft.Office.Interop.Excel.Range cellA5 = sheet.Cells.get_Range("A5"); cellA5.Value = "Sample Text Wrapped"; cellA5.WrapText = true; //Save the workbook workbook.SaveAs("OutputVsto.xlsx"); //Quit the application app.Quit(); }
private void ThisAddIn_Startup(object sender, System.EventArgs e) { //<snippet1> Microsoft.Office.Interop.Excel.Worksheet nativeWorksheet = Globals.ThisAddIn.Application.ActiveSheet; if (nativeWorksheet != null) { Microsoft.Office.Tools.Excel.Worksheet vstoSheet = Globals.Factory.GetVstoObject(nativeWorksheet); } //</snippet1> //<snippet2> Microsoft.Office.Interop.Excel.Workbook nativeWorkbook = Globals.ThisAddIn.Application.ActiveWorkbook; if (nativeWorkbook != null) { Microsoft.Office.Tools.Excel.Workbook vstoWorkbook = Globals.Factory.GetVstoObject(nativeWorkbook); } //</snippet2> //<snippet3> Microsoft.Office.Interop.Excel.Worksheet sheet = Globals.ThisAddIn.Application.ActiveSheet; if (sheet.ListObjects.Count > 0) { Excel.ListObject listObject = sheet.ListObjects[1]; Microsoft.Office.Tools.Excel.ListObject vstoListObject = Globals.Factory.GetVstoObject(listObject); } //</snippet3> }
public bool LimpiaExcel() { exHoja1 = null; exApli = null; exLibro = null; GC.Collect(); return(true); }
//<snippet1> void ThisWorkbook_SheetActivate2(object Sh) { Microsoft.Office.Tools.Excel.Worksheet vstoWorksheet = null; Microsoft.Office.Interop.Excel.Worksheet interopWorksheet = Sh as Microsoft.Office.Interop.Excel.Worksheet; if (interopWorksheet != null && Globals.Factory.HasVstoObject(interopWorksheet)) { vstoWorksheet = Globals.Factory.GetVstoObject(interopWorksheet); } if (vstoWorksheet != null) { // Do something with the VSTO worksheet here. } }
private void ThisAddIn_Startup(object sender, System.EventArgs e) { //Instantiate the Application object. Excel.Application ExcelApp = Application; //Add a Workbook. Excel.Workbook objBook = ExcelApp.Workbooks.Add(System.Reflection.Missing.Value); // Access a Vsto Worksheet Microsoft.Office.Interop.Excel.Worksheet nativeWorksheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet; Microsoft.Office.Tools.Excel.Worksheet sheet = Globals.Factory.GetVstoObject(nativeWorksheet); //Add sample data for pie chart //Add headings in A1 and B1 sheet.Cells[1, 1] = "Products"; sheet.Cells[1, 2] = "Users"; //Add data from A2 till B4 sheet.Cells[2, 1] = "Aspose.Cells"; sheet.Cells[2, 2] = 10000; sheet.Cells[3, 1] = "Aspose.Slides"; sheet.Cells[3, 2] = 8000; sheet.Cells[4, 1] = "Aspose.Words"; sheet.Cells[4, 2] = 12000; //Chart reference Microsoft.Office.Tools.Excel.Chart productsChart; //Add a Pie Chart productsChart = sheet.Controls.AddChart(0, 105, 330, 200, "ProductUsers"); productsChart.ChartType = Microsoft.Office.Interop.Excel.XlChartType.xlPie; //Set chart title productsChart.HasTitle = true; productsChart.ChartTitle.Text = "Users"; //Gets the cells that define the data to be charted. Microsoft.Office.Interop.Excel.Range chartRange = sheet.get_Range("A2", "B4"); productsChart.SetSourceData(chartRange, missing); //Access the Active workbook from Vsto sheet Microsoft.Office.Interop.Excel.Workbook workbook = sheet.Application.ActiveWorkbook; //Save the copy of workbook as OutputVsto.xlsx workbook.SaveCopyAs("OutputVsto.xlsx"); }
public bool CierraExcel(bool vSave) { if (vSave) { //if (File.Exists(_Fichero)) { File.Delete(_Fichero); } exLibro.SaveAs(_Fichero + ".xls", Excel.XlFileFormat.xlWorkbookNormal, vk_missing, vk_missing, vk_missing, vk_missing, Excel.XlSaveAsAccessMode.xlExclusive, vk_missing, vk_missing, vk_missing, vk_missing, vk_missing); //csharp.net-informations.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue } exLibro.Close(vSave, vk_missing, vk_missing); exApli.Workbooks.Close(); exApli.Quit(); exHoja1 = null; exApli = null; exLibro = null; GC.Collect(); return(true); }
private void test01_click(object sender, RibbonControlEventArgs e) { //Open Excel Worksheet Microsoft.Office.Interop.Excel.Worksheet activeWorksheet = Globals.RhinoInsideAddIn.Application.ActiveSheet; try { var sphere = new Sphere(Point3d.Origin, 12); var brep = sphere.ToBrep(); var mp = new MeshingParameters(0.5); var mesh = Mesh.CreateFromBrep(brep, mp); System.Windows.Forms.MessageBox.Show($"Mesh with {mesh[0].Vertices.Count} vertices created"); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } }
/// <summary> /// 将当前活动工作簿的指定名称的工作表设置为活动 /// </summary> /// <param name="sheetName"></param> public static void ActiveSheetByName(string sheetName) { MSExcel.Workbook book = Globals.ThisAddIn.Application.ActiveWorkbook; Microsoft.Office.Interop.Excel.Worksheet sheet = null; if (book == null) { return; } Int32 i = 0; for (i = 1; i <= book.Worksheets.Count; ++i) { sheet = book.Worksheets[i] as Microsoft.Office.Interop.Excel.Worksheet; if (sheet.Name == sheetName) { sheet.Activate(); } } }
private void ThisAddIn_Startup(object sender, System.EventArgs e) { //Access vsto application Microsoft.Office.Interop.Excel.Application app = Globals.ThisAddIn.Application; //Access workbook Microsoft.Office.Interop.Excel.Workbook workbook = app.ActiveWorkbook; //Access worksheet Microsoft.Office.Interop.Excel.Worksheet m_sheet = workbook.Worksheets[1]; //Access vsto worksheet Microsoft.Office.Tools.Excel.Worksheet worksheet = Globals.Factory.GetVstoObject(m_sheet); //Access cells A1, A2, A3 , A4 Microsoft.Office.Interop.Excel.Range cellA1 = worksheet.Range["A1"]; Microsoft.Office.Interop.Excel.Range cellA2 = worksheet.Range["A2"]; Microsoft.Office.Interop.Excel.Range cellA3 = worksheet.Range["A3"]; Microsoft.Office.Interop.Excel.Range cellA4 = worksheet.Range["A4"]; //Set integer values in cells A1, A2 and A3 cellA1.Value = 10; cellA2.Value = 20; cellA3.Value = 30; //Add formula in cell A4 cellA4.Formula = "=Sum(A1:A3)"; //Set the font bold in cell A4 cellA4.Font.Bold = true; //Set the background color to Yellow in cell A4 cellA4.Interior.Color = Excel.XlRgbColor.rgbYellow; //Save the workbook workbook.SaveAs("OutputVsto.xlsx"); //Quit the application app.Quit(); }
public void fncCopiarLinea(string vFich) { Fichero = vFich; AbreExcel(true); Visible = false; exHoja1 = null; exHoja1 = new Microsoft.Office.Interop.Excel.Worksheet(); exHoja1 = (Microsoft.Office.Interop.Excel.Worksheet)exLibro.Worksheets.get_Item(1); Excel.Range oRange; oRange = exHoja1.get_Range("2:2"); oRange.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing); //oRange = exHoja1.get_Range("1:1"); //oRange.Select(); //oRange2 = exHoja1.get_Range("2:2"); //exHoja1.UsedRange.Copy(oRange2); //oRange=oRange.EntireRow; //oRange.Insert(Excel.XlInsertShiftDirection.xlShiftDown, Type.Missing); //oRange = exHoja1.get_Range("2:2"); ////exHoja1.UsedRange.Insert(Type.Missing, Type.Missing); //exHoja1.Paste(Type.Missing, Type.Missing); CierraExcel(true); LimpiaExcel(); //Selection.Copy //Rows("2:2").Select //Selection.Insert Shift:=xlDown //Application.CutCopyMode = False //rng = (Excel.Range)rng.Cells[rng.Rows.Count, 1]; //rng = rng.EntireRow; //rng.Insert(Excel.XlInsertShiftDirection.xlShiftDown, missing); }
public bool CargaExcel(DataTable dt, int vNumHoja, string Grupo) { exHoja1 = null; try { exHoja1 = new Microsoft.Office.Interop.Excel.Worksheet(); } catch { } exHoja1 = (Microsoft.Office.Interop.Excel.Worksheet)exLibro.Worksheets.get_Item(vNumHoja); Excel.Range range; decimal vver = Convert.ToDecimal(exApli.Version.ToString()); int vFila = 0; int vCol = 0; try { if (vver > 80000) { frmInformacion.vTotReg = dt.Rows.Count; Application.DoEvents(); ADODB.Recordset rs = new ADODB.Recordset(); rs = cUtil.ConvertToRecordset(dt); exHoja1.Cells.CopyFromRecordset(rs, vk_MaxRows, vk_MaxColumns); Excel.Range exR = (Excel.Range)exHoja1.Cells[1, 1]; Excel.Range row = (Excel.Range)exR.EntireRow; row.Insert(Excel.XlInsertShiftDirection.xlShiftDown, false); foreach (DataColumn dc in dt.Columns) { vFila = 1; vCol = vCol + 1; exHoja1.Cells[vFila, vCol] = dc.ColumnName; } return(true); } } catch { } try { Excel.Range oRange; foreach (DataColumn dc in dt.Columns) { vFila = 1; vCol = vCol + 1; exHoja1.Cells[vFila, vCol] = dc.ColumnName; Application.DoEvents(); } string vGrupNew = ""; frmInformacion.vTotReg = dt.Rows.Count; Application.DoEvents(); int vContRow = 0; object[] vArr = new object[dt.Columns.Count]; foreach (DataRow dr in dt.Rows) { if (frmInformacion.vCancel) { return(true); } vContRow++; if (vContRow % 50 == 0) { frmInformacion.vNReg = vContRow; Application.DoEvents(); } vFila = vFila + 1; vCol = 0; if (Grupo != "") { int vColGrupo = Convert.ToInt16(Grupo); vGrupNew = dr[vColGrupo].ToString(); if ((_DatoGrupo != vGrupNew) & (_DatoGrupo != "")) { _DatoGrupo = vGrupNew; Array.Resize(ref FilasG, vContFilasG + 2); FilasG[vContFilasG] = vFila; vContFilasG++; vFila = vFila + 1; _UltimaFilaGrupo = vFila; } if ((_DatoGrupo == "")) { _DatoGrupo = vGrupNew; } } vArr = dr.ItemArray; string vtFila = "A" + vFila.ToString() + ":" + cUtil.fncLetraCol(vArr.Length) + vFila.ToString(); range = exHoja1.get_Range(vtFila); range.set_Value(vk_missing, vArr); _UltimaFila = vFila; _UltimaCol = vCol; } if ((_UltimaFilaGrupo != _UltimaFila)) { vFila = vFila + 1; _DatoGrupo = vGrupNew; Array.Resize(ref FilasG, vContFilasG + 2); FilasG[vContFilasG] = vFila; vContFilasG++; vFila = vFila + 1; _UltimaFila = vFila; } foreach (DataColumn dc in dt.Columns) { string vTipo = dt.Columns[vCol - 1].DataType.ToString(); if (vTipo == "System.DateTime") { string vRango = vCol + ":" + vCol; oRange = exHoja1.get_Range(vRango); oRange.NumberFormat = "mm/dd/yy;@"; } Application.DoEvents(); } _Error = ""; return(true); } catch (Exception ex) { _Error = ex.Message + "-" + ex.Source.ToString(); return(false); } }
public static string GetActiveSheetName() { Microsoft.Office.Interop.Excel.Worksheet sheet = null; sheet = Globals.ThisAddIn.Application.ActiveWorkbook.ActiveSheet as Microsoft.Office.Interop.Excel.Worksheet; return(sheet.Name); }