string fileNameToProcess = Station;
Microsoft.Office.Interop.Excel.Application oExcel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Workbook wb = null;
try
{
wb = oExcel.Workbooks.Open(fileNameToProcess.ToString(), false, false, Type.Missing, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "", false, false, 0, false, true, 0);
Microsoft.Office.Interop.Excel.Sheets sheets = wb.Worksheets as Microsoft.Office.Interop.Excel.Sheets;
for (int j = 1; j <= sheets.Count; j++)
{
Microsoft.Office.Interop.Excel.Worksheet sheet = sheets[j];
string startRange = "A1";
Microsoft.Office.Interop.Excel.Range endRange = sheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
Microsoft.Office.Interop.Excel.Range range = sheet.get_Range(startRange, endRange);
range.Rows.AutoFit();
range.Columns.AutoFit();
range.Copy();
BitmapSource image = Clipboard.GetImage();
FormatConvertedBitmap fcbitmap = new FormatConvertedBitmap(image, PixelFormats.Bgr32, null, 0);
using (var fileStream = new FileStream(AppDomain.CurrentDomain.BaseDirectory + j+".jpg", FileMode.Create))
{
PngBitmapEncoder encoder = new PngBitmapEncoder();
encoder.Interlace = PngInterlaceOption.On;
encoder.Frames.Add(BitmapFrame.Create(fcbitmap));
encoder.Save(fileStream);
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
wb.Close();
oExcel.Quit();
oExcel = null;
}