C#从excel中将数据导出到datatable
生活随笔
收集整理的這篇文章主要介紹了
C#从excel中将数据导出到datatable
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#region 從excel中將數據導出到datatable/// <summary>/// 讀取excel 默認第一行為標頭/// </summary>/// <param name="strFileName">excel文檔路徑</param>/// <returns></returns>public static DataTable ImportExceltoDt(string strFileName){DataTable dt = new DataTable();IWorkbook wb;using (FileStream file = new FileStream(strFileName, FileMode.Open, FileAccess.Read)){wb = WorkbookFactory.Create(file);}ISheet sheet = wb.GetSheetAt(0);dt = ImportDt(sheet, 0, true);return dt;}/// <summary>/// 將制定sheet中的數據導出到datatable中/// </summary>/// <param name="sheet">需要導出的sheet</param>/// <param name="HeaderRowIndex">列頭所在行號,-1表示沒有列頭</param>/// <returns></returns>/// static DataTable ImportDt(ISheet sheet, int HeaderRowIndex, bool needHeader){DataTable table = new DataTable();IRow headerRow;int cellCount;try{if (HeaderRowIndex < 0 || !needHeader){headerRow = sheet.GetRow(0);cellCount = headerRow.LastCellNum;for (int i = headerRow.FirstCellNum; i <= cellCount; i++){DataColumn column = new DataColumn(Convert.ToString(i));table.Columns.Add(column);}}else{headerRow = sheet.GetRow(HeaderRowIndex);cellCount = headerRow.LastCellNum;for (int i = headerRow.FirstCellNum; i <= cellCount; i++){if (headerRow.GetCell(i) == null){if (table.Columns.IndexOf(Convert.ToString(i)) > 0){DataColumn column = new DataColumn(Convert.ToString("重復列名" + i));table.Columns.Add(column);}else{DataColumn column = new DataColumn(Convert.ToString(i));table.Columns.Add(column);}}else if (table.Columns.IndexOf(headerRow.GetCell(i).ToString()) > 0){DataColumn column = new DataColumn(Convert.ToString("重復列名" + i));table.Columns.Add(column);}else{DataColumn column = new DataColumn(headerRow.GetCell(i).ToString());table.Columns.Add(column);}}}int rowCount = sheet.LastRowNum;for (int i = (HeaderRowIndex + 1); i <= sheet.LastRowNum; i++){try{IRow row;if (sheet.GetRow(i) == null){row = sheet.CreateRow(i);}else{row = sheet.GetRow(i);}DataRow dataRow = table.NewRow();for (int j = row.FirstCellNum; j <= cellCount; j++){try{if (row.GetCell(j) != null){switch (row.GetCell(j).CellType){case CellType.String:string str = row.GetCell(j).StringCellValue;if (str != null && str.Length > 0){dataRow[j] = str.ToString();}else{dataRow[j] = null;}break;case CellType.Numeric:if (DateUtil.IsCellDateFormatted(row.GetCell(j))){dataRow[j] = DateTime.FromOADate(row.GetCell(j).NumericCellValue);}else{dataRow[j] = Convert.ToDouble(row.GetCell(j).NumericCellValue);}break;case CellType.Boolean:dataRow[j] = Convert.ToString(row.GetCell(j).BooleanCellValue);break;case CellType.Error:dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue);break;case CellType.Formula:switch (row.GetCell(j).CachedFormulaResultType){case CellType.String:string strFORMULA = row.GetCell(j).StringCellValue;if (strFORMULA != null && strFORMULA.Length > 0){dataRow[j] = strFORMULA.ToString();}else{dataRow[j] = null;}break;case CellType.Numeric:dataRow[j] = Convert.ToString(row.GetCell(j).NumericCellValue);break;case CellType.Boolean:dataRow[j] = Convert.ToString(row.GetCell(j).BooleanCellValue);break;case CellType.Error:dataRow[j] = ErrorEval.GetText(row.GetCell(j).ErrorCellValue);break;default:dataRow[j] = "";break;}break;default:dataRow[j] = "";break;}}}catch (Exception exception){}}table.Rows.Add(dataRow);}catch (Exception exception){//wl.WriteLogs(exception.ToString());
}}}catch (Exception exception){//wl.WriteLogs(exception.ToString());
}return table;}#endregion
?
轉載于:https://www.cnblogs.com/liyunworld/p/11168153.html
總結
以上是生活随笔為你收集整理的C#从excel中将数据导出到datatable的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 本周ASP.NET英文技术文章推荐[02
- 下一篇: 用Python操作Redis