yc19901231
yc19901231
2017-09-06 23:57
采纳率: 0%
浏览 1.5k

vba转C#中读取excel中的图片,怎么用ChartObjects来设置?

With ActiveWorkbook.Worksheets("Summary")
.Range("A1:O28").CopyPicture
.Paste
For i = 1 To .Shapes.Count
Set shap = .Shapes(i)
shap.Copy
With .ChartObjects.Add(0, 0, shap.Width, shap.Height).Chart
.Paste
.Export "F:\DP_E_Planning\DP.jpg"
.Parent.Delete
End With
Next
End With
这段vba代码转成C#
ws = wb.Worksheets["Summary"]; // 打开sheet_Summary

         ws.Range["A1 : O28"].Copy();   //复制A1: O 28
         ws.Range["A1 : O28"].Insert(Microsoft.Office.Interop.Excel.XlDirection.xlDown);//黏贴
         for (int i = 0; i < ws.Shapes.Count-1; i++)
        {
             shap = ws.Shapes.Item(i+1);
             shap.Copy();

             Excel.ChartObjects xlCharts = (Excel.ChartObjects)ws.ChartObjects(Type.Missing); // 建立图表  
             Excel.ChartObject myChart = (Excel.ChartObject)xlCharts.Add(0, 0, shap.Width, shap.Height); //加入图表及设定大小  
             Excel.Chart chartPage = myChart.Chart;
             chartPage.Paste();
                            chartPage.Export(@"F:\DP_E_Planning\DP1.jpg");
             chartPage.Parent();

在执行c#的时候报错说Object引用没有被实例化。 这个问题要怎么解决?

        }
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • devmiao
    devmiao 2017-09-07 01:32
    点赞 评论
  • yc19901231
    yc19901231 2017-09-07 02:58

    @苏小喵 楼上的哥们, 你能看看你的答案在回答吗?

    点赞 评论