码迷,mamicode.com
首页 > 其他好文 > 详细

NPOI DataTable转Excel ,Excel转DataTable

时间:2017-09-05 15:44:33      阅读:173      评论:0      收藏:0      [点我收藏+]

标签:else   getc   short   memory   head   str   line   read   value   

DataTableToExcel:

 1  public static void dataTableToExcel(DataTable table, string fileName)
 2         {
 3             using (MemoryStream ms = new MemoryStream())
 4             {
 5 
 6                 IWorkbook workbook = null;
 7 
 8                 if (fileName.IndexOf(".xlsx") > 0)
 9                     workbook = new XSSFWorkbook();
10                 if (fileName.IndexOf(".xls") > 0)
11                     workbook = new HSSFWorkbook();
12                 ISheet sheet = workbook.CreateSheet();
13                 IRow headerRow = sheet.CreateRow(0);
14 
15                 // handling header.  
16                 foreach (DataColumn column in table.Columns)
17                     headerRow.CreateCell(column.Ordinal).SetCellValue(column.Caption);//If Caption not set, returns the ColumnName value  
18 
19                 // handling value.  
20                 int rowIndex = 1;
21 
22                 foreach (DataRow row in table.Rows)
23                 {
24                     IRow dataRow = sheet.CreateRow(rowIndex);
25 
26                     foreach (DataColumn column in table.Columns)
27                     {
28                         dataRow.CreateCell(column.Ordinal).SetCellValue(row[column].ToString());
29                     }
30 
31                     rowIndex++;
32                 }
33 
34                 workbook.Write(ms);
35                 ms.Flush();
36 
37                 using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write))
38                 {
39                     byte[] data = ms.ToArray();
40 
41                     fs.Write(data, 0, data.Length);
42                     fs.Flush();
43 
44                     data = null;
45                 }
46             }
47         }

ExcelToDataTable:

  1    public static DataTable ExcelToDataTable(string filePath, bool isColumnName,int sheetName)
  2         {
  3             DataTable dataTable = null;
  4             FileStream fs = null;
  5             DataColumn column = null;
  6             DataRow dataRow = null;
  7             IWorkbook workbook = null;
  8             ISheet sheet = null;
  9             IRow row = null;
 10             ICell cell = null;
 11             int startRow = 0;
 12             try
 13             {
 14                 using (fs = File.OpenRead(filePath))
 15                 {
 16                     // 2007版本
 17                     if (filePath.IndexOf(".xlsx") > 0)
 18                         workbook = new XSSFWorkbook(fs);
 19                     // 2003版本
 20                     else if (filePath.IndexOf(".xls") > 0)
 21                         workbook = new HSSFWorkbook(fs);
 22 
 23                     if (workbook != null)
 24                     {
 25                         sheet = workbook.GetSheetAt(sheetName);//读取第一个sheet,当然也可以循环读取每个sheet
 26                         dataTable = new DataTable();
 27                         if (sheet != null)
 28                         {
 29                             int rowCount = sheet.LastRowNum;//总行数
 30                             if (rowCount > 0)
 31                             {
 32                                 IRow firstRow = sheet.GetRow(0);//第一行
 33                                 int cellCount = firstRow.LastCellNum;//列数
 34 
 35                                 //构建datatable的列
 36                                 if (isColumnName)
 37                                 {
 38                                     startRow = 1;//如果第一行是列名,则从第二行开始读取
 39                                     for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
 40                                     {
 41                                         cell = firstRow.GetCell(i);
 42                                         if (cell != null)
 43                                         {
 44                                             if (cell.StringCellValue != null)
 45                                             {
 46                                                 column = new DataColumn(cell.StringCellValue);
 47                                                 dataTable.Columns.Add(column);
 48                                             }
 49                                         }
 50                                     }
 51                                 }
 52                                 else
 53                                 {
 54                                     for (int i = firstRow.FirstCellNum; i < cellCount; ++i)
 55                                     {
 56                                         column = new DataColumn("column" + (i + 1));
 57                                         dataTable.Columns.Add(column);
 58                                     }
 59                                 }
 60 
 61                                 //填充行
 62                                 for (int i = startRow; i <= rowCount; ++i)
 63                                 {
 64                                     row = sheet.GetRow(i);
 65                                     if (row == null) continue;
 66 
 67                                     dataRow = dataTable.NewRow();
 68                                     for (int j = row.FirstCellNum; j < cellCount; ++j)
 69                                     {
 70                                         cell = row.GetCell(j);
 71                                         if (cell == null)
 72                                         {
 73                                             dataRow[j] = "";
 74                                         }
 75                                         else
 76                                         {
 77                                             //CellType(Unknown = -1,Numeric = 0,String = 1,Formula = 2,Blank = 3,Boolean = 4,Error = 5,)
 78                                             switch (cell.CellType)
 79                                             {
 80                                                 case CellType.Blank:
 81                                                     dataRow[j] = "";
 82                                                     break;
 83                                                 case CellType.Numeric:
 84                                                     short format = cell.CellStyle.DataFormat;
 85                                                     //对时间格式(2015.12.5、2015/12/5、2015-12-5等)的处理
 86                                                     if (format == 14 || format == 31 || format == 57 || format == 58)
 87                                                         dataRow[j] = cell.DateCellValue;
 88                                                     else
 89                                                         dataRow[j] = cell.NumericCellValue;
 90                                                     break;
 91                                                 case CellType.String:
 92                                                     dataRow[j] = cell.StringCellValue;
 93                                                     break;
 94                                             }
 95                                         }
 96                                     }
 97                                     dataTable.Rows.Add(dataRow);
 98                                 }
 99                             }
100                         }
101                     }
102                 }
103                 return dataTable;
104             }
105             catch (Exception ex)
106             {
107                 Console.WriteLine("exception:" + ex.ToString());
108                 Console.ReadLine();
109                 if (fs != null)
110                 {
111                     fs.Close();
112                 }
113                 return null;
114             }
115         }

 

NPOI DataTable转Excel ,Excel转DataTable

标签:else   getc   short   memory   head   str   line   read   value   

原文地址:http://www.cnblogs.com/kejie/p/7478455.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!