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;
using System.Data.SqlClient;
The namespace WindowsFormsApplication1
{
Public partial class Form1: Form
{
Public _click ()
{
InitializeComponent ();
}
DataTable dt=new DataTable();
Private void btnSure_Click (object sender, EventArgs e)
{
OpenFileDialog fd=new OpenFileDialog ();//first of all, according to the open file dialog box, select the excel spreadsheet
Fd. Filter="table | *. XLSX; *. XLS ";//open file dialog box filter
String strPath;//file the full path name
If (fd) ShowDialog ()==DialogResult. OK)
{
Try
{
StrPath=fd. The FileName;
String strCon="Provider=Microsoft. Jet. The OLEDB. 4.0; Data Source="+ strPath +"; Extended Properties='Excel 12.0; HDR=YES; IMEX=1 '";//can't be 11.0
OleDbConnection Con=new OleDbConnection (strCon);//connect
String strSql="select * from [Sheet1 $]".//table name spelled should also pay attention to the different, corresponding to the excel table to sheet1, here to add a dollar sign $, after using brackets
OleDbCommand Cmd=new OleDbCommand (strSql, Con);//set up to implement the command
OleDbDataAdapter da=new OleDbDataAdapter (Cmd);//set up data adapter
DataSet ds=new DataSet();//the new data set
da.Fill(ds);//the data adapter to read data set of data in a table (table named shyman here, can take the table name)
Ds//the data source at the specified datagridview1 dataset of the first Table (namely shyman Table), can also write the ds. Table [" shyman "]
DataGridView1. The DataSource=ds. Tables [0];
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);//the catching
}
}
}
Private void btnQuit_Click (object sender, EventArgs e)
{
Application. The Exit ();
}
}
}
CodePudding user response:
Simply read content, if you have the OFFICE environment, using the object model is the most convenient option scheme; If cannot guarantee have OFFICE environment, you can try the expansion of the third party, like EPPlus NPOI all of these can be used out of the OFFICE environment,CodePudding user response:
Hello, don't know much about what you have said, I am a novice, using the object model is what? What is the guarantee of OFFICE environment?CodePudding user response:
C # using the EXCEL object model operation, advantages: Microsoft's official plan, good compatibility, the use of object model more accord with human visual perception, it cost is extremely low, if itself with VBA, the cost could have a big discount; Disadvantages: need to install Office computer,https://blog.csdn.net/weixin_42764060/article/details/90145351
Official of another is this: you send the OLEDB database engine to read and write EXCEL, faults are very complicated, and need to have a database driver (because I've never used this kind of solution, so don't know the driver is to provide by who, if provided by Office, just need to install Office computer),
===========================
Compatibility with other third-party scheme, especially in the mixture of WPS and Office document for editing, error easily, and some may be charge,
C # use excel EPPlus read, advantages: using habit is close to the Office object model,
https://blog.csdn.net/birdfly2015/article/details/90607323
C # using NPOI to read and write EXCEL,
https://blog.csdn.net/vblegend_2013/article/details/79483391
Besides, there are Spire. XLS, this used, inconvenient,
===============
Not only Excel, Word can use a similar scheme for procedural treatment,