C#导入Excel数据的方式(两种)
2014-03-24 14:26
603 查看
/// <summary>
/// 导入数据到数据集中
/// 备注:此种方法只支持excel原文件
/// </summary>
/// <param name="Path">文件路劲</param>
/// <param name="exceptionMsg">异常信息</param>
/// <returns></returns>
public static System.Data.DataTable InputExcel(string Path, ref string exceptionMsg)
{
System.Data.DataTable dt = null;
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
System.Data.DataTable sheetDt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] sheet = new string[sheetDt.Rows.Count];
for (int i = 0; i < sheetDt.Rows.Count; i++)
{
sheet[i] = sheetDt.Rows[i]["TABLE_NAME"].ToString();
}
string strExcel = string.Format("select * from [{0}]", sheet[0]);
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
dt = new System.Data.DataTable();
myCommand.Fill(dt);
conn.Close();
}
}
catch (Exception ex)
{
exceptionMsg = ex.Message;
}
return dt;
}
方法二、读取Excel文件,然后根据里面的数据信息拼装
#region 读取Excel表格中数据到DataTable中
public static System.Data.DataTable ChangeExcelToDateTable(string _path)
{
System.Data.DataTable tempdt = new System.Data.DataTable();
tempdt.TableName = "Excel";
Application app = new Application();
object obj = System.Reflection.Missing.Value;
try
{
Workbook _wBook = app.Workbooks.Open(_path, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj);
Worksheet _wSheet = (Worksheet)_wBook.Worksheets.get_Item(1);
DataRow newRow = null;
DataColumn newColumn = null;
for (int i = 2; i <= _wSheet.UsedRange.Rows.Count; i++)
{
newRow = tempdt.NewRow();
for (int j = 1; j <= _wSheet.UsedRange.Columns.Count; j++)
{
if (i == 2 && j == 1)
{
//表头
for (int k = 1; k <= _wSheet.UsedRange.Columns.Count; k++)
{
string str = (_wSheet.UsedRange[1, k] as Range).Value2.ToString();
newColumn = new DataColumn(str);
newRow.Table.Columns.Add(newColumn);
}
}
Range range = _wSheet.Cells[i, j] as Range;
if (range != null && !"".Equals(range.Text.ToString()))
{
newRow[j - 1] = range.Value2;
}
}
tempdt.Rows.Add(newRow);
}
_wSheet = null;
_wBook = null;
app.Quit();
Kill(app);
int generation = System.GC.GetGeneration(app);
app = null;
System.GC.Collect(generation);
return tempdt;
}
catch (Exception ex)
{
app.Quit();
Kill(app);
int generation = System.GC.GetGeneration(app);
app = null;
throw ex;
}
}
/// 导入数据到数据集中
/// 备注:此种方法只支持excel原文件
/// </summary>
/// <param name="Path">文件路劲</param>
/// <param name="exceptionMsg">异常信息</param>
/// <returns></returns>
public static System.Data.DataTable InputExcel(string Path, ref string exceptionMsg)
{
System.Data.DataTable dt = null;
try
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Path + ";" + "Extended Properties=Excel 8.0;";
using (OleDbConnection conn = new OleDbConnection(strConn))
{
conn.Open();
System.Data.DataTable sheetDt = conn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string[] sheet = new string[sheetDt.Rows.Count];
for (int i = 0; i < sheetDt.Rows.Count; i++)
{
sheet[i] = sheetDt.Rows[i]["TABLE_NAME"].ToString();
}
string strExcel = string.Format("select * from [{0}]", sheet[0]);
OleDbDataAdapter myCommand = new OleDbDataAdapter(strExcel, strConn);
dt = new System.Data.DataTable();
myCommand.Fill(dt);
conn.Close();
}
}
catch (Exception ex)
{
exceptionMsg = ex.Message;
}
return dt;
}
方法二、读取Excel文件,然后根据里面的数据信息拼装
#region 读取Excel表格中数据到DataTable中
public static System.Data.DataTable ChangeExcelToDateTable(string _path)
{
System.Data.DataTable tempdt = new System.Data.DataTable();
tempdt.TableName = "Excel";
Application app = new Application();
object obj = System.Reflection.Missing.Value;
try
{
Workbook _wBook = app.Workbooks.Open(_path, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj, obj);
Worksheet _wSheet = (Worksheet)_wBook.Worksheets.get_Item(1);
DataRow newRow = null;
DataColumn newColumn = null;
for (int i = 2; i <= _wSheet.UsedRange.Rows.Count; i++)
{
newRow = tempdt.NewRow();
for (int j = 1; j <= _wSheet.UsedRange.Columns.Count; j++)
{
if (i == 2 && j == 1)
{
//表头
for (int k = 1; k <= _wSheet.UsedRange.Columns.Count; k++)
{
string str = (_wSheet.UsedRange[1, k] as Range).Value2.ToString();
newColumn = new DataColumn(str);
newRow.Table.Columns.Add(newColumn);
}
}
Range range = _wSheet.Cells[i, j] as Range;
if (range != null && !"".Equals(range.Text.ToString()))
{
newRow[j - 1] = range.Value2;
}
}
tempdt.Rows.Add(newRow);
}
_wSheet = null;
_wBook = null;
app.Quit();
Kill(app);
int generation = System.GC.GetGeneration(app);
app = null;
System.GC.Collect(generation);
return tempdt;
}
catch (Exception ex)
{
app.Quit();
Kill(app);
int generation = System.GC.GetGeneration(app);
app = null;
throw ex;
}
}
相关文章推荐
- C#导出数据到Excel的两种方式:基于COM组件和NPIO
- C#读取Excel数据两种方式性能对比
- C#使用oledb方式将excel数据导入到datagridview后数据被截断为 255 个字符
- C#将Excel数据表导入SQL数据库的两种方法
- C#导出数据到Excel的两种方式:基于COM组件和NPIO
- C#将Excel数据表导入SQL数据库的两种方法(转)
- C#读取Excel数据两种方式性能对比
- C#导入导出Excel数据的两种方法
- C#将Excel数据表导入SQL数据库的两种方法
- C#、.net将Excel数据表导入SQL数据库的两种方法
- C#导入Excel数据时的一个BUG
- C# 操作EXCEL 类 可以实现导入导出功能 折腾了好长时间 现在两种方法显示出来
- C#导入导出数据到Excel的通用类代码
- C#将SQL数据库中数据导入Excel中,并将Excel中反导入SQL数据库中
- 如何提高C#中将数据导入到Excel的性能
- web excel .net 两种导入数据到excel的方法
- C#把表中的数据导入到Excel中
- C#中将数据导出为EXCEL方式汇总
- C# Excel数据导入 关于Excel导入的HDR=YES; IMEX=1详解
- C# 从Excel表中导入数据函数到DataSet