日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

Winform中使用NPOI实现Excel导入并赋值给DataTable

發(fā)布時(shí)間:2025/3/19 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Winform中使用NPOI实现Excel导入并赋值给DataTable 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

場(chǎng)景

首先打開一個(gè)excel文件,然后獲取其路徑,獲取第一行作為DataTable的標(biāo)題欄。

然后從第二行到最后一行作為顯示的數(shù)據(jù)。

參考Excel的導(dǎo)出

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100063457

NPOI相關(guān)Lib中各種dll文件下載

https://download.csdn.net/download/badao_liumang_qizhi/11608830

示例代碼下載

https://download.csdn.net/download/badao_liumang_qizhi/11612098

實(shí)現(xiàn)

在上面進(jìn)行Excel的導(dǎo)出時(shí),只添加了NPOI的引用,在進(jìn)行導(dǎo)入時(shí)還要添加如下引用。

?

然后添加一個(gè)按鈕,用來打開要導(dǎo)入的excel文件。

在其點(diǎn)擊事件中

private void button3_Click(object sender, EventArgs e){OpenFileDialog fileDialog = new OpenFileDialog();fileDialog.Multiselect = true;fileDialog.Title = "請(qǐng)選擇文件";fileDialog.Filter = "所有文件(*xls*)|*.xls*"; //設(shè)置要選擇的文件的類型if (fileDialog.ShowDialog() == DialogResult.OK){localFilePath = fileDialog.FileName;//返回文件的完整路徑???????????????}MessageBox.Show("打開成功要導(dǎo)入的文件路徑為:"+localFilePath);}

其中l(wèi)ocalFilePath 是全局變量用來存儲(chǔ)要導(dǎo)入的文件路徑。

string localFilePath = "";

要導(dǎo)入的文件內(nèi)容

?

效果

?

然后在下面導(dǎo)入Excel的按鈕的點(diǎn)擊事件中

private void button2_Click(object sender, EventArgs e){ISheet sheet = null;DataTable data = new DataTable();IWorkbook workbook = null;int startRow = 0;try{//獲取文件名,不帶路徑string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//以文件流的形式打開文件fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read);// 2007版本if (fileNameExt.IndexOf(".xlsx") > 0)workbook = new XSSFWorkbook(fs);// 2003版本else if (fileNameExt.IndexOf(".xls") > 0)workbook = new HSSFWorkbook(fs);if (fileNameExt != null){sheet = workbook.GetSheet("sheet1");//如果沒有找到指定的sheetName對(duì)應(yīng)的sheet,則嘗試獲取第一個(gè)sheetif (sheet == null){sheet = workbook.GetSheetAt(0);}}else{sheet = workbook.GetSheetAt(0);}if (sheet != null){IRow firstRow = sheet.GetRow(0);//第一行最后一個(gè)cell的編號(hào) 即第一行總的列數(shù)int cellCount = firstRow.LastCellNum;for (int i = firstRow.FirstCellNum; i < cellCount; ++i){ICell cell = firstRow.GetCell(i);if (cell != null){string cellValue = cell.StringCellValue;if (!string.IsNullOrEmpty(cellValue)){//設(shè)置DataTable的第一行的顯示內(nèi)容即標(biāo)題行DataColumn column = new DataColumn(cellValue);data.Columns.Add(column);}}}//excel表格中的第一行作為dataTable的標(biāo)題//所以從第二行開始startRow = sheet.FirstRowNum+1;//獲取最后一行的標(biāo)號(hào)int rowCount = sheet.LastRowNum;for (int i = startRow; i <= rowCount; ++i){IRow row = sheet.GetRow(i);//沒有數(shù)據(jù)的行默認(rèn)是null  if (row == null) continue;      DataRow dataRow = data.NewRow();for (int j = row.FirstCellNum; j < cellCount; ++j){//同理,沒有數(shù)據(jù)的單元格都默認(rèn)是nullif (row.GetCell(j) != null)dataRow[j] = row.GetCell(j).ToString();}data.Rows.Add(dataRow);}}}catch (Exception ex){Console.WriteLine("Exception: " + ex.Message);}//設(shè)置數(shù)據(jù)源this.dataGridView1.DataSource = data;}

效果

?

?

總結(jié)

以上是生活随笔為你收集整理的Winform中使用NPOI实现Excel导入并赋值给DataTable的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。