将图像导出为CSV

时间:2016-06-01 07:21:13

标签: c# asp.net image export-to-csv

您好我正在尝试使用QR代码生成导出.csv文件。

我已完成QR码生成和CSV导出,但我想生成QR码图像绑定到我的.csv文件。

这是我做的......

 protected void lnkbtnSave_Click(object sender, EventArgs e)
        {
            int NoOfCode = int.Parse(txtNoOfCode.Text);
            DateTime ExpiryDate = DateTime.Parse(txtExpiryDate.Text.ToString());
            string csv = string.Empty;

            //Add the Header row for CSV file.
            csv += "Code" + ',' + "Expiry On" + ',' + "QR Code" + ',';
            csv += "\r\n";

            for (int i = 0; i < NoOfCode; i++)
            {
                string GeneratedCode = GenerateString(8);
                Tuple<int, string, int> tuple = Code.AddData(GeneratedCode, ExpiryDate);

                QRCodeGenerator qrGenerator = new QRCodeGenerator();
                QRCodeGenerator.QRCode qrCode = qrGenerator.CreateQrCode(GeneratedCode, QRCodeGenerator.ECCLevel.Q);
                System.Web.UI.WebControls.Image imgBarCode = new System.Web.UI.WebControls.Image();
                imgBarCode.Height = 150;
                imgBarCode.Width = 150;
                byte[] byteImage;
                using (Bitmap bitMap = qrCode.GetGraphic(20))
                {
                    using (MemoryStream ms = new MemoryStream())
                    {
                        bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                        byteImage = ms.ToArray();
                        imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage);
                    }
                    plBarCode.Controls.Add(imgBarCode);

                    csv += "\r\n";

                    **csv += GeneratedCode.ToString() + ',' + ExpiryDate.ToString() + ',' + bitMap + ',';**
                }
            }

            Response.Clear();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", "attachment;filename=SqlExport.csv");
            Response.Charset = "";
            Response.ContentType = "application/text";
            Response.Output.Write(csv);
            Response.Flush();
            Response.End();

        }

提前致谢..

1 个答案:

答案 0 :(得分:1)

问题是,您无法在csv文件中保存图像。您需要更改文件扩展名才能执行此操作[例如.xls或.xlsx]。