一. 读取excel文件
下面这种方法是按照操作数据库的方法来读取xls文件, 用sql语句查询sheet1中的内容并赋给dataGridView
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace readEXCEL
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
dataGridView1.DataSource = null; //每次打开清空内容
DataSet dataSet = getData();
if (dataSet != null)
{
DataTable dt = dataSet.Tables[0];
RemoveEmpty(dt);
dataGridView1.DataSource = dt;
}
}
public DataSet getData()
{
//打开文件
OpenFileDialog file = new OpenFileDialog();
//file.Filter = "Excel(*.xlsx)|*.xlsx|Excel(*.xls)|*.xls";
file.Filter = "Excel文件 |*.xlsx;*.xls";
file.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
file.Multiselect = false;
if (file.ShowDialog() == DialogResult.Cancel)
return null;
//判断文件后缀
var path = file.FileName;
textBox1.Text = path;
string fileSuffix = System.IO.Path.GetExtension(path);
if (string.IsNullOrEmpty(fileSuffix))
return null;
using (DataSet ds = new DataSet())
{
//判断Excel文件是2003版本还是2007版本
string connString = "";
if (fileSuffix == ".xls")
connString = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + path + ";" + ";Extended Properties=\"Excel 8.0;HDR=YES;IMEX=1\"";
else