转换GridView的内容到Excel里面 ---带有格式
1. HTML 页面
添加GridView控件,编辑要绑定的字段。
添加转换按钮
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" OnPageIndexChanging="GridView1_PageIndexChanging" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="" CellSpacing="">
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" /> <Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Address" HeaderText="Address" />
</Columns>
</asp:GridView> <asp:Button ID="Button1" runat="server" Text="ExportToExcel" OnClick="Button1_Click"/>
</div>
2. 添加需要的命名空间
using System.IO;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Configuration;
3. 后台绑定GridView的方法
private void BindGrid()
{
string strConnString = ConfigurationManager.ConnectionStrings["DemosDatabaseConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(strConnString))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers"))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
4. 在页面加载的时候,绑定GridView
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
5. 实现GridView的分页--PageIndexChanging事件
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
this.BindGrid();
}
6.把GridView转换成Excel的方法
protected void Button1_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=GridViewExport.xls");
Response.Charset = "";
Response.ContentType = "application/vnd.ms-excel";
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw); //To Export all pages
GridView1.AllowPaging = false;
this.BindGrid(); GridView1.HeaderRow.BackColor = Color.White;
foreach (TableCell cell in GridView1.HeaderRow.Cells)
{
cell.BackColor = GridView1.HeaderStyle.BackColor;
}
foreach (GridViewRow row in GridView1.Rows)
{
row.BackColor = Color.White;
foreach (TableCell cell in row.Cells)
{
if (row.RowIndex % == )
{
cell.BackColor = GridView1.AlternatingRowStyle.BackColor;
}
else
{ cell.BackColor = GridView1.RowStyle.BackColor
}
cell.CssClass = "textmode";
}
} GridView1.RenderControl(hw);
//style to format numbers to string
string style = @"<style> .textmode { } </style>";
Response.Write(style);
Response.Output.Write(sw.ToString());
Response.Flush();
Response.End();
}
}
7. 必须添加的一个事件
public override void VerifyRenderingInServerForm(Control control)
{
/* Verifies that the control is rendered */
}
转换GridView的内容到Excel里面 ---带有格式的更多相关文章
- 将gridview 的数据导出EXCEL
gridview数据 单击“导出EXCEL”按钮后 1.在上面的代码中,先将gridview绑定到指定的数据源中,然后在button按钮(用来做导出到EXCEL的)的事件中,写入相关的代 ...
- ASP.NET中GridView数据导出到Excel
/// <summary> /// 导出按钮 /// </summary> /// <param name="sender"></para ...
- VBA读取word中的内容到Excel中
原文:VBA读取word中的内容到Excel中 Public Sub Duqu() Dim myFile As String Dim docApp As Word.Applicati ...
- Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现
Android控件GridView之仿支付宝钱包首页带有分割线的GridView九宫格的完美实现 2015-03-10 22:38 28419人阅读 评论(17) 收藏 举报 分类: Android ...
- Excel表格文本格式的数字和数字格式如何批量转换
Excel表格文本格式的数字和数字格式如何批量转换 在使用Excel表格对数据求和时,只能对单元格内常规格式的数据进行计算,而不能对单元格中的文本格式的数据进行计算,特点就是在单元格的左上角有一个绿色 ...
- C# 对Excel 单元格格式, 及行高、 列宽、 单元格边框线、 冻结设置
一.对行高,列宽.单元格边框等的设置 这篇简短的文字对单元格的操作总结的比较全面,特此转载过来. private _Workbook _workBook = null; private Workshe ...
- Java将数据按列写入Excel并设置格式(字体、背景色、自动列宽、对齐方式等)
本文使用jxl.jar工具类库将数据按列写入Excel并设置格式(字体.背景色.自动列宽.对齐方式等). /** * 按列写入Excel并设置格式 * * @param outputUrl * 输出路 ...
- 图解-Excel的csv格式特殊字符处理方式尝试笔记(个人拙笔)
Excel格式如下.(截图来自,WPS Office) CSV是一种文本格式的Excel文档格式.不支持Excel的字体特效(比如加粗,颜色)等等的保存. 每一行数据用 "\n" ...
- 03 Uipath调用VBA脚本,处理excel文档格式
前言: 在平时我们的工作中,经常需要使用Uipath自动的导入大量数据到Excel表格中,但是却发现,数据导入到Excel之后,格式却是很乱,基本不能看,就像下图: 而Uipath对Excel的操 ...
随机推荐
- Mysql笔记之 -- replace()实现mysql 替换字符串
mysql 替换函数replace()实现mysql 替换字符串 mysql 替换字符串的实现方法: mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数 ...
- 【LeetCode题意分析&解答】42. Trapping Rain Water
Given n non-negative integers representing an elevation map where the width of each bar is 1, comput ...
- Linux 查看文件
1: cat file_full_name 这种方法适合查看小文件 cat /etc/services cat -n file_full_name -n 可以加行号! 2: tac file_ful ...
- Linux 下修改Tomcat使用的JVM内存大小
我的服务器的配置: # OS specific support. $var _must_ be set to either true or false. JAVA_OPTS="-Xms10 ...
- extjs 4.0.7 Desktop 替换claaes.js
最近在学习Extjs 发现Demo 中有个Desktop的于是就尝试学习一下.结果发现怎么改都没效果后来偶然发现 默认采用的是classes.js 为压缩后的js 引入已经修改后的分开的js瞬间 一切 ...
- C功底挑战Java菜鸟入门概念干货(一)
一.认识Java 1.Java 程序比较特殊,它必须先经过编译,然后再利用解释的方式来运行. 2.Byte-codes 最大的好处是——可越平台运行,可让“一次编写,处处运行”成为可能. 3.使用 ...
- perl 爬虫两个技巧
<pre name="code" class="cpp">jrhmpt01:/root/lwp# cat data.html <div cla ...
- JqGrid使用经验
一.更改分页组件的样式 分页组件中
- Linux c 信号—pause、sigsuspend 的相同于区别
pause函数: 功能:让进程暂停直到信号出现 #include<unistd.h> intpause(); 函数说明:pause()会令目前的进程暂停(进入睡眠状态),直至信号 ...
- TP的SDK的调用
1,SDK简介 本SDK是基于ThinkPHP开发类库扩展,因此只能在ThinkPHP平台下使用(ThinkPHP版本要求2.0以上).DEMO中用到了控制器分层,因此运行DEMO需使用ThinkPH ...