导出EXCEL、WORD中带有图片的无敌替换法
1.简单的asp导出只要跳转到某个页面带上一个参数即可:
//导出Excel
int mIsExport = RequestHelper.GetQueryString<int>(“IsExport”, 0);
if (mIsExport == 1)
{
Response.Clear();
Response.Buffer = true;
Response.AppendHeader(“Content-Disposition”, “attachment;filename=” + DateTime.Now.ToString(“yyyyMMdd”) + “.xls”);
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = “application/vnd.ms-excel”;
this.EnableViewState = false;
}
//导出Word
int mIsExport = RequestHelper.GetQueryString<int>(“IsExport”, 0);
if (mIsExport == 1)
{
Response.Clear();
Response.Buffer = true;
Response.AppendHeader(“Content-Disposition”, “attachment;filename=” + DateTime.Now.ToString(“yyyyMMdd”) + “.doc”);
Response.ContentEncoding = System.Text.Encoding.Default;
Response.ContentType = “application/vnd.ms-word”;
this.EnableViewState = false;
}
以上方法很方便快捷,但是存在局限性,图片并不能一起导出,因此介绍第二种方法:
2.替换法:
优点:可以一起导出文字、表格、图片
缺点:模板确定性,不可循环,如果模板中有动态内容不推荐使用
局限性:偶然发现一个问题,模板是由office2007转换成的htm网页文件,但是导出结果WPS或者office2003似乎不支持图片,同时,我尝试用2003或者wps转成模板,但是2003或者wps不支持带图片转成模板。遇到瓶颈,求高手指点!
a).首先我们需要将要导出的文件(Word或者Excel~Whatever~)整理好,将页面需要填写数据的地方存在数据的都删除。同时多句嘴,最好打印预览一下文件是不是A4纸大小,方便导出的文件直接可以打印。
b).保存好模板(.doc或者.xls)然后将模板另存为-》其他个格式-》.htm文件注意将附属文件一起保存到一个文件夹中(不懂怎么设置的请咨询我)
c).到程序部分了,新建一个页面,页面内容如下:
<%@ Page Language=”C#” AutoEventWireup=”true” %>
<%@ Import Namespace=”AgileCorp.CBO.Service” %>
<%@ Import Namespace=”AgileCorp.Core.DB” %>
<%@ Import Namespace=”System.Collections.Generic” %>
<%@ Import Namespace=”AgileCorp.Core.ExFile” %>
<%@ Import Namespace=”AgileCorp.Core.Web” %>
<%@ Import Namespace=”System.Data” %>
<%@ Import Namespace=”AgileCorp.Core.Web.Html” %>
<%@ Import Namespace=”AgileCorp.SystemProvider” %>
<%
String tplContent = Server.MapPath(“~/CargoDan.htm”);
String tmpContent = FileHelper.ReadFile(tplContent);
String fileName = Server.MapPath(“~/CargoDan.xls”);
FileHelper.DeleteFile(fileName);
/*取需要的数据*/
String mConsignmentNum = RequestHelper.GetQueryString(“ConsignmentNum”);
//得到货运主表信息
List<DbParam> mDicSysParam = new List<DbParam>();
List<DbParam> mDicInputParam = new List<DbParam>();
mDicInputParam.Add(new DbParam(“Action”, “getone”));
mDicInputParam.Add(new DbParam(“ConsignmentNum”, mConsignmentNum));
mDicInputParam.Add(new DbParam(“PageIndex”, 0));
mDicInputParam.Add(new DbParam(“PageSize”, 0));
DataTable mDtFormDetail = AgileCorpAppSystemService.GetDataFunctionDataTable(“QueryCargo”, mDicInputParam, out mDicSysParam);
String mFromPlace = “”; //出发地
String mToPlace = “”;//到达地
String mAreaTel = “”; //到达地电话
String mWayBillNum = “”;//航班编号
String mReceiveCompanyName = “”;//收件人
String mRealTotalCount = “”;//总数量
String mRealWeightAll = “”;//总重量
String mRealEnterName = “”;//货物性质(即货物品名等等)
String mToPlaceAreaType = “”;
String mFlight1 = “”;
String mCargoDate = “”;
if (mDtFormDetail != null && mDtFormDetail.Rows.Count > 0)
{
mFromPlace = mDtFormDetail.Rows[0][“FromPlace”].ToString();
mToPlace = mDtFormDetail.Rows[0][“ToPlace”].ToString();
mAreaTel = mDtFormDetail.Rows[0][“AreaTel”].ToString();
mWayBillNum = mDtFormDetail.Rows[0][“WayBillNum”].ToString();
mReceiveCompanyName = mDtFormDetail.Rows[0][“ReceiveCompanyName”].ToString();
mRealTotalCount = mDtFormDetail.Rows[0][“RealTotalCount”].ToString();
mRealWeightAll = mDtFormDetail.Rows[0][“RealWeightAll”].ToString();
mRealEnterName = mDtFormDetail.Rows[0][“RealEnterName”].ToString();
mToPlaceAreaType = mDtFormDetail.Rows[0][“ToPlaceAreaType”].ToString();
mFlight1 = mDtFormDetail.Rows[0][“Flight1”].ToString();
mCargoDate = mDtFormDetail.Rows[0][“CargoDate”].ToString();
if (!String.IsNullOrEmpty(mCargoDate))
{
mCargoDate = DateTime.Parse(mDtFormDetail.Rows[0][“CargoDate”].ToString()).ToString(“yyyy-MM-dd”);
}
//临时数据
tmpContent = tmpContent.Replace(“{ConsignmentNum}”, mConsignmentNum);
tmpContent = tmpContent.Replace(“{FromPlace}”, mFromPlace);
tmpContent = tmpContent.Replace(“{ToPlace}”, mToPlace);
tmpContent = tmpContent.Replace(“{AreaTel}”, mAreaTel);
tmpContent = tmpContent.Replace(“{WayBillNum}”, mWayBillNum);
tmpContent = tmpContent.Replace(“{ReceiveCompanyName}”, mReceiveCompanyName);
tmpContent = tmpContent.Replace(“{RealTotalCount}”, mRealTotalCount);
tmpContent = tmpContent.Replace(“{RealWeightAll}”, mRealWeightAll);
tmpContent = tmpContent.Replace(“{RealEnterName}”, mRealEnterName);
tmpContent = tmpContent.Replace(“{ToPlaceAreaType}”, mToPlaceAreaType);
tmpContent = tmpContent.Replace(“{Flight1}”, mFlight1);
tmpContent = tmpContent.Replace(“{CargoDate}”, mCargoDate);
}
FileHelper.WriteFile(fileName, tmpContent);
Response.Redirect(ResolveUrl(“~/CargoDan.xls”));
%>
d).在之前保存好的.htm文件中在恰当的位置填入替换字符“{ConsignmentNum} …”
e).只需一个页面跳转到该执行页面,即可下载文件
需要注意的是,保存好的.htm文件要用VS打开,将其编码格式由gb2312改为UTF-8,不然导出会有乱码。
导出成功界面如下: