C#Winform版获取Excel文件的内容
public MainForm()
{
InitializeComponent();
//this.ofd_in.DefaultExt = ".xlsx";
this.ofd_in.Filter = "Excel文件(*.xlsx)|*.xlsx";
}
// code.GetSheetName getname = new code.GetSheetName();
private void btnDr_Click(object sender, EventArgs e)
{
if (ofd_in.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.txtFileName.Text = ofd_in.FileName.ToString();
List<Model.ExcelSheet> lst = code.GetSheetName.ExcelSheetName(ofd_in.FileName.ToString());
cmbBox.DataSource = lst;
cmbBox.DisplayMember = "SheetName";
cmbBox.ValueMember = "Id"; //ImportExcel(ofd_in.FileName, "[Sheet1$]");//传递excel文件名,和表名
}
} /// <summary>
/// 从选择的excel导入
/// </summary>
/// <returns></returns>
public void ImportExcel(string ExcelName, string Tablename)
{
// string strcon = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + ExcelName + ";Extended Properties='Excel 8.0;HDR=No;IMEX=1';";//连接excel文件的字符串 string strcon = "Provider=Microsoft.Ace.OleDb.12.0;data source=" + ExcelName + ";Extended Properties='Excel 12.0; HDR=Yes; IMEX=1'"; //此连接可以操作.xls与.xlsx文件 (支持Excel2003 和 Excel2007 的连接字符串)
if (ExcelName == null)
{
return;
}
OleDbConnection odcon = new OleDbConnection(strcon);//建立连接
odcon.Open();//打开连接 //把数据集dataset放到控件中显示
OleDbDataAdapter odda = new OleDbDataAdapter("select * from " + Tablename, odcon);
DataSet ds = new DataSet();
try
{
odda.Fill(ds);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
if (ds != null)
{ this.dgvdata.DataSource = ds.Tables[]; }
else
{
MessageBox.Show("没有数据");
}
} private void btnClose_Click(object sender, EventArgs e)
{
this.Close();
} /// <summary>
/// 保存,选中的datagridview的值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSave_Click(object sender, EventArgs e)
{
int selectedCellCount = dgvdata.GetCellCount(DataGridViewElementStates.Selected);
if (selectedCellCount > )
{
if (dgvdata.AreAllCellsSelected(true))
{
MessageBox.Show("All cells are selected", "Selected Cells");
}
else
{
StringBuilder sb = new StringBuilder(); for (int i = ;
i < selectedCellCount; i++)
{
sb.Append("Row: ");
int rows = dgvdata.SelectedCells[i].RowIndex;
sb.Append(rows.ToString());
sb.Append(", Column: ");
int columns = dgvdata.SelectedCells[i].ColumnIndex;
sb.Append(columns.ToString());
sb.Append(",Value: ");
sb.Append(dgvdata.Rows[rows].Cells[columns].Value.ToString());
sb.Append(Environment.NewLine);
} sb.Append("Total: " + selectedCellCount.ToString());
MessageBox.Show(sb.ToString(), "Selected Cells");
}
}
} /// <summary>
/// 加载对应的sheet页
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnSearch_Click(object sender, EventArgs e)
{
dgvdata.DataSource = null;
var sheetname = this.cmbBox.Text.Trim(); ;
ImportExcel(this.txtFileName.Text, "["+sheetname+"]");
}
C#Winform版获取Excel文件的内容的更多相关文章
- VB6.0 获取Excel文件工作表Sheet的名称
获取Excel文件工作表Sheet的名称 '产生Excel文档 Dim xlapp, xlbook As Object Dim sSheetName As String Set xlapp = Cre ...
- C#可以获取Excel文件中Sheet的名字
C#可以获取Excel文件中Sheet的名字吗 C#可以获取Excel文件中Sheet的名字吗 我试过WPS的表格可以 可以 要代码么 百度都有 [深圳]Milen(99696619) 14:13: ...
- .NetCore获取json文件配置内容
.netcore中的数据配置及内容用了json文件代替了之前framework的xml文件,那么json中的数据该怎么获取呢?下面讲解json文件在.net core中的获取方法. 首先,新建一个.n ...
- C#读取excel文件的内容(使用DataSet)
C#读取Excel文件的内容,通过OLEDB来连接,关键是连接的路径,如:string strConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data S ...
- C#中获取Excel文件的第一个表名
// 2.以数据库方式打开并输入数据// 此方式将xls文件所在目录看作数据库,其中的xls文件看作数据库表,表名即文件名(不加扩展名).// 函数importExcelTo ...
- 获取properties文件的内容
获取properties文件的内容 public void test() throws Exception{ String resource = "application.propertie ...
- 用PHPExcel类读取excel文件的内容
这里对PHPExcel类不做介绍,有兴趣的朋友可以自己查阅资料 在classes文件夹下有个PHPExcel.php文件,这个文件是这个类库的主要入口文件,在用之前,要引入这个类 其他的类,在此类中会 ...
- Python之excel文件追加内容
首先要安装三个模块:xlrd,xlwt,xlutils 命令:pip install xlrd xlwt xlutils 示例代码: #!/usr/bin/env python # -*- codin ...
- winform下读取excel文件并绑定datagridview例子
首先我要读取这个excel文件然后生成Datable 用winform编程的方式 前台界面: 后台的代码 using System; using System.Collections.Generic; ...
随机推荐
- JS基础DOM篇之二:DOM级别与节点层次?
通过上一篇我们大致了解了什么是DOM,今天我们继续深入了解. 1.DOM级别 在大家阅读DOM标准的时候,可能会看到DOM(0/1/2/3)级的字眼,这就是DOM级别.但实际上,DOM0级 ...
- 字符串匹配 - KMP算法
#include<cstdio> #include<cstring> #include<cstdlib> void GetNext(char *t,int *nex ...
- PL SQL Developer 使用总结
如果OS为windows 7 64位系统,Oracle版本为 Oracle 11g 64 安装PL SQL Developer 请参考 http://myskynet.blog.51cto.co ...
- Webservice服务中如何保持Session
问题一:webservice服务中如果保持Session 调用Session 对于Web Service,每个方法的调用都会启动一个Session,可以用下面的方法来使多个调用在同一个Session里 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
- HDU 4593 H - Robot 水题
H - RobotTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.act ...
- 小学生玩ACM----优先队列
思来想去,本人还是觉得,这个优先队列啊,不学不行,怎么说咧?虽说有时候我可以模仿它的功能,但是有的题目会坑的我大放血,况且多学会用一个小东东总不会伤身的撒,何况我是永举不垂的,哦耶,嘿嘿 优先队列嘛就 ...
- Android apk程序调用其它的APK程序
Intent mIntent = new Intent(); ComponentName comp = new ComponentName("启动的APK包名","启动的 ...
- [Javascript] Functor law
Functor laws: 1. Identity: map(id) == id 2. Composition: compose(map(f), map(g)) == map(compose(f,g) ...
- [AngularJS] Introduction to ui-router
Introduce to basic $stateProvider.state() with $stateParams services. Understand how nested router w ...