.net excel利用NPOI导入oracle

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数据

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 = ;
try
{
fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
if (fileName.IndexOf(".xlsx") > ) // 2007版本
workbook = new HSSFWorkbook(fs);
else if (fileName.IndexOf(".xls") > ) // 2003版本
workbook = new HSSFWorkbook(fs); if (sheetName != null)
{
sheet = workbook.GetSheet(sheetName);
if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet
{
sheet = workbook.GetSheetAt();
}
}
else
{
sheet = workbook.GetSheetAt();
}
if (sheet != null)
{
IRow firstRow = sheet.GetRow();
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 + ;
}
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()){}

*/

}

}

上一篇:Python基础 - 迭代


下一篇:Cannot read property 'validate' of undefined