.net excel利用NPOI导入oracle
2015-07-29 17:53
489 查看
1.链接数据库
引用System.Data.OracleClient;
//数据库链接字符串 Data Source如:192.168.5.153:1521/orcl
string linkStr = "User ID=" + name + "; Password=" + password + "; Data Source=" + oraLink;
OracleConnection oraCon = new OracleConnection(linkStr);
oraCon.Open();
2.利用NPOI读取excel数据
excelHelper类
3.插入数据库
--利用openFileDialog读取文件名
--代码如下:
openFileDialog1.Filter = "(*.xls)|*.xls";
openFileDialog1.ShowDialog();
this.textBox1.Text = openFileDialog1.FileName;
--将数据插入oracle数据库
using(ExcelHelper excel = new ExcelHelper(this.textBox1.Text))
{
DataTable dbdata = excel.ExcelToDataTable(null, true); //调用excelHelper中的函数
for (int i = 0; i < dbdata.Rows.Count; i++) //解析数据
{
string XMMC = dbdata.Rows[i][0].ToString();
string sql ="insert into XXX(xmmc) values ('"+XMMC +"')";
OracleCommand cmd = new OracleCommand(sql, oraCon);
OracleDataReader reader = cmd.ExecuteNonQuery();//增删改
/* 判断数据是否存在
OracleCommand cmd = new OracleCommand(sql1, oraCon);
OracleDataReader reader = cmd.ExecuteReader();
if(reader.Read()){}
*/
}
}
引用System.Data.OracleClient;
//数据库链接字符串 Data Source如:192.168.5.153:1521/orcl
string linkStr = "User ID=" + name + "; Password=" + password + "; Data Source=" + oraLink;
OracleConnection oraCon = new OracleConnection(linkStr);
oraCon.Open();
2.利用NPOI读取excel数据
public class ExcelHelper:IDisposable { private string fileName = null; //文件名 private IWorkbook workbook = null; private FileStream fs = null; private bool disposed; public ExcelHelper(string fileName) { this.fileName = fileName; disposed = false; } /// <summary> /// 将Excel导入数据库 /// </summary> /// <param name="sheetName"></param> /// <param name="isFirstRowColumn"></param> /// <returns></returns> public DataTable ExcelToDataTable(string sheetName, bool isFirstRowColumn) { ISheet sheet = null; DataTable data = new DataTable(); int startRow = 0; try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 workbook = new HSSFWorkbook(fs); else if (fileName.IndexOf(".xls") > 0) // 2003版本 workbook = new HSSFWorkbook(fs); if (sheetName != null) { sheet = workbook.GetSheet(sheetName); if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet { sheet = workbook.GetSheetAt(0); } } else { sheet = workbook.GetSheetAt(0); } if (sheet != null) { IRow firstRow = sheet.GetRow(0); int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数 if (isFirstRowColumn) { for (int i = firstRow.FirstCellNum; i < cellCount; ++i) { ICell cell = firstRow.GetCell(i); if (cell != null) { string cellValue = cell.StringCellValue; if (cellValue != null) { DataColumn column = new DataColumn(cellValue); data.Columns.Add(column); } } } startRow = sheet.FirstRowNum + 1; } else { startRow = sheet.FirstRowNum; } //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = startRow; i <= rowCount; ++i) { IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null dataRow[j] = row.GetCell(j).ToString(); } data.Rows.Add(dataRow); } } return data; } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); return null; } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { if (fs != null) fs.Close(); } fs = null; disposed = true; } }
excelHelper类
3.插入数据库
--利用openFileDialog读取文件名
--代码如下:
openFileDialog1.Filter = "(*.xls)|*.xls";
openFileDialog1.ShowDialog();
this.textBox1.Text = openFileDialog1.FileName;
--将数据插入oracle数据库
using(ExcelHelper excel = new ExcelHelper(this.textBox1.Text))
{
DataTable dbdata = excel.ExcelToDataTable(null, true); //调用excelHelper中的函数
for (int i = 0; i < dbdata.Rows.Count; i++) //解析数据
{
string XMMC = dbdata.Rows[i][0].ToString();
string sql ="insert into XXX(xmmc) values ('"+XMMC +"')";
OracleCommand cmd = new OracleCommand(sql, oraCon);
OracleDataReader reader = cmd.ExecuteNonQuery();//增删改
/* 判断数据是否存在
OracleCommand cmd = new OracleCommand(sql1, oraCon);
OracleDataReader reader = cmd.ExecuteReader();
if(reader.Read()){}
*/
}
}
相关文章推荐
- oracle数据同步方案
- oracle表查询(5)
- oracle表管理(4)
- oracle数据类型(3)
- oracle数据库常用plsql语句
- ORACLE Initialization or shutdown in progress
- Oracle 查看收集统计信息
- ORACLE中如何查看CURRENT_SCHEMA
- oracle用户权限操作(2)
- ORACLE 存储过程 循环表操作
- mysql实现oracle sequence功能
- Oracle存储过程
- ORACLE中Merge into使用理解
- 【原】Oracle查询指定表里的触发器
- oracle入门命令(1)
- ORACLE 毫秒变换为日期 日期转换毫秒
- oracle 中如何获取本机ip地址,根据IP地址获取域名?
- oracle笔记之管理员
- oracle 误删数据
- oracle——表修改语句集合