读取Excel数据绑定到Gridview进行显示
读取Excel数据绑定到Gridview进行显示示例代码。
读取excel代码
/// <summary>
/// 读取Excel
/// authon:codeo.cn
/// </summary>
/// <param name="strExcelFileName"></param>
/// <param name="strSheetName"></param>
/// <returns>DataTable</returns>
public static DataTable ExcelToDataTable(string strExcelFileName, string strSheetName)
{
//源的定义
string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + strExcelFileName + ";" + "Extended Properties='Excel 8.0;HDR=NO;IMEX=1';"; //Sql语句
//string strExcel = string.Format("select * from [{0}$]", strSheetName); 这是一种方法
string strExcel = "select * from [" + strSheetName + "$]";//[sheet1$] //定义存放的数据表
DataSet ds = new DataSet(); //连接数据源
OleDbConnection conn = new OleDbConnection(strConn); conn.Open(); //适配到数据源
OleDbDataAdapter adapter = new OleDbDataAdapter(strExcel, strConn);
adapter.Fill(ds, strSheetName); conn.Close(); return ds.Tables[strSheetName];
}
GridView绑定daTable数据
protected void Page_Load(object sender, EventArgs e)
{
string path = GetRootPath();
DataTable daTable = ExcelToDataTable(path + "Content\\demo.xlsx", "Sheet1");
GridView1.DataSource = daTable;
GridView1.DataBind();
}
asp.net取得网站根目录的物理路径
/// <summary>
/// 取得网站根目录的物理路径
/// authon:codeo.cn
/// </summary>
/// <returns></returns>
public static string GetRootPath()
{
string AppPath = "";
HttpContext HttpCurrent = HttpContext.Current;
if (HttpCurrent != null)
{
AppPath = HttpCurrent.Server.MapPath("~");
}
else
{
AppPath = AppDomain.CurrentDomain.BaseDirectory;
if (Regex.Match(AppPath, @"\\$", RegexOptions.Compiled).Success)
AppPath = AppPath.Substring(, AppPath.Length - );
}
return AppPath;
}
补充
如何修改表头信息?
只需在绑定数据前将datatable的列名修改。
daTable.Columns[].ColumnName = "表头1";
daTable.Columns[].ColumnName = "表头2";
daTable.Columns[].ColumnName = "表头3";
读取Excel数据绑定到Gridview进行显示的更多相关文章
- c#读取Excel数据到Gridview
#region 读取Excel数据到Gridview public void ReadExcel(string sExcelFile, GridView dgBom) { DataTable E ...
- VC6.0读取Excel文件数据
啰嗦一下:本人所在公司从事碟式斯特林太阳能发电设备的研发与销售.单台设备图如下: 工作原理如下:整个设备大致可分为五个部分, 1.服务器,负责气象.发电等数据存取,电.网连接等处理: 2.气象站,通过 ...
- C#读取Excel显示到repeater中
首先需要一个用来存储我们需要显示的内容,防止页面回发丢失(添加时使用) #region 缓存文件 private DataTable excelData; /// <summary> // ...
- clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据。 这是一个bug 哈哈
clientdataset 读取excel 如果excel 文件不存在的时候 相应的gird 会不显示数据, 鼠标掠过 gird 格子 才会显示数据. 这是一个bug 哈哈
- Python:读取Excel表格时出现的u'\u51c6’ 无法正确显示汉字
读取Excel后,想显示其中一行的元素,结果读出来是这样[u'\u51c6\u8003\u8bc1\u53f7', u'\u8003\u751f\u59d3\u540d'],始终不显示正常的汉字 依照 ...
- C# ASP.NET 读取EXCEL 单元格 读取 空值 不显示
跟大家分享一下,[摘自]:http://blog.csdn.net/li185416672/article/details/8213729 读取excel时,某些单元格为空值 原来如此: 当我们用ol ...
- 读取Excel任务列表并显示在Outlook日历上
前几天,公司发了一个任务安排,时间不固定,但要求准时到,为了给自己加一个提醒,也为了回顾一下以前的技术,特做了一个Demo. 读取Excel就不多说了,代码很简单,但支持老版本Excel和的版本Exc ...
- 【转】ExcelHelper类,用npoi读取Excel文档
//------------------------------------------------------------------------------------- // All Right ...
- [转]C# web 读取Excel文件
项目中总是遇到要整理基础数据的问题,少量的数据还好说,如果数据量大的话,这无疑会增加项目开发的用时,拖延交期. 那么我们会让客户自己去整理基础数据,但是问题是,客户整理的数据怎写入系统呢?我们一般会采 ...
随机推荐
- XML约束图解
- 限制窗口拉伸范围——WM_GETMINMAXINFO
注意:此方法对CListCtrl的Report模式下的表头绘制有影响,用时需注意测试! 使用OnSizing的改进版 该例程用处为将窗口限制了大小,并且只允许上下拉伸.需要注意的是WM_GETMINM ...
- ONVIF Event消息解析(How to work with gSoap)
Prepare Requirements ONVIF Event gSoap reference ONVIF Specification 问题描述 Event是ONVIF核心规范中一块, 文档解释了如 ...
- 分享Git的一些个人配置
先贴上自己.gitconfig的相关命令,我再具体说一下 diff.external=~/.git-meld.sh http.sslverify=false http.proxy=http://127 ...
- RemoteWebDriver使用说明
1. 本地代码使用RemoteWebDriver启动: public class Testing { public void myTest()throws Exception { WebDriver ...
- 【转载】如何学习javascript
如何学习Javascript 作者: chaomao 首先要说明的是,咱现在不是高手,最多还是一个半桶水,算是入了JS的门. 谈不上经验,都是一些教训. 这个时候有人要说,“靠,你丫半桶水,凭啥教我 ...
- C# Word
C# 操作word文档 1.c#操作word 在指定书签插入文字或者图片 1using Word = Microsoft.Office.Interop.Word; 2 3object Nothing ...
- FMDB警告Warning: there is at least one open result set around after performing的问题
FMDB操作sqlite的时候总是报警告Warning: there is at least one open result set around after performing,后来发现是执行查询 ...
- 深入了解使用egret.WebSocket
概念 本教程不讲解TCP/IP协议,Socket属于哪层,消息包体怎么设计等,主讲 egret.WebSocket 使用示例 与 protobuf 使用示例. 在使用egret.WebSocket之前 ...
- jquery获取元素方式
1 从集合中通过指定的序号获取元素 html: <div> <p>0</p> <p>1</p> <p>2</p> & ...