第一种最常见,并且最简单的方式,直接把GridView导出,导出格式为文本表格形式。

protected void btnSaveExcel_Click(object sender, EventArgs e)
{
string FileName = "xxx";
System.IO.StringWriter objSW = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter objHTW = new System.Web.UI.HtmlTextWriter(objSW);
try
{
//設定格式
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", FileName));
Response.ContentType = "application/ms-excel";// "application/vnd.ms-excel";
Response.Charset = "UTF-8";
this.EnableViewState = false; this.GridViewData.RenderControl(objHTW);
Response.Write(objSW.ToString());
Response.End();
}
catch (Exception ex)
{
string aa = ex.Message.ToString();
}
finally
{
objSW.Close();
objSW = null;
objHTW.Close();
objHTW = null;
}
}
///必须重载VerifyRenderingInServerForm, 不然会报错
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}

第二种:不用安装Excel,生成原生Excel格式方法

如果你和我一样要实现不调用Excel组件实现Excel数据导出的话,那我严重向你推荐MyXls,MyXls是用C#开源项目,可以应用于asp.net 或者 .net应用程序上。它根据微软公开的Excle文档格式文件(BIFF),以二进制格式直接生成excel文档,支持Excel versions 97 - 2007 。这意味着你可以不用在服务器上安装office就能够以excle格式输出数据库中存储的数据了,这对于许多项目来说都是很有用的

第一步,当然是下在MyXls,地址:http://sourceforge.net/projects/myxls/

第二步,添加引用org.in2bits.MyXls.dll

第三步,实现数据导出,我这里是将一个DataTable作为数据导出,导出后内容格式和DataTable一致,具体代码如下:

        private void Output(DataTable dt)
{
org.in2bits.MyXls.XlsDocument doc = new org.in2bits.MyXls.XlsDocument();
doc.FileName = DateTime.Now.ToString().Replace("-", "").Replace(":", "").Replace(" ", "") + ".xls";//excel文件名称
org.in2bits.MyXls.Worksheet sheet = doc.Workbook.Worksheets.AddNamed("sheet1");//Excel工作表名称
org.in2bits.MyXls.Cells cells = sheet.Cells;
int colnum = dt.Columns.Count;//获取DataTable列数 for (int i = ; i < colnum; i++)
{
cells.Add(, (i + ), dt.Columns[i].Caption.ToString());//导出DataTable列名
}
for (int i = ; i < dt.Rows.Count; i++)
{
for (int j = ; j < colnum; j++)
{
cells.Add((i + ), (j + ), dt.Rows[i][j].ToString());
}
}
//doc.Save(@"D:\"); //保存到指定位置
doc.Send();//把写好的excel文件输出到客户端
}

生成多个WorkSheet

