Using Microsoft.Office.Interop.Excel;
string fileNameToProcess = @"C:\Users\infoobjects\Desktop\Test1 Single sheet.xlsx";
//Start Excel and create a new document.
xls.Application oExcel = new xls.Application();
xls.Workbook wb = null;
try
{
wb = oExcel.Workbooks.Open(fileNameToProcess.ToString(), false, false, Type.Missing, "", "", true, xls.XlPlatform.xlWindows, "", false, false, 0, false, true, 0);
//wb.RefreshAll();
xls.Sheets sheets = wb.Worksheets as xls.Sheets;
for (int i = 1; i <= sheets.Count; i++)
{
xls.Worksheet sheet = sheets[i];
//Following is used to find range with data
string startRange = "A1";
Microsoft.Office.Interop.Excel.Range endRange = sheet.Cells.SpecialCells(Microsoft.Office.Interop.Excel.XlCellType.xlCellTypeLastCell, Type.Missing);
xls.Range range = sheet.get_Range(startRange, endRange);
range.Rows.AutoFit();
range.Columns.AutoFit();
range.Copy();
System.Drawing.Image imgRange1 = Clipboard.GetImage();
imgRange1.Save(@"C:\Users\infoobjects\Desktop\" + "Test1" + i + ".Jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Console.Write("Specified range converted to image successfully. Press Enter to continue.");
sheets[i].Delete();
}
wb.Save();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
//throw;
}
finally
{
wb.Close();
oExcel.Quit();
oExcel = null;
}
Console.ReadLine();It will create a single image of the excel sheet based on the cell used in the sheet, but I want to create multiple images(If required) according to the A4 size sheet.


0 comments:
Post a Comment
Note: only a member of this blog may post a comment.