C# 连接 读取 Excel
来源:互联网 发布:代办各种假证淘宝 编辑:程序博客网 时间:2024/04/30 00:53
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;
}
}
}
- C# 连接 读取 Excel
- C#读取不同版本excel的连接字符串
- C#读取Excel,导出Excel
- C#读取Excel,导出Excel
- C#读取excel表
- C#读取Excel数据
- C#读取Excel数据
- C#读取Excel数据
- C#读取Excel数据
- C#读取Excel数据
- C#读取Excel数据
- C#读取Excel数据
- C# 读取excel表
- C#读取Excel数据
- 读取excel文件 C#
- C#读取Excel数据
- c#读取excel文件
- C# 读取Excel文件
- 利用Apache AXIS 1 发布WebService
- Bone Collector
- android开启手机虚拟键的菜单键
- /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.11' not found的问题
- 利用redis实现tomcat session共享
- C# 连接 读取 Excel
- 移动互联网APP产品设计
- VMware安装Redhat9
- tmg增加 ssl端口
- 使用GridView实现九宫格布局
- imwrite()函数
- DEV控件之ChartControl用法
- Coarunsoft Code Generation代码生成工具
- js Map和Set