今天项目要求在公司小机房里面跑了。小机房里装的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