• MIME生成EXCEL 包括图片的写入 支持EXCEL2003 2007 草稿


    代码
    using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.IO;
    using System.Text;

    namespace Web
    {
        
    public partial class excel : System.Web.UI.Page
        {
            
    protected void Page_Load(object sender, EventArgs e)
            {

            }

            
    /// <summary>
            
    /// Get Image's Code In Base64 Code Format.
            
    /// </summary>
            
    /// <param name="path"></param>
            
    /// <param name="strCode"></param>
            
    /// <returns></returns>
            private static string GetImageCodeInBase64(string path)
            {
                
    string strImgCodeInBase64 = "";
                FileStream file;
                
    byte[] base64buffer;          //开辟缓冲区
                
    // 打开图片文件,利用该图片构造一个文件流
                file = new FileStream(path, FileMode.Open);

                base64buffer 
    = new byte[(int)file.Length];
                BinaryReader br 
    = new BinaryReader(file, Encoding.UTF8);
                br.Read(base64buffer, 
    0, (int)file.Length);

                strImgCodeInBase64 
    = Convert.ToBase64String(base64buffer);
                
    return strImgCodeInBase64;
            }

            
    protected void Button1_Click(object sender, EventArgs e)
            {
                StringBuilder sb 
    = new StringBuilder();
                sb.Append(
    "MIME-Version: 1.0\r\n");
                sb.Append(
    "X-Document-Type: Worksheet\r\n");
                sb.Append(
    "Content-Type: multipart/related; boundary=\"----=mtrSystem\"\r\n\r\n\r\n");
                sb.Append(
    "------=mtrSystem\r\n");
                sb.Append(
    "Content-Type: text/html; charset=\"utf-8\"\r\n");
                sb.Append(
    "<html xmlns:o=3D\"urn:schemas-microsoft-com:office:office\"\r\n");
                sb.Append(
    "xmlns:x=3D\"urn:schemas-microsoft-com:office:excel\">\r\n\r\n\r\n");

                sb.Append(
    "<head></head><body>\r\n");
                sb.Append(
    "<table border=\"1\"  width=3D\"600\"><tr height=3D\"51\"><td><img src=\"cid:baiduimg\"></td><td><img src=\"cid:cnblogsimg\" width=3D\"200\"></td><td>dfasdfasdf</td></tr><tr height=3D\"51\"><td></td><td></td></table>\r\n");
                sb.Append(
    "</body></html>\r\n\r\n");

                sb.Append(
    "------=mtrSystem\r\n");
                sb.Append(
    "Content-ID: baiduimg\r\n");
                sb.Append(
    "Content-Transfer-Encoding: base64\r\n");
                sb.Append(
    "Content-Type: image/jpeg\r\n\r\n");
                sb.Append(GetImageCodeInBase64(Server.MapPath(
    "./logo-yy.gif")) + "\r\n\r\n\r\n\r\n");

                sb.Append(
    "------=mtrSystem\r\n");
                sb.Append(
    "Content-ID: cnblogsimg\r\n");
                sb.Append(
    "Content-Transfer-Encoding: base64\r\n");
                sb.Append(
    "Content-Type: image/png\r\n\r\n");
                sb.Append(GetImageCodeInBase64(Server.MapPath(
    "./MIME_XLS.png")) + "\r\n\r\n\r\n");

                sb.Append(
    "------=mtrSystem--\r\n");

                Response.ContentType 
    = "application/vnd.ms-excel";
                Response.Charset 
    = "utf-8";
                Response.ContentEncoding 
    = System.Text.Encoding.UTF8;
                
    string name = HttpUtility.UrlEncode(DateTime.Now.ToString("yyyyMMddhhmmssss"), System.Text.Encoding.UTF8);
                Response.AppendHeader(
    "Content-Disposition""inline;filename=" + name + ".xls");
                Response.Write(sb.ToString());
                Response.End();


            }
        }
    }
  • 相关阅读:
    L1-049 天梯赛座位分配​​​​​​​
    L1-046 整除光棍 大数除法
    天梯赛 L1-043 阅览室
    Hdu 1022 Train Problem I 栈
    蓝桥杯 历届试题 格子刷油漆  (动态规划)
    第九届蓝桥杯省赛真题 日志统计
    2018年第九届蓝桥杯第7题 螺旋折线
    2018年第九届蓝桥杯省赛 递增三元组
    蓝桥杯 历届试题 高僧斗法  (尼姆博弈)
    K-th Number
  • 原文地址:https://www.cnblogs.com/chjf2008/p/1795886.html
Copyright © 2020-2023  润新知