@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:
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.