资源简介
读取Excel文件到DataGridView,同时支持Excel2003和Excel2007。Excel的数据列是自适应的。
第一行需要写列名。
代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace ExcelDemo
{
public partial class Form1 : Form
{
DataTable dt_Excel;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender EventArgs e)
{
OpenFileDialog openExcelFile = new OpenFileDialog();
openExcelFile.Filter = “excel file (*.xls)|*.xls|All files (*.*)|*.*“;
openExcelFile.RestoreDirectory = true;
if (openExcelFile.ShowDialog() == DialogResult.OK)
{
textBox1.Text = openExcelFile.FileName;
}
}
private DataSet GetExcelData(string str)
{
string strCon;
if (radioButton1.Checked)
{
//Excel 2003
strCon = “ Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =“ + str + “;Extended Properties=Excel 8.0“;
}
else
{
//Excel 2007
strCon = “Provider=Microsoft.ACE.OLEDB.12.0;Data Source=“ + str + “;Extended Properties=\“Excel 12.0;HDR=YES\““;
}
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = “ SELECT * FROM [Sheet1$]“;
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom myConn);
DataSet myDataSet = new DataSet();
myCommand.Fill(myDataSet “[Sheet1$]“);
myConn.Close();
return myDataSet;
}
private void button2_Click(object sender EventArgs e)
{
DataSet ds_Excel = GetExcelData(textBox1.Text);
dt_Excel = ds_Excel.Tables[0];
dataGridView1.DataSource = dt_Excel;
}
}
}
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2015-02-06 10:54 ExcelDemo
目录 0 2015-02-06 10:55 ExcelDemo\ExcelDemo
目录 0 2015-02-06 10:54 ExcelDemo\ExcelDemo\bin
目录 0 2015-02-06 10:56 ExcelDemo\ExcelDemo\bin\Debug
文件 11264 2015-02-06 11:02 ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.exe
文件 26112 2015-02-06 11:02 ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.pdb
文件 11600 2015-02-06 11:03 ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.vshost.exe
文件 490 2010-03-17 22:39 ExcelDemo\ExcelDemo\bin\Debug\ExcelDemo.vshost.exe.manifest
文件 3677 2015-02-06 10:55 ExcelDemo\ExcelDemo\ExcelDemo.csproj
文件 2030 2015-02-06 11:02 ExcelDemo\ExcelDemo\Form1.cs
文件 5666 2015-02-06 11:02 ExcelDemo\ExcelDemo\Form1.Designer.cs
文件 5817 2015-02-06 11:02 ExcelDemo\ExcelDemo\Form1.resx
目录 0 2015-02-06 10:54 ExcelDemo\ExcelDemo\obj
目录 0 2015-02-06 10:54 ExcelDemo\ExcelDemo\obj\x86
目录 0 2015-02-06 11:02 ExcelDemo\ExcelDemo\obj\x86\Debug
文件 2152 2015-02-06 10:54 ExcelDemo\ExcelDemo\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache
文件 6402 2015-02-06 11:02 ExcelDemo\ExcelDemo\obj\x86\Debug\DesignTimeResolveAssemblyReferencesInput.cache
文件 1249 2015-02-06 11:03 ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.csproj.FileListAbsolute.txt
文件 11264 2015-02-06 11:02 ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.exe
文件 180 2015-02-06 11:02 ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.Form1.resources
文件 26112 2015-02-06 11:02 ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.pdb
文件 180 2015-02-06 10:56 ExcelDemo\ExcelDemo\obj\x86\Debug\ExcelDemo.Properties.Resources.resources
文件 488 2015-02-06 11:02 ExcelDemo\ExcelDemo\obj\x86\Debug\GenerateResource.read.1.tlog
文件 1082 2015-02-06 11:02 ExcelDemo\ExcelDemo\obj\x86\Debug\GenerateResource.write.1.tlog
目录 0 2015-02-06 10:54 ExcelDemo\ExcelDemo\obj\x86\Debug\TempPE
文件 490 2015-02-06 10:54 ExcelDemo\ExcelDemo\Program.cs
目录 0 2015-02-06 10:54 ExcelDemo\ExcelDemo\Properties
文件 1350 2015-02-06 10:54 ExcelDemo\ExcelDemo\Properties\AssemblyInfo.cs
文件 2866 2015-02-06 10:54 ExcelDemo\ExcelDemo\Properties\Resources.Designer.cs
文件 5612 2015-02-06 10:54 ExcelDemo\ExcelDemo\Properties\Resources.resx
............此处省略7个文件信息
- 上一篇:harris角点
- 下一篇:qt 定制省市区三级选择框
相关资源
- talend open studio for big data v6.3.0地址
- js读取本地Excel
- 用于excel的md5宏xla
- 批量合并文件夹下所有文件并以文件
- poi基于Excel模板导入导出注释详细,绝
- EXCEL财务记账系统-傻瓜帐套
- excel办公技巧大全
- HCNA-BigData(H13-711)题库
- Winfrom 嵌入word、excel实现源码
- 真正RTL8812AU的Datasheet,USB3.0,802.11a
- 全国省份城市县整理excel格式,可以按
- labview office报表工具包
- VMwarevSphereDataProtection6.1.6.txt
- Excel工具类
- 仓库管理系统表格Excel模板
- 自己写的一个分页控件,可以实现d
- System.Windows.Forms.DataVisualization.dll 3
- datagridview增加汇总行 源码
- firefox 插件 tamper data 11.1.0
- Dynamic_Data_Race_Detection
- kitti百度云.txt
- 易语言读写excel超链接
- 经纬度求距离excel工具
- js excel表内容导入到页面table中
- 模拟数据生成器 Data-Processer
- word excel 97-03密码破解软件
- 实现TreeView和ListView合体效果的控件
- SAP Data Services如何连接BW
- springboot+spring data jpa+thymeleaf学习web项
- Wine Data Set.rar( 数据挖掘 数据集 )
评论
共有 条评论