2

I want to export excel sheet with qr-code images in my table. The qrcode saved with its serial number and i generate it with google apis. I do it in controller that return MvcHtmlString and i use it in my view like @Html.QRCode(item.SerialNumber, 80, 0) and it works in my view well.

my Controller

  public static MvcHtmlString QRCode(this HtmlHelper htmlHelper, string data, int size = 80, int margin = 4, QRCodeErrorCorrectionLevel errorCorrectionLevel = QRCodeErrorCorrectionLevel.Low, object htmlAttributes = null)
{
    if (data == null)
        throw new ArgumentNullException("data");
    if (size < 1)
        throw new ArgumentOutOfRangeException("size", size, "Must be greater than zero.");
    if (margin < 0)
        throw new ArgumentOutOfRangeException("margin", margin, "Must be greater than or equal to zero.");
    if (!Enum.IsDefined(typeof(QRCodeErrorCorrectionLevel), errorCorrectionLevel))
        throw new InvalidEnumArgumentException("errorCorrectionLevel", (int)errorCorrectionLevel, typeof(QRCodeErrorCorrectionLevel));

    var url = string.Format("http://chart.apis.google.com/chart?cht=qr&chld={2}|{3}&chs={0}x{0}&chl={1}", size, HttpUtility.UrlEncode(data), errorCorrectionLevel.ToString()[0], margin);

    var tag = new TagBuilder("img");
    if (htmlAttributes != null)
        tag.MergeAttributes(new RouteValueDictionary(htmlAttributes));
    tag.Attributes.Add("src", url);
    tag.Attributes.Add("width", size.ToString());
    tag.Attributes.Add("height", size.ToString());

    return new MvcHtmlString(tag.ToString(TagRenderMode.SelfClosing));
}

To export excel sheet i do the following :

  1. create a grid
  2. give it the Data Source of the desired data .

the qrcode data that i give to the grid view is

data.QRcode = atm.SerialNumber;

it actually get the string of qrcode how to get its qrcode image of its code like in my view that i use

@Html.QRCode(item.SerialNumber, 80, 0)

Note : i need any solution that can get the qrcode image and other data external to print it.

Update : I reached to the source of the image then if any solution for how to display image in excel sheet according to the source of the image . detailed steps : 1- get the link of the image like with google apis

System.Web.UI.WebControls.Image image = new System.Web.UI.WebControls.Image();

            image.ImageUrl = url;
            image.Width = 80;
            image.Height = 80;
            atm2.QRcode = image;

but this column doesn't appear in the exported excel sheet

Thanks for any help .

Sameh
  • 51
  • 1
  • 11
  • have you tried base64 encoding the image? http://stackoverflow.com/questions/17874733/converting-image-to-base64 – wes Mar 05 '14 at 17:59
  • No , because i cannot convert string to qr code i use it in a controller to use it as a tag in the view and i don't know how to use it in a controller – Sameh Mar 06 '14 at 08:36
  • it isn't entirely clear from your post. What is your actual question? – wes Mar 06 '14 at 14:58
  • I can get the string url with google apis like [link](http://chart.apis.google.com/chart?cht=qr&chld=Low|4&chs=80x80&chl=25981017) but i try to hold it as an image and display it in the excel sheet then i try to change data type of qrcode to Image and wirte `image.ImageUrl = url; // image.ResolveUrl(url); image.Width = 80; image.Height = 80; atm2.QRcode = image;` but it doesn't work – Sameh Mar 06 '14 at 15:38
  • I don't know what your "atm2.QRCode" business is, but it should work if you do Image image = new Image(); image.ImageUrl = ; my guess would be that your other code/control is messed up. You can export image tags to excel just fine http://forums.asp.net/t/1056951.aspx?How+to+export+an+image+to+excel+Using+ASP+NET+2+0+and+VB+ – wes Mar 06 '14 at 19:14
  • atm2 is object and qrcode in an image type and i put all data in this object to use it in grid view but the qr image doesn't appear . i use your recommendation and put the values in html tags and export word file then the images appear well . but the other text data doesn't appear in its uni code and when i put the ` " "` the the exported word file was unable to open – Sameh Mar 07 '14 at 21:19
  • my tag : `string tags = ""; tags += " "; then why when "" is exist the file doesn't work if any solution to run its in uni-code or if i can return back to the method of grid view and put the column that hold image with html tag ?
    Bank Name Governate Area Address ATM Number Serial Number QRcode
    – Sameh Mar 07 '14 at 21:25

0 Answers0