net导出excelWord文档格式.docx

上传人:b****1 文档编号:5779206 上传时间:2023-05-05 格式:DOCX 页数:19 大小:18.60KB
下载 相关 举报
net导出excelWord文档格式.docx_第1页
第1页 / 共19页
net导出excelWord文档格式.docx_第2页
第2页 / 共19页
net导出excelWord文档格式.docx_第3页
第3页 / 共19页
net导出excelWord文档格式.docx_第4页
第4页 / 共19页
net导出excelWord文档格式.docx_第5页
第5页 / 共19页
net导出excelWord文档格式.docx_第6页
第6页 / 共19页
net导出excelWord文档格式.docx_第7页
第7页 / 共19页
net导出excelWord文档格式.docx_第8页
第8页 / 共19页
net导出excelWord文档格式.docx_第9页
第9页 / 共19页
net导出excelWord文档格式.docx_第10页
第10页 / 共19页
net导出excelWord文档格式.docx_第11页
第11页 / 共19页
net导出excelWord文档格式.docx_第12页
第12页 / 共19页
net导出excelWord文档格式.docx_第13页
第13页 / 共19页
net导出excelWord文档格式.docx_第14页
第14页 / 共19页
net导出excelWord文档格式.docx_第15页
第15页 / 共19页
net导出excelWord文档格式.docx_第16页
第16页 / 共19页
net导出excelWord文档格式.docx_第17页
第17页 / 共19页
net导出excelWord文档格式.docx_第18页
第18页 / 共19页
net导出excelWord文档格式.docx_第19页
第19页 / 共19页
亲,该文档总共19页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

net导出excelWord文档格式.docx

《net导出excelWord文档格式.docx》由会员分享,可在线阅读,更多相关《net导出excelWord文档格式.docx(19页珍藏版)》请在冰点文库上搜索。

net导出excelWord文档格式.docx

///HttpResponse辅助类。

///提供一些用于请求相应的辅助方法,如:

Excel导出、CSV格式导出等。

/summary>

publicstaticclassHttpResponseHelper

{

#regionPrivatemembersandmethods.

///表现一个缺失的System.Object值。

privatestaticobjectmissing=System.Reflection.Missing.Value;

///Excel文件的MIME类型。

privatestaticreadonlystringMIMEType_AppExcel="

application/excel"

;

///导出Excel(私有方法)。

remarks>

须重载导出页VerifyRenderingInServerForm方法,

///禁用基页同名方法。

<

/remarks>

paramname="

downloadPage"

>

下载页面实例。

/param>

dataSource"

被导出的GridView实例。

fileNameWithouExtension"

不包含后缀的文件名。

textEncoding"

字符编码类型(若输出中含有中文,则必须使用UTF7编码,

///否则中文会显示显示为乱码)。

hiddenColumnIndexs"

(可选)被隐藏列索引数组

///(-1表示最后一列,-2表示倒数第二列)。

privatestaticvoidDoExportExcel(PagedownloadPage,GridViewdataSource,stringfileNameWithouExtension,EncodingtextEncoding,paramsint[]hiddenColumnIndexs)

#regionVerifyParameters.

if(downloadPage==null)

thrownewArgumentNullException("

ExceptionMessageList.ArgumentIsNullOrEmpty);

if(dataSource==null)

PathHelper.VerifyFileNameChars(fileNameWithouExtension,"

);

#endregionVerifyParameters.

//隐藏指定类型的列。

GridViewHelper.HideColumns(dataSource,hiddenColumnIndexs);

//转换Web控件为字面值。

ControlHelper.ConvertToLiteral(dataSource,null);

StringWritersw=newStringWriter();

HtmlTextWriterhw=newHtmlTextWriter(sw);

dataSource.RenderControl(hw);

hw.Flush();

hw.Close();

sw.Flush();

sw.Close();

stringfileNameString=string.Format("

attachment;

filename={0}{1}"

HttpUtility.UrlEncode(fileNameWithouExtension.Trim().Replace(StringHelper.Space,CharHelper.Underscore.ToString())),

FileExtensionList.DotXls);

downloadPage.Response.ContentType=MIMEType_AppExcel;

downloadPage.Response.Clear();

downloadPage.Response.AppendHeader("

Content-Disposition"

fileNameString);

//当从DataGrid导出时,若输出中含有中文(表头或记录中)必须使用UTF7编码,

//否则中文会显示显示为乱码。

downloadPage.Response.ContentEncoding=textEncoding;

downloadPage.Response.Write(sw);

downloadPage.Response.Flush();

downloadPage.Response.End();

}

///将给定控件的内容导出为Excel2003兼容格式的.xls文件

///(注:

WebServer上需安装Excel2003)。

target"

给定控件。

tempRelativeWebDirectory"

用于保存临时文件的Web相对路径,形如:

~/temp。

deleteTempFile"

导出后是否自动删除临时文件,

///true:

删除临时文件;

false:

保留临时文件。

showColumnIndexes"

(可选参数)可显示列的列索引数组,仅对GridView有效。

privatestaticvoidDoExportExcelFor2003(PagedownloadPage,Controltarget,stringtempRelativeWebDirectory,stringfileNameWithouExtension,booldeleteTempFile,EncodingtextEncoding,paramsint[]showColumnIndexes)

#endregion

stringfullPath=DoCreateExportExcelFor2003(target,tempRelativeWebDirectory,fileNameWithouExtension,textEncoding,showColumnIndexes);

CreateDownLoadPage(downloadPage,deleteTempFile,textEncoding,fullPath);

privatestaticstringDoCreateExportExcelFor2003(Controltarget,stringtempRelativeWebDirectory,stringfileNameWithouExtension,EncodingtextEncoding,paramsint[]showColumnIndexes)

if(target==null)

#region将给定控件Render后写入指定文件。

if(targetisGridView)

GridViewHelper.ShowColumns((GridView)target,showColumnIndexes);

ControlHelper.ConvertToLiteral(target,null);

target.RenderControl(hw);

//文件目录。

stringfileDir=HttpContext.Current.Server.MapPath(tempRelativeWebDirectory);

//文件名。

stringfileName=string.Concat(fileNameWithouExtension,FileExtensionList.DotXls);

//全路径=文件目录+文件名。

stringfullPath=Path.Combine(fileDir,fileName);

//将Render后的控件内容写入指定文件。

FileStreamfs=newFileStream(fullPath,FileMode.Create);

StreamWriterstw=newStreamWriter(fs,textEncoding);

stw.Write(sw.ToString());

stw.Flush();

stw.Close();

#region转换为Excel2003兼容的格式。

ApplicationoExcel=null;

WorkbooksoBooks=null;

WorkbookoBook=null;

try

oExcel=newApplication();

//不显示任何提示信息。

oExcel.DisplayAlerts=false;

//所用到的对象须独立声明。

oBooks=oExcel.Workbooks;

oBook=oBooks.Open(fullPath,missing,missing,missing,missing,missing,missing,missing,

missing,missing,missing,missing,missing,missing,missing);

//XlFileFormat.xlWorkbookNormal格式可同时在Office2003和2007下正常打开。

oBook.SaveAs(fullPath,XlFileFormat.xlWorkbookNormal,missing,missing,false,false,XlSaveAsAccessMode.xlNoChange,missing,missing,missing,missing,missing);

//XlFileFormat.xlCSV即导出.csv文件时使用。

//oBook.SaveAs(fullPath,XlFileFormat.xlCSV,missing,missing,false,false,XlSaveAsAccessMode.xlNoChange,missing,missing,missing,missing,missing);

oBook.Close(missing,fullPath,missing);

oExcel.Quit();

finally

ClearComObject(oBook);

//须清理。

ClearComObject(oBooks);

ClearComObject(oExcel);

returnfullPath;

///将给定控件的内容生成为Excel2003兼容格式的.xls文件

publicstaticstringCreateExportExcelFor2003(Controltarget,stringtempRelativeWebDirectory,stringfileNameWithouExtension,EncodingtextEncoding,paramsint[]showColumnIndexes)

returnDoCreateExportExcelFor2003(target,tempRelativeWebDirectory,fileNameWithouExtension,textEncoding,showColumnIndexes);

///创建文件下载页。

是否删除临时文件。

编码。

fullPath"

下载文件完整路径。

publicstaticvoidCreateDownLoadPage(PagedownloadPage,booldeleteTempFile,EncodingtextEncoding,stringfullPath)

#region将导出的.xls文件写入下载页Response流。

filename={0}"

HttpUtility.UrlEncode(Path.GetFileName(fullPath).Replace(StringHelper.Space,CharHelper.Underscore.ToString())));

//当从DataGrid导出时,若输出中含有中文(表头或记录中)必须使用UTF7编码,

//否则中文会显示显示为乱码。

downloadPage.Response.TransmitFile(fullPath);

catch

throw;

if(deleteTempFile)

File.Delete(fullPath);

///清理给定的COM组件对象。

给定的COM组件对象。

privatestaticvoidClearComObject(objecttarget)

System.Runtime.InteropServices.Marshal.ReleaseComObject(target);

catch{}

target=null;

/*

*

///重载此页面方法,禁用基类验证方法。

control"

publicoverridevoidVerifyRenderingInServerForm(Controlcontrol)

//base.VerifyRenderingInServerForm(control);

*/

#regionZzzSleepingCode:

将GridView直接导出为伪.xls文件。

///导出Excel。

///(-1表

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 工程科技 > 能源化工

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2