XlsDocument xls = new XlsDocument();//新建一个xls文档
xls.FileName = "MyXlsDemo.xls";//设定Excel文件名 xls.SummaryInformation.Author = "Terry Li"; //填加Excel文件作者信息
xls.SummaryInformation.Subject = "MyXls Demo";//填加文件主题信息
xls.DocumentSummaryInformation.Company = "in2bits.org";//填加文件公司信息 string sheetName = "第一个Sheet Demo";#region string sheetName = "第一个Sheet Demo";
Worksheet sheet = xls.Workbook.Worksheets.Add(sheetName);//填加名为"第一个Sheet Demo"的sheet页
Cells cells = sheet.Cells;//Cells实例是sheet页中单元格(cell)集合
//单元格1-base
Cell cell = cells.Add(, , "三");//设定第2行,第3例单元格的值
cell.HorizontalAlignment = HorizontalAlignments.Centered;//设定文字居中
cell.Font.FontName = "行楷";//设定字体
cell.Font.Height = * ;//设定字大小(字体大小是以 1/20 point 为单位的)
cell.UseBorder = true;//使用边框
cell.BottomLineStyle = ;//设定边框底线为粗线
cell.BottomLineColor = Colors.Red;//设定颜色为红色
cell.RightLineStyle = ;
cell.RightLineColor = Colors.Red; //cell的格式还可以定义在一个xf对象中
XF cellXF = xls.NewXF();//为xls生成一个XF实例(XF是cell格式对象)
cellXF.HorizontalAlignment = HorizontalAlignments.Centered;//设定文字居中
cellXF.Font.FontName = "隶书";//设定字体
cellXF.Font.Height = * ;//设定字大小(字体大小是以 1/20 point 为单位的)
cellXF.UseBorder = true;//使用边框
cellXF.BottomLineStyle = ;//设定边框底线为粗线
cellXF.BottomLineColor = Colors.Green;//设定颜色为绿色
cellXF.LeftLineStyle = ; //设定边框左线为粗线
cellXF.LeftLineColor = Colors.Green; cell = cells.Add(, , "国", cellXF);//以设定好的格式填加cell cellXF.Font.FontName = "仿宋_GB2312";
cellXF.BottomLineStyle = ; //设定边框底线为粗线
cellXF.BottomLineColor = Colors.Blue;//设定颜色为蓝色
cellXF.RightLineStyle = ;//设定边框右线为粗线
cellXF.RightLineColor = Colors.Blue;//设定颜色为蓝色
cellXF.LeftLineStyle = ;
cell = cells.Add(, , "志", cellXF);//格式可以多次使用 //ColumnInfo colInfo = new ColumnInfo(xls, sheet);//生成列格式对象
////设定colInfo格式的起作用的列为第2列到第5列(列格式为0-base)
//colInfo.ColumnIndexStart = 1;//起始列为第二列
//colInfo.ColumnIndexEnd = 5;//终止列为第六列
//colInfo.Width = 15 * 256;//列的宽度计量单位为 1/256 字符宽
//sheet.AddColumnInfo(colInfo);//把格式附加到sheet页上(注:AddColumnInfo方法有点小问题,不给把colInfo对象多次附给sheet页)
//colInfo.ColumnIndexEnd = 6;//可以更改列对象的值
//ColumnInfo colInfo2 = new ColumnInfo(xls, sheet);//通过新生成一个列格式对象,才到能设定其它列宽度
//colInfo2.ColumnIndexStart = 7;
//colInfo2.ColumnIndexEnd = 8;
//colInfo2.Width = 20 * 256;
//sheet.AddColumnInfo(colInfo2); MergeArea meaA = new MergeArea(, , , );//一个合并单元格实例(合并第2行、第5列 到 第3行、第7例)
sheet.AddMergeArea(meaA);//填加合并单元格
cellXF.VerticalAlignment = VerticalAlignments.Centered;
cellXF.Font.FontName = "隶书";
//cellXF.Font.Height = 48 * 20;
//cellXF.Font.Bold = true;
cellXF.Pattern = ;//设定单元格填充风格。如果设定为0,则是纯色填充(无色),1代表没有间隙的实色
cellXF.PatternBackgroundColor = Colors.Red;//填充的底色
cellXF.PatternColor = Colors.Green;//设定填充线条的颜色
cell = cells.Add(, , "晋/陈寿", cellXF);
#endregion sheet.Cells.Merge(, , , );
cell = cells.Add(, , "MyXls 合并单元格 Demo");
cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.VerticalAlignment = VerticalAlignments.Centered; for (int sheetNumber = ; sheetNumber <= ; sheetNumber++)
{
sheetName = "Sheet " + sheetNumber;
int rowMin = sheetNumber;
int rowCount = sheetNumber + ;
int colMin = sheetNumber;
int colCount = sheetNumber + ;
sheet = xls.Workbook.Worksheets.Add(sheetName);
cells = sheet.Cells;
for (int r = ; r < rowCount; r++)
{
if (r == )
{
for (int c = ; c < colCount; c++)
{
cells.Add(rowMin + r, colMin + c, "Column" + (c + )).Font.Bold = true;
}
}
else
{
for (int c = ; c < colCount; c++)
{
int val = r + c;
cell = cells.Add(rowMin + r, colMin + c, val+ ":51CTO五岁了!");
if (val % != )
{
cell.HorizontalAlignment = HorizontalAlignments.Centered;
cell.Font.FontName = "Times New Roman";
cell.Font.Underline = UnderlineTypes.Double;
cell.Font.ColorIndex = ;
cell.Rotation = ; //字符倾斜45度
}
}
}
}
} xls.Send();//XlsDocument.SendMethods.Inline

效果

