asp.net 下载和在线预览Excel的方法

直接代码,红色字体是对大家有用地。

public string ExcelCache
        {
            get
            {
                string _path = Path.Combine(System.Web.Hosting.HostingEnvironment.ApplicationPhysicalPath, "ExcelBuffer\\");
                if (!Directory.Exists(_path))
                {
                    Directory.CreateDirectory(_path);
                }
                return _path;
            }

        }

                Report rpt = Report.LoadReport(Convert.ToInt32(reportID));
                ExcelHeaderBuilder builder = new ExcelHeaderBuilder();
                ExcelWorkbook book = new ExcelWorkbook();
                book.CreateSheet(sheetName);
                book.SetActiveSheet = sheetName;
                ExcelWorksheet sheet = book.GetSheet(sheetName);
                ExcelCellStyle style = book.CreateStyle();
                style.BorderColour = EnumColours.Black;
                style.BorderLineStyle = EnumLineStyle.Thin;
                style.Font.Name = "黑体";
                style.Font.Size = 12;
                style.HorizontalAlignment = EnumHorizontalAlignment.Center;
                style.VerticalAlignment = EnumVerticalAlignment.Center;

                builder.Style = style;
                builder.MakeHeader(sheet, rpt);

                book.Save(path);
                    //下载
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + Path.GetFileName(path));
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.Charset = "GB2312";
                    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    HttpContext.Current.Response.WriteFile(path);
                    HttpContext.Current.Response.Flush();
            
                    //在线
                    HttpContext.Current.Response.Clear();
                    HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + Path.GetFileName(path));
                    HttpContext.Current.Response.ContentType = "application/octet-stream";
                    HttpContext.Current.Response.Charset = "GB2312";
                    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    HttpContext.Current.Response.WriteFile(path);
                    HttpContext.Current.Response.Flush();


转载于:https://www.cnblogs.com/raylovelc/archive/2008/01/24/1051973.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值