记录点滴

记录生活

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  67 随笔 :: 2 文章 :: 233 评论 :: 17万 阅读
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
复制代码
代码
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();


        }
    }
}
复制代码

 

posted on   啊峰  阅读(453)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· 展开说说关于C#中ORM框架的用法!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
点击右上角即可分享
微信分享提示