Click here to Skip to main content
15,883,910 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can anyone tell me how to convert excel file into images using C# code and Microsoft.Office.Interop.Excel lib?

C#
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.
Posted
Updated 28-Jul-22 21:31pm
Comments
Richard MacCutchan 5-Feb-15 6:03am    
You have a choice of creating it manually by building an image from the actual data content, or by displaying the relevant portion of the worksheet and using screen capture to create the image.

1 solution

string fileNameToProcess = Station;
//Start Excel and create a new document.
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];
        //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);
        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;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900