Home > Net >  Read the Excel data imported into a DataSet or the DataTable
Read the Excel data imported into a DataSet or the DataTable

Time:03-09

For c # using NPOI operation way to Excel in the DataGridView how to write code?? Turn to for help, I use the oledb method to get data incomplete

OpenFileDialog ofd=new OpenFileDialog (); One way to//new

//"(*. Et; *. XLS; | * *. XLSX). Et; *. XLS; *. XLSX | | all *. * "-- -- -- -- -- -- -- -- -- -- -- -- -- -- --
if you want to multiple choicesOfd. Filter="(*. XLS; | * *. XLSX). XLS; *. XLSX ";//delete selected, set file display type

Ofd. ShowDialog ();//show the window of the open file
If (ofd. ShowDialog ()==DialogResult. OK)
{
FileName=ofd. FileName;

}
Try
{
//string strCon="Provider=Microsoft. ACE. The OLEDB. 12.0; Data Source="+ fileName +"; Extended Properties='Excel 8.0; HDR=NO. IMEX=1 '";
String strCon="Provider=Microsoft. Jet. The OLEDB. 4.0; Data Source="+ fileName +"; Extended Properties='Excel 8.0; HDR=NO. IMEX=1 '";
Using (OleDbConnection conn=new OleDbConnection (strCon))
{
TextBox1. Text=ofd. FileName;
Conn. The Open ();
DataTable sheetName=conn. GetOleDbSchemaTable (OleDbSchemaGuid. Tables, new object [] {null, null, null, "Table"});
String FirstSheetName=sheetName. Rows [0] [2]. The ToString ();
String SQL=the string. Format (" SELECT * FROM [{0}] ", FirstSheetName);
OleDbDataAdapter Ada=new OleDbDataAdapter (SQL, strCon);

The DataSet characters=new DataSet ();
Ada. The Fill (characters);

DataGridView1. The DataSource=characters. Tables [0];

}
}
The catch (Exception ex)
{
MessageBox. Show (ex. Message);
}

CodePudding user response:

Please put the above code into NPOI reading Excel operation method
  •  Tags:  
  • C#
  • Related