Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5940345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:57:19+00:00 2026-05-22T15:57:19+00:00

The below code which uses npoi to create excel documents displays images in open

  • 0

The below code which uses npoi to create excel documents displays images in open office calc but not in excel.

If i open the doc in calc and save the document and then open the document in excel i can then see the images in excel.

Here is the code.

  public static byte[] CreateExcel(CampaignViewModel viewModel, string fileName)
    {

        byte[] output;

        using (FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(@"\Data\templates\NPOITemplate.xls"), FileMode.Open, FileAccess.ReadWrite))
        {

            var templateWorkbook = new HSSFWorkbook(fs, true);
            var sheet = templateWorkbook.GetSheet("Sheet1");
            var patriarch = sheet.CreateDrawingPatriarch();
            var leftFieldHeaders = CsvHelper.GetMatrixAllLeftFields();
            var productHeaders = CsvHelper.GetMatrixProducts(viewModel.ProductCampaigns);
            var totalCols = leftFieldHeaders.Count + productHeaders.Count;
            var colWidth = 5000;
            for (int i = 0; i < totalCols; i++)
            {
                sheet.SetColumnWidth(i, colWidth);
            }

            var imageRow = sheet.CreateRow(0);
            imageRow.Height = 2000;
            var imageCellCount = 0;
            foreach (var header in leftFieldHeaders)
            {
                imageRow.CreateCell(imageCellCount).SetCellValue("");
                imageCellCount++;
            }

            foreach (var product in viewModel.ProductCampaigns)
            {
                try
                {
                    var anchor = new HSSFClientAnchor(0, 0, 0, 0, imageCellCount, 0, imageCellCount, 0);
                    anchor.AnchorType = 2;
                    var path = HttpContext.Current.Server.MapPath(product.Product.ImageThumbUrl);
                    var picture = patriarch.CreatePicture(anchor, LoadImage(@path, templateWorkbook));

                    picture.Resize();
                    picture.LineStyle = HSSFPicture.LINESTYLE_SOLID;
                }
                catch (Exception)
                {

                }
                imageCellCount++;
            }


            using (MemoryStream ms = new MemoryStream())
            {
                templateWorkbook.Write(ms);
                output = ms.ToArray();
            }
        }
        return output;
    }

    public static int LoadImage(string path, HSSFWorkbook wb)
    {
        try
        {
            var file = new FileStream(path, FileMode.Open, FileAccess.ReadWrite);
            var buffer = new byte[file.Length];
            file.Read(buffer, 0, (int)file.Length);
            return wb.AddPicture(buffer, PictureType.JPEG);
        }
        catch (Exception)
        {
            return 0;
        }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:57:20+00:00Added an answer on May 22, 2026 at 3:57 pm

    i’ve resolved the above in a round about way. Turns out i didn’t really need to use the template and could just create the xls from scratch. This add’s a bit more meta data to the file which i suspect was the issue

    public static byte[] CreateExcel2(CampaignViewModel viewModel, ICollection<DeliveryPoint> deliveryPoints, string fileName)
    {
        FileContentResult fileContentResult;
        byte[] output;
        var matrixCampaignLines = viewModel.MatrixCampaignLines;
    
        HSSFWorkbook hssfworkbook = new HSSFWorkbook();
        DocumentSummaryInformation dsi = PropertySetFactory.CreateDocumentSummaryInformation();
        dsi.Company = "NPOI Team";
        hssfworkbook.DocumentSummaryInformation = dsi;
        ////create a entry of SummaryInformation
        SummaryInformation si = PropertySetFactory.CreateSummaryInformation();
        si.Subject = "NPOI SDK Example";
        hssfworkbook.SummaryInformation = si;
        var sheet = hssfworkbook.CreateSheet("Sheet1");
    
        var patriarch = sheet.CreateDrawingPatriarch();
        var leftFieldHeaders = (viewModel.Campaign.EnableNameOnCampaign) ? CsvHelper.GetMatrixAllLeftFields() : CsvHelper.GetMatrixAllLeftFieldsWithoutName();
        var productHeaders = CsvHelper.GetMatrixProducts(viewModel.ProductCampaigns);
        var totalCols = leftFieldHeaders.Count + productHeaders.Count;
        var colWidth = 5000;
        for (int i = 0; i < totalCols; i++)
        {
            sheet.SetColumnWidth(i, colWidth);
        }
    
        var imageRow = sheet.CreateRow(0);
        imageRow.Height = 2000;
        var imageCellCount = 0;
        foreach (var header in leftFieldHeaders)
        {
            imageRow.CreateCell(imageCellCount).SetCellValue("");
            imageCellCount++;
        }
    
        foreach (var product in viewModel.ProductCampaigns)
        {
            try
            {
                var anchor = new HSSFClientAnchor(0, 0, 0, 0, imageCellCount, 0, imageCellCount, 0);
                anchor.AnchorType = 2;
                var path = HttpContext.Current.Server.MapPath(product.Product.ImageThumbUrl);
                var picture = patriarch.CreatePicture(anchor, LoadImage(@path, hssfworkbook));
    
                picture.Resize();//Comment this line if your code crashes.
    
                picture.LineStyle = HSSFPicture.LINESTYLE_SOLID; might not
            }
            catch (Exception)
            {
    
            }
            imageCellCount++;
        }
    
        using (MemoryStream ms = new MemoryStream())
        {
            hssfworkbook.Write(ms);
            output = ms.ToArray();
        }
    
        return output;
    }
    
    public static int LoadImage(string path, HSSFWorkbook wb)
    {
        try
        {
            var file = new FileStream(path, FileMode.Open, FileAccess.Read);
            var buffer = new byte[file.Length];
            file.Read(buffer, 0, (int)file.Length);
            return wb.AddPicture(buffer, PictureType.JPEG);
        }
        catch (Exception)
        {
            return 0;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've discovered (below) that I need to use -fblocks when compiling code which uses
So far i have got the code below which works lovely when trying an
Code below does not run correctly and throws InvalidOperationExcepiton . public void Foo() {
I have a code which uses bit-fields declared as follows typedef struct my{ const
I have some fairly generic code which uses preprocessor macros to add a certain
When running the below code a type is never returned, despite there being a
In the below code snippet can i replace char * to const char *
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
In the below code sample, what does {0:X2} mean? This is from the reflection
In the below code, the ListBox gets filled with the names of the colors

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.