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();


        }
    }
}

 

你可能感兴趣的:(excel2003)