该代码出自(http://terryli.blog.51cto.com/704315/392125

ASP.NET导出Excel文件的更多相关文章

  1. asp.net 导出excel文件

    之前做过winfrom程序的导出excel文件的功能,感觉非常简单.现在试着做asp.net中导出excel的功能,之前用的是Microsoft.Office.Interop.Excel这个对象来实现 ...

  2. ASP.NETCore -----导出Excel文件并下载

    本事例分为nopi(安装DotNetCore.NPOI)下载和EPPlus(EPPlus.Core.dll)下载,其中npoi下载演示的是根据执行的模板进行数据下载 npoi帮助类NpoiExcelU ...

  3. ASP.NET 导出EXCEL文件处理多对应排列的

    这次项目遇到了一个导出excel需要对应排列的问题.本来在做这个项目之前都基本没做过excel导出的菜鸡,这次强行做还是有些忐忑的,加上那个表的结构比较奇特.    废话不多说,先介绍表结构吧 是数据 ...

  4. ASP.NET 导出excel文件出现乱码的解决办法

    string html =TABLE ;//<table>标签,可以是多张表string modified = Regex.Replace(html, "<table &g ...

  5. 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格式不一致的解决办法

    -----转载:http://blog.csdn.net/sgear/article/details/7663502 关于asp.net C# 导出Excel文件 打开Excel文件格式与扩展名指定格 ...

  6. ASP.NET Core导入导出Excel文件

    ASP.NET Core导入导出Excel文件 希望在ASP.NET Core中导入导出Excel文件,在网上搜了一遍,基本都是使用EPPlus插件,EPPlus挺好用,但商用需要授权,各位码友若有好 ...

  7. Asp.net导出Excel续章(自定义合并单元格,非Office组件)

    结合上次写的导出Excel方法,这次上头要求我将列头进行一下合并 以前的效果: 改进后的效果: 在上篇文章中写到了Excel的导出方法,这次为了避免在生产环境中使用Office组件,服务器各种权限配置 ...

  8. asp.net导出excel示例代码

    asp.net导出excel的简单方法. excel的操作,最常用的就是导出和导入. 本例使用NPOI实现. 代码:/// <summary> );             ;       ...

  9. [转] Asp.Net 导出 Excel 数据的9种方案

    湛刚 de BLOG 原文地址 Asp.Net 导出 Excel 数据的9种方案 简介 Excel 的强大之处在于它不仅仅只能打开Excel格式的文档,它还能打开CSV格式.Tab格式.website ...

随机推荐

  1. ms08-067漏洞--初识渗透测试--想必很多初学者都会遇到我文中提及的各种问题

    最近读了一本书--<<渗透测试实践指南>>,测试了书中的一些例子后,开始拿ms08-067这个经典的严重漏洞练手,实践当中遇到诸多问题,好在一一解决了,获益匪浅. 在谷歌搜索的 ...

  2. JSON 的标准:双引号而非单引号!

    刚刚测试发现一段很简单的.看似正确的代码却是错误的: <?php $json_str = "{'name':'Eric', 'age':23}"; var_dump(json ...

  3. C#中base 关键字的作用

    引用:http://msdn.microsoft.com/en-us/library/hfw7t1ce.aspx base base 关键字用于从派生类中访问基类的成员: 调用基类上已被其他方法重写的 ...

  4. [Linux]I/O多路复用和epoll

    首先我们来定义流的概念,一个流可以是文件,socket,pipe等等可以进行I/O操作的内核对象. 不管是文件,还是套接字,还是管道,我们都可以把他们看作流. 之后我们来讨论I/O的操作,通过read ...

  5. android下giflib

    源码路径: android/external/giflib 用到的该lib的APP: ./external/chromium_org/android_webview/build/aosp_manife ...

  6. 应用商店后台MIS的一些思考

    1.有些签名验证的工作应该在开发者上传APP的时候进行校验: 1)如果是更新新版本(包名packagename一致),那么需要验证两个APK的包的签名是否一致,不一致的,应该限制上传,除非先下架旧的A ...

  7. mmap为什么比read/write快(兼论buffercache和pagecache)

    参考文献: <从内核文件系统看文件读写过程>http://www.cnblogs.com/huxiao-tee/p/4660352.html?utm_source=tuicool& ...

  8. Best code水题之路

    BestCoder 2nd Anniversary: 1001.Oracle There is once a king and queen, rulers of an unnamed city, wh ...

  9. GitHub Desktop+码云(GIT.oschina)使用方法

    一.如何从码云GIT导入到GitHubDeskTop桌面工具. 1.先用命令行切换到本地的目录. 2.使用git clone 码云GIT地址 命令将项目克隆到本地. 3.在GitHub Desktop ...

  10. json 序列化为数组

    我们通常从后台取到json格式的数据到前台进行展示,在这个过程中可能户遇到一些json格式不是自己想要的格式,今天本人就遇到一个棘手的问题,最后在师傅的协助下才进行了正确格式的转换. 可以说最悲哀的莫 ...