码迷,mamicode.com
首页 > 其他好文 > 详细

com组件提取excel中的图片office07可以提取office13无法提取的问题

时间:2015-02-09 14:10:12      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

今天项目要求在公司小机房里面跑了。小机房里装的office2013 顿时我从excel中提取图片的代码就用不了了

找了一上午资料终于在stackoverflow上找到办法

贴代码:

 public void ReadPic(string path,List<int> noDataList)
        {
            try
            {
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                }

                int count = 1;
                int num = 0;
                foreach (Microsoft.Office.Interop.Excel.Worksheet sheet in workbook.Worksheets)
                {
                    
                    //object savefilename = (object)savePath;
                    ChartObjects x1Charts = (ChartObjects)sheet.ChartObjects(Type.Missing);
                    Dictionary<double, ChartObject> dic = new Dictionary<double, ChartObject>();
                    List<double> list = new List<double>();
                    for (int i = 0; i < x1Charts.Count; i++)
                    {
                        ChartObject myChart = (ChartObject)x1Charts.Item(i + 1);
                        double height = myChart.Top;
                        list.Add(height);
                        dic.Add(height, myChart);

                    }
                    double[] nums = list.ToArray();
                    com.InsertSort(nums);
                    list = new List<double>(nums);
                    for (int i = 0; i < list.Count; i++)
                    {
                        if (noDataList.Contains(count))
                        {
                            count++;
                            num++;
                            continue;
                        }
                        string name = com.GetFileName(count-num);
                        string savePath = System.IO.Path.Combine(path, name);
                        ChartObject myChart = dic[list[i]];
                        myChart.Activate();
                        Chart chart = myChart.Chart;
                        chart.Export(savePath, "jpeg", false);
                        count = count + 1;
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                 GCFinal();
            }

        }
原因是少了一句myChart.Activate();加了这一句就可以再13上跑了不加就只能在07上跑。。囧。

附原帖链接 http://stackoverflow.com/questions/21759417/how-can-i-export-an-excel-worksheet-as-image

com组件提取excel中的图片office07可以提取office13无法提取的问题

标签:

原文地址:http://blog.csdn.net/qzyf1992/article/details/43669819

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!