C# 连接 读取 Excel
2015-08-27 16:26
417 查看
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows;
using System.Windows.Forms;
namespace MyProjectDAL
{
public class ExcelDB
{
private OleDbConnection connection, connWithoutHDR;
private string connectionString, connStringWithoutHDR;
//构造函数
public ExcelDB(string strExcelFileName)
{
string path = Application.ExecutablePath;
FileInfo exeInfo = new FileInfo(path);
path = exeInfo.DirectoryName + "\\";
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + "user data\\excel files\\" + strExcelFileName + ".xls;" + "Extended Properties='Excel 5.0;HDR=YES;'";
connStringWithoutHDR = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + "user data\\excel files\\" + strExcelFileName + ".xls;" + "Extended Properties='Excel 5.0;HDR=NO;'";
connection = new OleDbConnection(connectionString);
connWithoutHDR = new OleDbConnection(connStringWithoutHDR);
}
//将excel文件内容读入DataTable
public DataTable Excel2DataTable(int startLine, int endLine, string columns)
{
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
int readCount = 0;
int i;
try
{
string query = "select " + columns + " from [Sheet1$]";
this.connWithoutHDR.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, connWithoutHDR);
adapter.Fill(dt1);
connWithoutHDR.Close();
}
catch (OleDbException ex)
{
MessageBox.Show("Excel数据读取失败 ErrorCode: " + (ex.ErrorCode).ToString());
}
readCount = dt1.Rows.Count;
if (readCount > endLine)
{
for (i = startLine - 1; i < endLine; i++)
{
dt2.Rows.Add(dt1.Rows[i]);
}
}
if (readCount >= startLine && readCount <= endLine)
{
for (i = startLine - 1; i < readCount; i++)
{
dt2.Rows.Add(dt1.Rows[i]);
}
}
return dt2;
}
}
}
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Windows;
using System.Windows.Forms;
namespace MyProjectDAL
{
public class ExcelDB
{
private OleDbConnection connection, connWithoutHDR;
private string connectionString, connStringWithoutHDR;
//构造函数
public ExcelDB(string strExcelFileName)
{
string path = Application.ExecutablePath;
FileInfo exeInfo = new FileInfo(path);
path = exeInfo.DirectoryName + "\\";
connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + "user data\\excel files\\" + strExcelFileName + ".xls;" + "Extended Properties='Excel 5.0;HDR=YES;'";
connStringWithoutHDR = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + "user data\\excel files\\" + strExcelFileName + ".xls;" + "Extended Properties='Excel 5.0;HDR=NO;'";
connection = new OleDbConnection(connectionString);
connWithoutHDR = new OleDbConnection(connStringWithoutHDR);
}
//将excel文件内容读入DataTable
public DataTable Excel2DataTable(int startLine, int endLine, string columns)
{
DataTable dt1 = new DataTable();
DataTable dt2 = new DataTable();
int readCount = 0;
int i;
try
{
string query = "select " + columns + " from [Sheet1$]";
this.connWithoutHDR.Open();
OleDbDataAdapter adapter = new OleDbDataAdapter(query, connWithoutHDR);
adapter.Fill(dt1);
connWithoutHDR.Close();
}
catch (OleDbException ex)
{
MessageBox.Show("Excel数据读取失败 ErrorCode: " + (ex.ErrorCode).ToString());
}
readCount = dt1.Rows.Count;
if (readCount > endLine)
{
for (i = startLine - 1; i < endLine; i++)
{
dt2.Rows.Add(dt1.Rows[i]);
}
}
if (readCount >= startLine && readCount <= endLine)
{
for (i = startLine - 1; i < readCount; i++)
{
dt2.Rows.Add(dt1.Rows[i]);
}
}
return dt2;
}
}
}
相关文章推荐
- 使用Python生成Excel格式的图片
- c#调用COM组件
- Excel 曝出 Power Query 安全漏洞,1.2 亿用户易受远程 DDE 攻击
- excel vba 限制工作表的滚动区域代码
- excel vba 高亮显示当前行代码
- C#实现把指定数据写入串口
- C#动态创建button的方法
- C#中抽象方法与虚拟方法的区别
- c#中虚函数的相关使用方法
- C#使用加边法计算行列式的值
- C#实现多线程的同步方法实例分析
- C#中尾递归的使用、优化及编译器优化
- C#通用邮件发送类分享
- C#中this的用法集锦
- C#.NET获取拨号连接的宽带连接方法
- C#实现AddRange为数组添加多个元素的方法
- C#中Equality和Identity浅析
- C#生成饼形图及添加文字说明实例代码
- C#判等对象是否相等的方法汇总
- C#简单的向量用法实例教程