We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Map in excel/PDF using .Net

We are looking for some means to bring maps in excel/pdf through .NET.Is it possible through Aspose.GIS? If yes how do we do that?Capture.zip (103.1 KB)

Hi, @srilak1984

The Aspose.GIS library can create map in Png, Bmp, Jpeg, Svg formats. May be you need to use the Aspose.Pdf product to insert map image in PDF.

Please consider exporting to csv instead of xsl if you need only data. Another way is to use Aspose.Cells and Aspose.GIS products together.

Please feel free to contact us if you have any other issues or product questions.

Thanks for your reply. Can you pls provide some examples on how this can be done -Using aspose.cells and aspose.gis together?

Please consider the following code. This code creates an xlsx file with two sheets. The first sheet contains the exported data. The second sheet contains the rendered map image.

I use KML file format but you could use any file format. To do this, change the driver when using the layer

    static void Main(string[] args)
    {
        // set licenses if needed
        new Aspose.Gis.License().SetLicense(@"W:\prj\aspose\gis.net\testdata\licensing\Aspose.Total.lic");
        new Aspose.Cells.License().SetLicense(@"W:\prj\aspose\gis.net\testdata\licensing\Aspose.Total.lic");

        string dataDir = @"W:\prj\aspose\temp\demo\";
        var kmlFile = @"W:\prj\aspose\gis.net\testdata\kml\Polygons.Google_Campus.kml";

        // Instantiate a Workbook object that represents Excel file.
        // When you create a new workbook, a default "Sheet1" is added to the workbook.
        using (var workbook = new Workbook())
        {
            // == export data in first sheet
            var dataSheet = workbook.Worksheets[0];

            // open layer 
            using (var layer = Drivers.Kml.OpenLayer(kmlFile))
            {
                // set cursor to '0' row
                var row = 0;

                // read each features
                foreach (var feature in layer)
                {
                    row++;
                    
                    // load all values
                    var dump = feature.GetValuesDump();

                    // write values to excel row 
                    for (int i = 0; i < dump.Length; i++)
                    {
                        var column = i+1;
                        var cell = dataSheet.Cells[row, column];
                        cell.PutValue(dump[i].ToString());
                    }
                }
            }

            // == export map in second sheet
            var mapSheet = workbook.Worksheets.Add("map image");
            var outputMapPath = Path.Combine(dataDir, "map_out.png");

            // generate map
            using (var map = new Map(800, 400))
            {
                map.Add(VectorLayer.Open(kmlFile, Drivers.Kml));
                map.Render(outputMapPath, Renderers.Png);
            }

            {
                // image position on sheet
                int row = 1;
                int column = 1;
                int width = 800;
                int height= 400;

                //Set the height of the first row
                mapSheet.Cells.SetRowHeightPixel(row, height);

                //height equals to picture height
                //Set the width of the first and second column
                mapSheet.Cells.SetColumnWidthPixel(column, width);

                //Add a picture inside cell at this row and column
                int picId = mapSheet.Pictures.Add(row, column, outputMapPath);
                Picture pic = mapSheet.Pictures[picId];

                //Set the height and width of picture
                pic.Height = height;
                pic.Width = width;
            }


            // == Save the Excel file.
            workbook.Save(Path.Combine(dataDir, "MyBook_out.xlsx"));
        }

        Console.WriteLine("finish");
        Console.WriteLine("press key to exit");
        Console.ReadKey();
    }

Best Regards.