Hi,
I have visio file that file have ‘Excel’ , The excel have image record (Last Column values) . I tried to save that excel as image. Some data lost in the excel.
Input : Excel_Image.zip (2.6 MB)
Output : Excel_Image.png (8 KB)
Sample code :
public void VisioExcelToImage()
{
FileStream st = new FileStream(strInputFile, FileMode.Open);
Diagram vsdDiagram = new Diagram(st);
st.Close();
List<Shape> shapes = new List<Shape>();
foreach (Page diagramPage in vsdDiagram.Pages)
{
foreach (Shape OLE_Shape in diagramPage.Shapes)
{
AddShapes(OLE_Shape, shapes);
}
}
foreach (Shape shape in shapes)
{
Workbook workbook = new Workbook(new MemoryStream(shape.ForeignData.ObjectData));
ImageOrPrintOptions options = new ImageOrPrintOptions
{
OnePagePerSheet = true,
ImageType = ImageType.Png,
OnlyArea = true,
IsCellAutoFit = true,
HorizontalResolution = 200,
VerticalResolution = 200,
GridlineType = GridlineType.Dotted
};
object workSheet = workbook.Worksheets.OleSize;
Regex regexMatch = new Regex("\\(.*?\\)");
MemoryStream streamPng = null;
long id = 0;
foreach (Worksheet worksheet in workbook.Worksheets)
{
if (!worksheet.IsSelected) continue;
int nActiveSheet = workbook.Worksheets.ActiveSheetIndex;
worksheet.PageSetup.PrintArea = regexMatch.Match(workSheet.ToString()).ToString();
// Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(worksheet, options);
Bitmap bitmap = sr.ToImage(0);
bitmap.Save(strOutputPath + $"{Path.GetFileNameWithoutExtension(strInputFile)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}1.png");
}
}
}
private void AddShapes(Shape OLE_Shape, List shapes)
{
if(OLE_Shape.Type == TypeValue.Foreign)
{
if(OLE_Shape.ForeignData.ForeignType == ForeignType.Object)
{
if(OLE_Shape.ForeignData.ObjectData != null)
{
Stream Ole_stream = new MemoryStream(OLE_Shape.ForeignData.ObjectData);
// Get format of the OLE file object
FileFormatInfo info = FileFormatUtil.DetectFileFormat(Ole_stream);
if(info.LoadFormat == LoadFormat.Xlsx || info.LoadFormat == LoadFormat.Excel97To2003)
{
shapes.Add(OLE_Shape);
}
}
}
}
foreach(Shape shape in OLE_Shape.Shapes)
{
AddShapes(shape, shapes);
}
}
Waiting for your reply.
Thanks.