我创建了一个excel文件,并希望将其内容导出为png或jpeg文件。
不幸的是,图像的质量非常低。
有解决方案吗?我希望有一张非常高分辨率的照片。
谢谢
我目前的代码(来自互联网):
Excel.Range xlRange = xlWorksheet5.get_Range("A1", "K30");
xlRange.CopyPicture(Excel.XlPictureAppearance.xlScreen, Excel.XlCopyPictureFormat.xlPicture);
Excel.ChartObject chartObj;
chartObj = xlWorksheet5.ChartObjects().Add(xlRange.Left, xlRange.Top, xlRange.Width, xlRange.Height);
chartObj.Activate();
string path_image = path + "\\image.png";
Excel.Chart chart = chartObj.Chart;
chart.Paste();
chart.Export(path_image);
答案 0 :(得分:1)
试试这个。你需要在你运行它的任何线程的入口点上放置一个[STAThread]
属性。
//This first copy/paste is to convert from chart to image
chartObj.CopyPicture();
xlWorksheet5.Paste();
//This image has decent resolution
xlWorksheet5.Shapes.Item(xlWorksheet5.Shapes.Count).Copy();
//Save the image
System.Windows.Media.Imaging.BitmapEncoder enc = new System.Windows.Media.Imaging.BmpBitmapEncoder();
enc.Frames.Add(System.Windows.Media.Imaging.BitmapFrame.Create(System.Windows.Clipboard.GetImage()));
using (System.IO.MemoryStream outStream = new System.IO.MemoryStream())
{
enc.Save(outStream);
System.Drawing.Image pic = new System.Drawing.Bitmap(outStream);
pic.Save("image.png");
}