Created
May 19, 2016 07:32
-
-
Save johnsonz/1f9dc0f79d4d005a80cd5b1716c76627 to your computer and use it in GitHub Desktop.
C#使用NPOI操作excel文件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private DataTable GetAllShowMobileFromExcel(string path) | |
{ | |
DataTable dt = new DataTable(); | |
dt.Columns.Add("Mobile",typeof(string)); | |
DataRow dr = null; | |
IWorkbook wb = null; | |
using (FileStream fs = File.OpenRead(path)) | |
{ | |
wb = WorkbookFactory.Create(fs); | |
} | |
ISheet sheet = wb.GetSheetAt(0); | |
IRow headerRow = sheet.GetRow(sheet.FirstRowNum); | |
for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) | |
{ | |
IRow row = sheet.GetRow(i); | |
if (row != null) | |
{ | |
ICell cell = row.GetCell(0); | |
cell.SetCellType(CellType.String); | |
if (cell != null && cell.StringCellValue != string.Empty) | |
{ | |
dr = dt.NewRow(); | |
dr[0] = cell.StringCellValue; | |
dt.Rows.Add(dr); | |
} | |
} | |
} | |
return dt; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment