C# 导入Excel
#region 导入
/// <summary>
/// 导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnImport_Click(object sender, EventArgs e)
{
try
{
DataSet ds = new DataSet();
OpenFileDialog openFileDialog = new OpenFileDialog();
openFileDialog.Filter = "Excel文件(*.xls,*.xlsx)|*.xls;*.xlsx";
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
string filename = openFileDialog.FileName;
string connection = "Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + filename + ";Extended Properties=Excel 8.0";
OleDbConnection thisconnection = new OleDbConnection(connection);
thisconnection.Open();
DataTable tb = thisconnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string sheetName = tb.Rows[]["TABLE_NAME"].ToString();
string command = "select * from [" + sheetName + "]";
OleDbDataAdapter adapter = new OleDbDataAdapter(command, thisconnection);
adapter.Fill(ds, "[" + sheetName + "]");
thisconnection.Close();
DataTable _DataRows = ds.Tables[];
if (_DataRows.Rows.Count > )
{
//将从Excel读取的数据显示到gd上
ShowItems(_DataRows);
}
else
Commons.ShowMessageEx("所选择文件中没有可导入数据行!", MessageKind.Exclamation);
}
}
catch (Exception ex)
{
if (ex.Message == "外部表不是预期的格式。")
{
Commons.ShowMessageEx("请将数据文件进行另存后,重试导入!", MessageKind.Error);
}
else
MessageBoxEx.ShowError(ex);
}
}
void ShowItems(DataTable ds)
{
this.dg_WelfaresDetails.Rows.Clear();
int err = ;
string Msgs = string.Empty;
decimal mJe = ; //总金额 int dgRows = ;
for (int i = ; i < ds.Rows.Count; i++)
{
//检测账户是否存在
DataTable dt = CommonBLL.GetList("*", "V_Finance_EmployeeAccs", "ccode='" + ds.Rows[i]["账户编号"].ToString().Trim() + "' and CNAME='" + ds.Rows[i]["账户名称"].ToString().Trim() + "' and cbank='" + ds.Rows[i]["身份证号"].ToString().Trim() + "'");
if (dt.Rows.Count > )
{
this.dg_WelfaresDetails.Rows.Add();
this.dg_WelfaresDetails.Rows[dgRows].Cells["账户ID"].Value = dt.Rows[]["AccID"].ToString();
this.dg_WelfaresDetails.Rows[dgRows].Cells["账户编号"].Value = ds.Rows[i]["账户编号"].ToString();
this.dg_WelfaresDetails.Rows[dgRows].Cells["账户名称"].Value = dt.Rows[]["cname"].ToString();
this.dg_WelfaresDetails.Rows[dgRows].Cells["身份证号"].Value = dt.Rows[]["cbank"].ToString();
this.dg_WelfaresDetails.Rows[dgRows].Cells["福利金额"].Value = ds.Rows[i]["福利金额"].ToString();
this.dg_WelfaresDetails.Rows[dgRows].Cells["福利备注"].Value = ds.Rows[i]["福利备注"].ToString();
this.dg_WelfaresDetails.Rows[dgRows].Cells["DetailsID"].Value = "";
dgRows++; mJe += Commons.ParseDecimalValue(ds.Rows[i]["福利金额"].ToString());
}
else
{
err++;
Msgs += "Excel中第【" + (i + ).ToString() + "】行," + ds.Rows[i]["账户名称"].ToString().Trim() + "【账户信息有误】,请核查!\r\n";
}
}
labMsgInfo.Text = "总行数:" + this.dg_WelfaresDetails.Rows.Count.ToString() + " " + "总金额:" + mJe.ToString() + " [元]";
if (err == )
{
//允许保存
MessageBoxEx.ShowMessage("浏览Excle中数据成功!", MessageKind.Information);
this.btnDeleteRow.Enabled = true;
this.btnSave.Enabled = true;
}
else
{
//不允许保存
MessageBoxEx.ShowMessage(Msgs, MessageKind.Exclamation);
this.btnSave.Enabled = false;
}
}
#endregion
C# 导入Excel的更多相关文章
- 一步步实现ABAP后台导入EXCEL到数据库【3】
在一步步实现ABAP后台导入EXCEL到数据库[2]里,我们已经实现计划后台作业将数据导入数据库的功能.但是,这只是针对一个简单的自定义结构的导入程序.在实践应用中,面对不同的表.不同的导入文件,我们 ...
- 一步步实现ABAP后台导入EXCEL到数据库【1】
在SAP的应用当中,导入.导出EXCEL文件的情况是一个常见的需求,有时候用户需要将大量数据定期导入到SAP的数据库中.这种情况下,使用导入程序在前台导入可能要花费不少的时间,如果能安排导入程序为后台 ...
- [Asp.net]常见数据导入Excel,Excel数据导入数据库解决方案,总有一款适合你!
引言 项目中常用到将数据导入Excel,将Excel中的数据导入数据库的功能,曾经也查找过相关的内容,将曾经用过的方案总结一下. 方案一 NPOI NPOI 是 POI 项目的 .NET 版本.POI ...
- IE8下导入EXCEL数据传到客户端以附件下载
IE8下导入EXCEL数据传到客户端以附件下载方式出现,而不显示数据,解决方法:以text/html格式返回. HttpResponseMessage message = new HttpRespon ...
- java的poi技术读取和导入Excel
项目结构: http://www.cnblogs.com/hongten/gallery/image/111987.html 用到的Excel文件: http://www.cnblogs.com/h ...
- 报表工具如何实现多次导入Excel
很多人在开发报表的时候会遇到将多张表样相同的excel导入到模板,然后提交至数据库中.但问题是很多情况,在线导入不支持一次性选择多个excel,一次只能选择一个excel,也不能将多个excel中的数 ...
- .NET导入Excel到SQL数据库
在我们开发各类应用型系统,经常会遇到导入导出Excel,为什么会用到他呢?企业或者单位在从无信息化到信息化的一个转变过程.在没有信息化的企业或单位之前,一般都采用Excel来记录相应的数据,做统计计算 ...
- SQL Server服务器上需要导入Excel数据的必要条件
SQL Server服务器上需要导入Excel数据,必须安装2007 Office system 驱动程序:数据连接组件,或者Access2010的数据库引擎可再发行程序包,这样就不必在服务器上装Ex ...
- 导入excel数据到数据库
1.上传excel到服务器 jsp页面代码 <form action="actionname" method="post" id="form1& ...
- PLSQL Developer导入Excel数据
LSQL Developer导入Excel数据 最近处理将Excel数据导入Oracle的工作比较多.之前都是采用Sqlldr命令行导入的方式处理.每次导入不同格式的Excel表数据,都需要先把Exc ...
随机推荐
- linux安装卸载MySQL以及密码设置+Hive测试
linux系统卸载MYSQL 1,先通过yum方式卸载mysql及相关组件 命令:yum remove mysql* 2.通过命令:rpm -qa|grep -i mysql 查找系统的有关于mysq ...
- HDU4305:Lightning(生成树计数+判断点是否在线段上)
Lightning Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total S ...
- POJ1679:The Unique MST(最小生成树)
The Unique MST Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 38430 Accepted: 14045 ...
- [nginx]nginx rewrite规则之last和break
c俺靠这篇博文 http://eyesmore.iteye.com/blog/1142162 有用的配置: 1.开启rewrite_log,这样在/var/log/nginx/error.log中显示 ...
- Qt ------- QMap和QHash的区别
基本概念: QMap提供了一个从类项为key的键到类项为T的直的映射,通常所存储的数据类型是一个键对应一个值,并且按照Key的次序存储数据.同时这个类也支持一键多值的情况,用类QMultiMap可以实 ...
- spring boot 在IDEA控制台中打印彩色日志
只需要在application.properties中加入 spring.output.ansi.enabled=ALWAYS 即可
- [LeetCode] 8. String to Integer (atoi) ☆
Implement atoi to convert a string to an integer. Hint: Carefully consider all possible input cases. ...
- C# 后台获取请求来源、文件下载
文件流下载文件 void BigFileDownload() { try { string FileName = "测试.docx"; string filePath = Page ...
- MongoDB入门(4)- MongoDB日常操作
MongoDB客户端 MongoDB有很多客户端 MongoVue Robomongo MongoDB命令行 启动mongo shell 在windows下,双击mongo.exe可以启动mongo ...
- 【JSP EL】EL表达式获取当前时间(两种方式)
第一种方式: //先在代码段定义<% long date = new Date().getTime(); request.setAttribute("date", date) ...