net导出excelWord文档格式.docx
- 文档编号:5779206
- 上传时间:2023-05-05
- 格式:DOCX
- 页数:19
- 大小:18.60KB
net导出excelWord文档格式.docx
《net导出excelWord文档格式.docx》由会员分享,可在线阅读,更多相关《net导出excelWord文档格式.docx(19页珍藏版)》请在冰点文库上搜索。
///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表
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- net 导出 excel