2016-06-01 194 views
3

嗨,我試圖用QR碼生成導出.csv文件。將圖像導出爲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

CSV是一個文本文件...爲什麼你想要在一個文本文件中存儲圖片?看到類似的問題http://stackoverflow.com/questions/2333522/how-can-i-include-image-into-csv – Alex

回答

1

的問題是,你不能CSV文件中保存圖像。您需要更改文件擴展名來執行此操作[例如.xls或.xlsx]。