none
How can I hide the Excel when I update charts in word by C#> RRS feed

  • Question

  • private void butChart_Click(object sender, EventArgs e)
    {
    object oMissing = System.Reflection.Missing.Value;
    object oEndOfDoc = "//endofdoc"; /* /endofdoc is a predefined bookmark */
    object remarkOverTable = "chy";
    
    //Start Word and create a new document.
    Microsoft.Office.Interop.Word._Application oWord;
    Microsoft.Office.Interop.Word._Document oDoc;
    oWord = new Microsoft.Office.Interop.Word.Application();
    oWord.Visible = true;
    object Template = Application.StartupPath + @"\chy.docx";
    oDoc = oWord.Documents.Add(ref Template, ref oMissing,
    ref oMissing, ref oMissing);
    
    //得到书签
    Microsoft.Office.Interop.Word.Bookmark mark = oDoc.Bookmarks.get_Item(ref remarkOverTable);
    
    //清理excel进程类
    ExcelProcessManager pm = new ExcelProcessManager();
    pm.Lock();
    
    //重点在这句话上,强制转换成 Interop.Word.InlineShapes
    Interop.Word.InlineShapes inlineShapes = (Interop.Word.InlineShapes)mark.Range.InlineShapes;
    
    if (inlineShapes.Count > 0)
    {
    try
    {
    object Name = new object();
    
    Name = mark.Name;
    
    Object[,] obj = new Object[11, 3];
    // fill data to obj
    /* ------------------------------------
    * | | 收入 | 支出 |
    * ------------------------------------
    * | 2012/03/01 | 1023.2 | 304.23 |
    * -------------------------------------
    * | 2012/03/02 | 1123.2 | 314.23 |
    * -------------------------------------
    * | 2012/03/03 | 1223.2 | 674.23 |
    * -------------------------------------
    * | 2012/03/04 | 1043.2 | 124.23 |
    * -------------------------------------
    * | 2012/03/05 | 1083.2 | 384.23 |
    * -------------------------------------
    * | 2012/03/06 | 923.2 | 404.23 |
    * -------------------------------------
    * | 2012/03/07 | 2023.2 | 305.23 |
    * -------------------------------------
    * | 2012/03/08 | 1623.2 | 303 |
    * -------------------------------------
    * | 2012/03/09 | 1403.2 | 314.23 |
    * -------------------------------------
    * | 2012/03/10 | 1003.2 | 340 |
    * -------------------------------------
    */
    //添加横向第一列作为标题
    obj[0, 0] = string.Empty;
    obj[0, 1] = "收入";
    obj[0, 2] = "支出";
    
    DateTime dt = new DateTime(2012, 3, 1);
    Random rnd = new Random();
    int last = 1000;
    // fill data
    for (int i = 1; i < 11; i++)
    {
    obj[i, 0] = dt.ToString("yyyy/MM/dd");
    obj[i, 1] = last + rnd.Next(i, 100);
    obj[i, 2] = last - rnd.Next(i, 100);
    last = (int)obj[i, 1];
    dt = dt.AddDays(1);
    }
    
    //成功找到图表
    Interop.Word.Chart chart = inlineShapes[1].Chart;
    
    ChartUpdate(chart, Name.ToString(), obj);
    }
    catch (Exception ex)
    {
    
    }
    }
    pm.Release();
    }
    
    
     /// <summary>
    /// 更新图表数据
    /// </summary>
    /// <param name="chart"></param>
    /// <param name="bookMark"></param>
    /// <param name="dataArr"></param>
    protected void ChartUpdate(Interop.Word.Chart chart, string bookMark, Object[,] dataArr)
    {
    if (chart == null)
    {
    //logger.Warn("Word.Chart is null!");
    return;
    }
    
    if (dataArr == null)
    {
    //logger.Warn("Data is null!");
    return;
    }
    
    Object oMissing = System.Reflection.Missing.Value;
    Interop.Word.Chart chrt;
    Microsoft.Office.Interop.Excel.Workbook wb = null;
    
    Object bk = bookMark;
    try
    {
    //xlApp.ScreenUpdating = false;
    chrt = chart;
    
    if (chrt.ChartData == null)
    {
    //logger.Warn("chart.ChartData is null!");
    return;
    }
    
    chrt.ChartData.Activate();//////////////////////////////////////Look at this step.When this code run,The Excel.exe will 'jump out'
    wb = (Excel.Workbook)chrt.ChartData.Workbook;///////////////////////////
    
    if (wb == null)
    {
    //logger.Warn("chart.ChartData.Workbook is null!");
    return;
    }
    wb.Application.ScreenUpdating = false;
    wb.Application.WindowState = Microsoft.Office.Interop.Excel.XlWindowState.xlMinimized;
    
    Microsoft.Office.Interop.Excel.Worksheet wSh = (Excel.Worksheet)wb.Worksheets[1];
    
    if (wSh == null)
    {
    //logger.Warn("chart.ChartData.Workbook.Worksheets[1] is null!");
    return;
    }
    
    // save formats
    wSh.Cells.ClearContents();
    Microsoft.Office.Interop.Excel.Range Rng = wSh.get_Range("A1", "A1");
    Rng.get_Resize(dataArr.GetUpperBound(0) + 1, dataArr.GetUpperBound(1) + 1).Value2 = dataArr;
    chrt.SetSourceData("'Sheet1'!" + Rng.get_Resize(dataArr.GetUpperBound(0) + 1, dataArr.GetUpperBound(1) + 1).get_Address(Type.Missing, Type.Missing, Excel.XlReferenceStyle.xlA1, Type.Missing, Type.Missing), Type.Missing);
    chrt.Refresh();
    wb.Application.ScreenUpdating = true;
    
    wb.Close(Type.Missing, Type.Missing, Type.Missing);
    }
    catch (System.Runtime.InteropServices.COMException ex)
    {
    //logger.Warn("Update chart failed!", ex);
    //xlApp.ScreenUpdating = true;
    }
    finally
    {
    if (wb != null)
    {
    try
    {
    Marshal.ReleaseComObject(wb);
    }
    catch { }
    }
    }
    } 
    //clean excel process
    private class ExcelProcessManager
    {
    private bool _exits;
    public ExcelProcessManager() { }
    
    public void Lock()
    {
    _exits = Process.GetProcessesByName("EXCEL").Length > 0;
    }
    
    public void Release()
    {
    if (_exits)
    return;
    
    Process[] excelproc = Process.GetProcessesByName("EXCEL");
    if (excelproc.Length > 0)
    excelproc[0].Kill();
    }
    } 

    I can update Excel charts data .The problem is there are 18 charts in my word .And the Excel.exe will jump out 18 times!

    Can hide the Excel.exe ? 

    chrt.ChartData.Activate();//////////////////////////////////////Look at this step.When this code run,The Excel.exe will 'jump out'
    wb = (Excel.Workbook)chrt.ChartData.Workbook;


    赵召

    Tuesday, May 13, 2014 1:06 AM

Answers

  • <<I can update Excel charts data .The problem is there are 18 charts in my word .And the Excel.exe will jump out 18 times!

    Can hide the Excel.exe ?>>

    No, you cannot. I asked about this during beta when the new charting interface was introduced to Office. The behavior is "by design". If you want to work "silently" with charts then you should work with the (closed) file's Open XML, possibly using the Open XML SDK. If you automate Word, then you have to live with the UI behavior.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, May 13, 2014 10:16 AM
    Moderator

All replies

  • <<I can update Excel charts data .The problem is there are 18 charts in my word .And the Excel.exe will jump out 18 times!

    Can hide the Excel.exe ?>>

    No, you cannot. I asked about this during beta when the new charting interface was introduced to Office. The behavior is "by design". If you want to work "silently" with charts then you should work with the (closed) file's Open XML, possibly using the Open XML SDK. If you automate Word, then you have to live with the UI behavior.


    Cindy Meister, VSTO/Word MVP, my blog

    Tuesday, May 13, 2014 10:16 AM
    Moderator
  • Thank you Cindy Meister.

    But I could't find a way to sovle my problem by Open Xml.

    Than you very much.


    赵召

    Wednesday, May 14, 2014 2:17 AM