Skip to content

Instantly share code, notes, and snippets.

@johnsonz
Created May 19, 2016 07:32
Show Gist options
  • Save johnsonz/1f9dc0f79d4d005a80cd5b1716c76627 to your computer and use it in GitHub Desktop.
Save johnsonz/1f9dc0f79d4d005a80cd5b1716c76627 to your computer and use it in GitHub Desktop.
C#使用NPOI操作excel文件
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