1Mayur
1Mayur

Reputation: 3485

Inserting Image in Excel from String

I'm generating a CSV file from the following code

public ActionResult Index()
{
    var csv = "मानक हिन्दी;some other value";
    var data = Encoding.UTF8.GetBytes(csv);
    data = Encoding.UTF8.GetPreamble().Concat(data).ToArray();
    var cd = new ContentDisposition
    {
        Inline = false,
        FileName = "newExcelSheet.csv"
    };
    Response.AddHeader("Content-Disposition", cd.ToString());
    return File(data, "text/csv");
}

Now I wish to insert Image in the top row of the excel, Please assist me in the following problem

Thanks :)

Upvotes: 0

Views: 1693

Answers (3)

Yahia
Yahia

Reputation: 70369

CSV doesn't support what you ask for AND Interop is officially NOT supported by MS in server-scenarios (like ASP.NET...).

You will need to create "real" Excel files (XLS or XLSX) - some options to create Excel files:

MS provides the free OpenXML SDK V 2.0 - see http://msdn.microsoft.com/en-us/library/bb448854%28office.14%29.aspx

This can read+write MS Office files (including Excel XLSX but not XLS!).

Another option see http://www.codeproject.com/KB/office/OpenXML.aspx

IF you need more like rendering, formulas etc. then there are different free and commercial libraries like ClosedXML, EPPlus, Aspose.Cells, SpreadsheetGear, LibXL and Flexcel.

Upvotes: 0

Icarus
Icarus

Reputation: 63956

You can't do it without using the interop assembly. You either go that route or download epplus, a free Excel .Net library that supports what you need.

Code examples on the website:

http://epplus.codeplex.com/

Upvotes: 0

Isak Savo
Isak Savo

Reputation: 35884

CSV is not a format capable of including binary data such as images. The only thing you can include in a CSV file is text.

If you need to add an image to an excel document you would have to use a proper excel file (i.e. a .xls or .xlsx file). There are various APIs that you can use to write to such files, including the Excel Object Model exposed through COM when you have Office installed.

See this question for details on how to insert images through COM.

Upvotes: 1

Related Questions