C# - I want to convert excel table to image using DocumentFormat.OpenXml.Spreadsheet and System.Drawing.Image not any paid import

1shiv2am 1 Reputation point
2022-01-25T08:41:24.023+00:00

i want to convert excel sheet to image in C#.Net i can read the file but can't save to image

using System;
using System.Linq;
using System.Data;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Spreadsheet;

class Program
{
    static void Main()
    {
        String fileName = @"/Users/user/Downloads/Test.xlsx";
        String path = @"/Users/user/Downloads/Test.png";
        // Open the document for editing.
        using (SpreadsheetDocument spreadsheetDocument = SpreadsheetDocument.Open(fileName, false))
        {
            WorkbookPart workbookPart = spreadsheetDocument.WorkbookPart;
            WorksheetPart worksheetPart = workbookPart.WorksheetParts.First();

            // OpenXmlReader reader = OpenXmlReader.Create(worksheetPart);
            // string text;
            // while (reader.Read())
            // {
            //     if (reader.ElementType == typeof(CellValue))
            //     {
            //         text = reader.GetText();
            //         Console.Write(text + " ");
            //     }
            // }
            Console.WriteLine();
            //Console.ReadKey();

            //worksheetPart.Worksheet.Save();
            //workbookPart.Workbook.Save();
            spreadsheetDocument.SaveAs(path);

        }
    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,110 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,606 Reputation points Microsoft Vendor
    2022-01-26T06:49:37.307+00:00

    @1shiv2am , based on my research, it is hard for me to use Openxml to convert excel to image. However, I find an alternative method to do it.

    Please install nuget-package Microsoft.Office.Interop.Excel first of all.

    Then you could try the following code to convert excel table to image.

    Code:

     public partial class Form1 : Form  
        {  
            public Form1()  
            {  
                InitializeComponent();  
            }  
      
            private void button1_Click(object sender, EventArgs e)  
            {  
                ExportRangeAsJpg();  
            }  
      
            public void ExportRangeAsJpg()  
            {  
                Microsoft.Office.Interop.Excel.Application xl=new Microsoft.Office.Interop.Excel.Application();   
      
                if (xl == null)  
                {  
                    MessageBox.Show("No Excel !!");  
                    return;  
                }  
      
                Excel.Workbook wb = xl.Workbooks.Open(AppDomain.CurrentDomain.BaseDirectory + "test1.xlsx");  
                Excel.Range r = wb.ActiveSheet.Range["A1:C5"];  
                r.CopyPicture(Excel.XlPictureAppearance.xlScreen,  
                               Excel.XlCopyPictureFormat.xlBitmap);  
      
                if (Clipboard.GetDataObject() != null)  
                {  
                    IDataObject data = Clipboard.GetDataObject();  
      
                    if (data.GetDataPresent(DataFormats.Bitmap))  
                    {  
                        Image image = (Image)data.GetData(DataFormats.Bitmap, true);  
                        this.pictureBox1.Image = image;  
                        image.Save(AppDomain.CurrentDomain.BaseDirectory+@"sample.jpg",  
                            System.Drawing.Imaging.ImageFormat.Jpeg);  
                    }  
                    else  
                    {  
                        MessageBox.Show("No image in Clipboard !!");  
                    }  
                }  
                else  
                {  
                    MessageBox.Show("Clipboard Empty !!");  
                }  
            }  
        }  
    

    Result:

    168633-sample.jpg

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.