NPOI read the data in Excel

The first is to install the project NPOI.DLL: the Install-Package Penalty for NPOI -Version 2.4.1

 1   HttpPostedFile upLoadPostFile = FileUpload1.PostedFile;
 2                 string upLoadPath = upLoadPostFile.FileName;
 3                 //ExcelToDataTable(upLoadPath);
 4                 string outs="";
 5                 DataSet ds=   ExcelToDataSet(upLoadPath, outs);
 6                 DataTable dtInfo = ds.Tables[0];
 7                 string ss = "";
 8                 for (int i = 0; i < dtInfo.Rows.Count; i++)
 9                 {
10                     //ss = dtInfo.Rows[i].ToString();
11                     ss = dtInfo.Rows[i+1][0].ToString();
12                 }
  1   /// <summary>
  2         /// Excel转换成DataSet(.xlsx/.xls)
  3         /// </summary>
  4         /// <param name="filePath">Excel文件路径</param>
  5         /// <param name="strMsg"></param>
  6         /// <returns></returns>
  7         public static DataSet ExcelToDataSet(string filePath,  string strMsg)
  8         {
  9             strMsg = "";
 10             DataSet ds = new DataSet();
 11             DataTable dt = new DataTable();
 12             string fileType = Path.GetExtension(filePath).ToLower();
 13             string fileName = Path.GetFileName(filePath).ToLower();
 14             try
 15             {
 16                 ISheet sheet = null;
 17                 int sheetNumber = 0;
 18                 FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
 19                 if (fileType == ".xlsx")
 20                 {
 21                     // 2007版本
 22                     XSSFWorkbook workbook = new XSSFWorkbook(fs);
 23                     sheetNumber = workbook.NumberOfSheets;
 24                     for (int i = 0; i < sheetNumber; i++)
 25                     {
 26                         string sheetName = workbook.GetSheetName(i);
 27                         sheet = workbook.GetSheet(sheetName);
 28                         if (sheet != null)
 29                         {
30                              dt = GetSheetDataTable (Sheet, strMsg);
 31 is                              IF (! Dt = null )
 32                              {
 33 is                                  dt.TableName = sheetName.Trim ();
 34 is                                  ds.Tables.Add (dt);
 35                              }
 36                              the else 
37 [                              {
 38 is                                  the MessageBox. show ( " Sheet data acquisition failure reason: " + strMsg);
 39                              }
 40                         }
 41                     }
 42                 }
 43                 else if (fileType == ".xls")
 44                 {
 45                     // 2003版本
 46                     HSSFWorkbook workbook = new HSSFWorkbook(fs);
 47                     sheetNumber = workbook.NumberOfSheets;
 48                     for (int i = 0; i < sheetNumber; i++)
 49                     {
 50                         string sheetName = workbook.GetSheetName(i);
 51                         sheet = workbook.GetSheet(sheetName);
 52                         if (sheet != null)
 53                         {
 54                             dt = GetSheetDataTable(sheet,  strMsg);
 55                             if (dt != null)
 56                             {
 57                                 dt.TableName = sheetName.Trim();
 58                                 ds.Tables.Add(dt);
 59                             }
 60                             else
 61                             {
 62 is                                  MessageBox.Show ( " Sheet Data acquisition failure reason: " + strMsg);
 63 is                              }
 64                          }
 65                      }
 66                  }
 67                  return DS;
 68              }
 69              the catch (Exception EX)
 70              {
 71 is                  strMsg = ex.Message;
 72                  return  null ;
 73 is              }
 74          }
 75         /// <summary>
 76         /// 获取sheet表对应的DataTable
 77         /// </summary>
 78         /// <param name="sheet">Excel工作表</param>
 79         /// <param name="strMsg"></param>
 80         /// <returns></returns>
 81         private static DataTable GetSheetDataTable(ISheet sheet,  string strMsg)
 82         {
 83             strMsg = "";
 84             DataTable dt = new DataTable();
 85             string sheetName = sheet.SheetName;
 86             int startIndex = 0;// sheet.FirstRowNum;
 87             int lastIndex = sheet.LastRowNum;
 88             //最大列数
 89             int cellCount = 0;
 90             IRow maxRow = sheet.GetRow(0);
 91             for (int i = startIndex; i <= lastIndex; i++)
 92             {
 93                 IRow row = sheet.GetRow(i);
 94                 if (row != null && cellCount < row.LastCellNum)
 95                 {
 96                     cellCount = row.LastCellNum;
 97                     maxRow = row;
 98                 }
 99             }
100             //列名设置
101             try
102             {
103                 for (int i = 0; i < maxRow.LastCellNum; i++)//maxRow.FirstCellNum
104                 {
105                     dt.Columns.Add(Convert.ToChar(((int)'A') + I) .ToString ());
 106                      // the DataColumn the DataColumn new new column = ( "the Column" + (I +. 1) .ToString ());
 107                      // dt.Columns.Add (column); 
108                  }
 109              }
 110              the catch 
111              {
 112                  strMsg = " sheet " + + sheetName " no data " ;
 113                  return  null ;
 114              }
 115              // data padding 
1 16              for ( int I = startIndex; I <= the lastIndex; I ++)
117             {
118                 IRow row = sheet.GetRow(i);
119                 DataRow drNew = dt.NewRow();
120                 if (row != null)
121                 {
122                     for (int j = row.FirstCellNum; j < row.LastCellNum; ++j)
123                     {
124                         if (row.GetCell(j) != null)
125                         {
126                             ICell cell = row.GetCell(j);
127                             switch(cell.CellType)
 128                              {
 129                                  Case CellType.Blank:
 130.                                      drNew [J] = "" ;
 131 is                                      BREAK ;
 132                                  Case CellType.Numeric:
 133                                      Short the format = cell.CellStyle.DataFormat;
 134                                      // time format (2015.12. 5,2015 / 12 / 5,2015-12-5 like) process 
135                                      IF (the format == 14 || the format == 31 is || the format == 57 is || the format == 58 )
 136                                          drNew [J] = cell.DateCellValue;
137                                     else
138                                         drNew[j] = cell.NumericCellValue;
139                                     if (cell.CellStyle.DataFormat == 177 || cell.CellStyle.DataFormat == 178 || cell.CellStyle.DataFormat == 188)
140                                         drNew[j] = cell.NumericCellValue.ToString("#0.00");
141                                     break;
142                                 case CellType.String:
143                                     drNew[j] = cell.StringCellValue;
144                                     break;
145                                  Case CellType.Formula:
 146                                      try 
147                                      {
 148                                          drNew [j] = cell.NumericCellValue;
149                                          if (cell.CellStyle.DataFormat == 177 || cell.CellStyle.DataFormat == 178 || cell.CellStyle.DataFormat == 188 )
 150                                              drNew [j] = cell.NumericCellValue.ToString ( " # 00 o'clock " );
151                                      }
 152                                      catch 
153                                     {
154                                         try
155                                         {
156                                             drNew[j] = cell.StringCellValue;
157                                         }
158                                         catch { }
159                                     }
160                                     break;
161                                 default:
162                                     drNew[j] = cell.StringCellValue;
163                                     break;
164                             }
165                         }
166                     }
167                 }
168                 dt.Rows.Add(drNew);
169             }
170             return dt;
171         }
172     }

 

Guess you like

Origin www.cnblogs.com/ZkbFighting/p/11236385.html