|
分類:[C#]
エクセル内の A1:D3の画像をjpegで保存したいのですが
調べながらコードを書いたのですがうまくいかないので
教えて下さい
c#コンソールアプリ
using System;
using System.IO;
using Microsoft.Office.Interop.Excel;
namespace ExcelToImage
{
class Program
{
static void Main(string[] args)
{
Application excelApp = new Application();
excelApp.Visible = false;
Workbook excelBook = excelApp.Workbooks.Open(@"c:\data\book1.xlsx");
Worksheet excelSheet = (Worksheet)excelBook.Worksheets[1];
Range rangeToCopy = excelSheet.Range["A1:D3"];
rangeToCopy.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
var image = System.Windows.Forms.Clipboard.GetImage();
string pathToSave = @"c:\data\画像";
if (!Directory.Exists(pathToSave))
{
Directory.CreateDirectory(pathToSave);
}
string fileName = Path.Combine(pathToSave, "image.png");
image.Save(fileName);
excelBook.Close(false);
excelApp.Quit();
}
}
}
|