Aspose Words、Excel导出等操作
/*Word、Excel先保存再输出-下载*/
strReportFilePath = Server.MapPath("~") + strReportFilePath; doc.Save(strReportFilePath); if (!string.IsNullOrEmpty(strReportFilePath))
{
string NewFile = strReportFilePath;// Server.MapPath(strReportFilePath); //如果文件不存在,可能需要执行重新生成
FileStream fs = new FileStream(NewFile, FileMode.Open);
byte[] buffer = new byte[fs.Length];
fs.Read(buffer, , buffer.Length); string strFileName = strReportName + ".docx";
//判断浏览器类型 如果是IE文件名转编码
string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
//IE浏览器
if (curBrowser.IndexOf("explorer", StringComparison.Ordinal) >= ||
curBrowser.IndexOf("ie", StringComparison.Ordinal) >= )
{
strFileName = HttpUtility.UrlEncode(strFileName, Encoding.UTF8);
} Response.ContentType = "application/ms-word";
Response.Charset = "utf-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName); Response.OutputStream.Write(buffer, , buffer.Length);
fs.Flush();
fs.Close();
Response.Flush();
return;
}
/*Word直接输出*/
MemoryStream mStream = new MemoryStream();
doc.Save(mStream, Aspose.Words.SaveFormat.Doc); HttpContext.Current.Response.BinaryWrite(mStream.ToArray());
/*Excel导出*/
Workbook workbook = new Workbook();
/****/
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "utf-8";
Response.SetCookie(new HttpCookie("fileDownload", "true") { Path = "/" });
//判断浏览器类型 如果是IE文件名转编码
string curBrowser = HttpContext.Current.Request.Browser.Type.ToLower();
//IE浏览器
if (curBrowser.IndexOf("explorer", StringComparison.Ordinal) >= || curBrowser.IndexOf("ie", StringComparison.Ordinal) >= )
{
strName = HttpUtility.UrlEncode(strName, Encoding.UTF8);
} HttpContext.Current.Response.AppendHeader("Content-Disposition", string.Format("attachment;filename={0}.xls", strName));
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.BinaryWrite(workbook.SaveToStream().ToArray());
HttpContext.Current.Response.End();
//以html table 的方式输出Excel
string strMNName = string.Format("{0}{1}({2}到{3})综合报表", listMN[].UnitsName, listMN[].MNName,
DateTime.Parse(strST).ToString("yyyy年MM月dd日"), DateTime.Parse(strET).ToString("yyyy年MM月dd日")); stBuilder.Append("<style type='text/css'>tr,td{border:solid .5pt Black;}</style>");
stBuilder.Append(string.Format("<table><tr height=24 style='font-size:10pt;text-align: center;font-weight:bolder;'><td colspan={0}>{1}</td></tr>", iRow, strMNName)); stBuilder.Append(stTitle.);
stBuilder.Append(stContent);
stBuilder.Append("</table>");
System.IO.StringWriter sw = new System.IO.StringWriter(stBuilder); HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strMNName, Encoding.UTF8) + ".xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
HttpContext.Current.Response.ContentType = "application/ms-excel";
HttpContext.Current.Response.Write(sw);
HttpContext.Current.Response.End();
Excel xls最大行65536行,xlsx最大行1048576行;
Xlsx格式输出问题
因Aspose.Cells Workbook Response.BinaryWrite(workbook.SaveToStream().ToArray()); 输出流输出sheet页最大行65536行;可改为先存储文件,再以url连接下载或文件流的方式输出
//输出xlsx格式
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; //"application/ms-excel";
//保存xlsx文件--可用上述方法(FileStream)输出
workbook.Save("C:\\Test.xlsx", SaveFormat.Xlsx);
文件流输出方式参考GetExcelFile 或 DownLoadFile方法
Asp.Net输出Excel Xlsx
//Asp.Net输出Excel Xlsx
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ExpCellNameToXLSX = true;
saveOptions.SaveFormat = SaveFormat.Xlsx; //SaveOptions saveOptions1 = null;
//saveOptions1.ExpCellNameToXLSX = true;
//saveOptions1.SaveFormat = SaveFormat.Xlsx;
//saveOptions.Encoding= Encoding.UTF8;
//workbook.Save("D://1.xlsx", FileFormatType.Xlsx, SaveType.Default, Response);
workbook.Save(Response, "1.xlsx", ContentDisposition.Inline, saveOptions);
Response.Flush();
Response.Close();
Response.End();
Aspose Words、Excel导出等操作的更多相关文章
- 依赖Aspose.Cells Excel 导出
public static void SaveExcel() { //新建工作簿 Workbook workbook = new Workbook(); //工作簿 Worksheet sheet = ...
- 利用Aspose.Cells完成easyUI中DataGrid数据的Excel导出功能
我准备在项目中实现该功能之前,google发现大部分代码都是利用一般处理程序HttpHandler实现的服务器端数据的Excel导出,但是这样存在的问题是ashx读取的数据一般都是数据库中视图的数据, ...
- Aspose.Cells 导入导出EXCEL(转)
Aspose.Cells 导入导出EXCEL 修改样式 Workbook workbook = new Workbook(); //工作簿 Worksheet ...
- 对Aspose.Cells Excel文件操作的扩展
工作中对Excel操作的需求很是常见,今天其他项目组的同事在进行Excel数据导入时,使用Aspose.Cells Excel 遇到了些问题. 刚好闲来不忙,回想自己用过的Excel文件操作,有NPO ...
- 基于ABP和Magicodes实现Excel导出操作
前端使用的vue-element-admin框架,后端使用ABP框架,Excel导出使用的Magicodes.IE.Excel.Abp库.Excel导入和导出操作几乎一样,不再介绍.文本主要介绍E ...
- 转:POI操作Excel导出
package com.rd.lh.util.excel; import java.beans.PropertyDescriptor; import java.io.FileOutputStream; ...
- 偷懒小工具 - Excel导出公共类
说明 最近接了一个任务,就是做一个列表的Excel导出功能.并且有很多页面都会使用这个功能. 导出的Excel大体格式如图 很简单的列表,标题加背景色,然后不同类型,显示方式不一样.对齐方式不一样.不 ...
- TP5.0源生Excel导出
PHPExcel类在TP5里边并不能很好的兼容,使用起来很麻烦. 不像是tp3.2那样直接import()加进来就能new,因为它里边的命名空间找不到.总是说undefined class. 如果是使 ...
- Atitit.excel导出 功能解决方案 php java C#.net版总集合.doc
Atitit.excel导出 功能解决方案 php java C#.net版总集合.docx 1.1. Excel的保存格式office2003 office2007/2010格式1 1.2. 类库选 ...
随机推荐
- 日志服务与SIEM(如Splunk)集成方案实战
背景信息 目标 本文主要介绍如何让阿里云日志服务与您的SIEM方案(如Splunk)对接, 以便确保阿里云上的所有法规.审计.与其他相关日志能够导入到您的安全运维中心(SOC)中. 名词解释 LOG( ...
- Django关于migrate无效的问题
目录 django关于manage.py migrate无效的问题解决 django关于manage.py migrate无效的问题解决 问题描述: 对于django已有的model,修改之后,想重新 ...
- System.Web.Mvc.JsonResult.cs
ylbtech-System.Web.Mvc.JsonResult.cs 1.程序集 System.Web.Mvc, Version=5.2.3.0, Culture=neutral, PublicK ...
- .Net Core微服务系列--服务发现
什么是服务发现 首先我们先思考一个问题,当我们在浏览器中输入一个域名比如baidu.com,然后发生了什么才能让我们访问到百度的网页?简单来说,浏览器会首先从主机的hosts文件中查看是否有baidu ...
- SPRINGBOOT配置MYSQL,MYBATIS,DRUID
配置 DRUID连接池 MYSQL数据库 MYBATIS持久层框架 添加依赖 <dependency> <groupId>mysql</groupId> <a ...
- shell下时间日期的加减乘除运算
首先我们先来说说什么是shell下的时间戳: 自1970年1月1日(00:00:00 UTC/GMT)以来的秒数.它也被称为Unix时间戳(Unix Timestam.Unix epoch.POSIX ...
- MySQL系列(十一)--外键约束foreign key的基本使用
有些时候,为了保证数据的完整性,我们会选择的使用外键约束,例如教师对应的表和课程表中老师的id,这种时候就要使用外键约束了. PS:这里不考虑表结构设计,三范式与反范式等设计问题,基于MySQL8.0 ...
- HZOI20190803 B题
题目:https://www.cnblogs.com/Juve/articles/11295333.html 话说这题方法挺多 40分:暴力 65:莫队,你会T得飞起 我考场上没打出带修莫队,没有修改 ...
- md5密码入库
<?php //连接数据库 $pdo = new PDO('mysql:host=localhost;dbname=md5;charset=UTF8', 'root', ''); # 设置为fa ...
- 省际联动distpicker插件的使用讲解
1.在使用input页面加载script的引用 <script src="js/distpicker/distpicker.data.js"></script&g ...