使用NPOI组件读取excel数据
2012-12-21 15:11
417 查看
分享一段代码,将excel数据读取到DataTable:
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Linq; using System.Text; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; namespace ConsoleApp.Utility { public class ExcelBuilder { /// <summary> /// exact excel data into DataTable /// </summary> /// <param name="excel">excel file name</param> /// <param name="index">sheet index </param> /// <param name="header"> the first row in excel whether belongs the columns</param> /// <returns>DataTable</returns> public static DataTable ToDataTable(string excel,int index, bool header) { DataTable dt = new DataTable(Path.GetFileNameWithoutExtension(excel) + "_Sheet" + index); IWorkbook workbook; using (FileStream file = new FileStream(excel, FileMode.Open, FileAccess.Read)) { workbook = new XSSFWorkbook(file); } ISheet sheet = workbook.GetSheetAt(index); var rows = sheet.GetRowEnumerator(); rows.MoveNext(); IRow row = (XSSFRow)rows.Current; for (int i = 0; i < row.LastCellNum; i++) { ICell cell = row.GetCell(i); string columnName = header ? cell.StringCellValue : i.ToString(); dt.Columns.Add(columnName, typeof(string)); } if (!header) { DataRow first = dt.NewRow(); for (int i = 0; i < row.LastCellNum; i++) { ICell cell = row.GetCell(i); first[i] = cell.StringCellValue; } dt.Rows.Add(first); } while (rows.MoveNext()) { row = (XSSFRow)rows.Current; DataRow dataRow = dt.NewRow(); for (int i = 0; i < row.LastCellNum; i++) { ICell cell = row.GetCell(i); dataRow[i] = cell.StringCellValue; } dt.Rows.Add(dataRow); } return dt; } } }
.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
相关文章推荐
- NET使用NPOI组件将数据导出Excel
- .NET使用NPOI组件将数据导出Excel
- Java使用jacob组件读取大量Excel数据,性能差的问题
- NPOI 2.1.1 系列(2) 使用NPOI读取List或者datatable数据生成 Excel文档 ;Npoi生成 xlsx 2007以上文档
- 【推荐】.NET(webform,MVC)使用NPOI组件将数据导出、导入Excel。 Aspose.Cell 控件报表
- .NET使用NPOI组件将数据导出Excel
- .NET使用NPOI组件将数据导出Excel
- NET使用NPOI组件将数据导出Excel-通用方法 【推荐】
- NET使用NPOI组件将数据导出Excel-通用方法 【推荐】
- asp.net使用组件koogra读取Excel数据批量上传导入
- 【推荐】.NET使用NPOI组件将数据导出Excel
- NET使用NPOI组件将数据导出Excel [转]
- .NET-使用NPOI组件将数据导出Excel-通用方法
- 使用NPOI插件读取excel模版修改数据后保存到新目录新文件中
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- NET使用NPOI组件将数据导出Excel
- DataTable to Excel(使用NPOI、EPPlus将数据表中的数据读取到excel格式内存中)
- C#调用NPOI组件读取excel表格数据转为datatable写入word表格中并向word中插入图片/文字/书签 获得书签列表
- NET使用NPOI组件将数据导出Excel
- 使用NPOI读取Excel数据到DataTable