日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無先后,達者為師

網(wǎng)站首頁 編程語言 正文

C#使用NPOI實現(xiàn)Excel和DataTable的互轉(zhuǎn)_C#教程

作者:林深時見祿 ? 更新時間: 2022-04-25 編程語言

什么是NPOI?

NPOI是指構(gòu)建在POI 3.x版本之上的一個程序,NPOI可以在沒有安裝Office的情況下對Word或Excel文檔進行讀寫操作。

NPOI是一個開源的C#讀寫Excel、WORD等微軟OLE2組件文檔的項目。

使用NuGet安裝NPOI

NuGet直接搜索NPOI,目前版本是v2.4.1,將其安裝至項目即可。

安裝完成后,項目會自動為我們添加這4個引用

同時還需要在程序中引入NPOI.SS.UserModel;NPOI.XSSF.UserModel;NPOI.HSSF.UserModel;三個命名空間

廢話不多說,直接上代碼

DataTable導(dǎo)出Excel

/// 
/// Datable導(dǎo)出成Excel
/// 
/// 
/// 導(dǎo)出路徑(包括文件名與擴展名)
public static void TableToExcel(DataTable dt, string file)
? ? ? ? {
? ? ? ? ? ? IWorkbook workbook;
? ? ? ? ? ? string fileExt = Path.GetExtension(file).ToLower();
? ? ? ? ? ? if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(); } else { workbook = null; }
? ? ? ? ? ? if (workbook == null) { return; }
? ? ? ? ? ? ISheet sheet = string.IsNullOrEmpty(dt.TableName) ? workbook.CreateSheet("Sheet1") : workbook.CreateSheet(dt.TableName);
?
? ? ? ? ? ? //表頭 ?
? ? ? ? ? ? IRow row = sheet.CreateRow(0);
? ? ? ? ? ? for (int i = 0; i < dt.Columns.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ICell cell = row.CreateCell(i);
? ? ? ? ? ? ? ? cell.SetCellValue(dt.Columns[i].ColumnName);
? ? ? ? ? ? }
?
? ? ? ? ? ? //數(shù)據(jù) ?
? ? ? ? ? ? for (int i = 0; i < dt.Rows.Count; i++)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? IRow row1 = sheet.CreateRow(i + 1);
? ? ? ? ? ? ? ? for (int j = 0; j < dt.Columns.Count; j++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ICell cell = row1.CreateCell(j);
? ? ? ? ? ? ? ? ? ? cell.SetCellValue(dt.Rows[i][j].ToString());
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
?
? ? ? ? ? ? //轉(zhuǎn)為字節(jié)數(shù)組 ?
? ? ? ? ? ? MemoryStream stream = new MemoryStream();
? ? ? ? ? ? workbook.Write(stream);
? ? ? ? ? ? var buf = stream.ToArray();
?
? ? ? ? ? ? //保存為Excel文件 ?
? ? ? ? ? ? using (FileStream fs = new FileStream(file, FileMode.Create, FileAccess.Write))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? fs.Write(buf, 0, buf.Length);
? ? ? ? ? ? ? ? fs.Flush();
? ? ? ? ? ? }
? ? ? ? }

Excel導(dǎo)入DataTable

/// 
/// Excel導(dǎo)入成Datable
/// 
/// 導(dǎo)入路徑(包含文件名與擴展名)
/// 
public static DataTable ExcelToTable(string file)
? ? ? ? {
? ? ? ? ? ? DataTable dt = new DataTable();
? ? ? ? ? ? IWorkbook workbook;
? ? ? ? ? ? string fileExt = Path.GetExtension(file).ToLower();
? ? ? ? ? ? using (FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read))
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //XSSFWorkbook 適用XLSX格式,HSSFWorkbook 適用XLS格式
? ? ? ? ? ? ? ? if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(fs); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(fs); } else { workbook = null; }
? ? ? ? ? ? ? ? if (workbook == null) { return null; }
? ? ? ? ? ? ? ? ISheet sheet = workbook.GetSheetAt(0);
?
? ? ? ? ? ? ? ? //表頭 ?
? ? ? ? ? ? ? ? IRow header = sheet.GetRow(sheet.FirstRowNum);
? ? ? ? ? ? ? ? List columns = new List();
? ? ? ? ? ? ? ? for (int i = 0; i < header.LastCellNum; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? object obj = GetValueType(header.GetCell(i));
? ? ? ? ? ? ? ? ? ? if (obj == null || obj.ToString() == string.Empty)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? dt.Columns.Add(new DataColumn("Columns" + i.ToString()));
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? else
? ? ? ? ? ? ? ? ? ? ? ? dt.Columns.Add(new DataColumn(obj.ToString()));
? ? ? ? ? ? ? ? ? ? columns.Add(i);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? //數(shù)據(jù) ?
? ? ? ? ? ? ? ? for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++)
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? DataRow dr = dt.NewRow();
? ? ? ? ? ? ? ? ? ? bool hasValue = false;
? ? ? ? ? ? ? ? ? ? foreach (int j in columns)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? dr[j] = GetValueType(sheet.GetRow(i).GetCell(j));
? ? ? ? ? ? ? ? ? ? ? ? if (dr[j] != null && dr[j].ToString() != string.Empty)
? ? ? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? ? ? hasValue = true;
? ? ? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? ? ? if (hasValue)
? ? ? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? ? ? dt.Rows.Add(dr);
? ? ? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? }
? ? ? ? ? ? }
? ? ? ? ? ? return dt;
? ? ? ? }
?
? ? ? ? /// 
? ? ? ? /// 獲取單元格類型
? ? ? ? /// 
? ? ? ? /// 
? ? ? ? /// 
? ? ? ? private static object GetValueType(ICell cell)
? ? ? ? {
? ? ? ? ? ? if (cell == null)
? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? switch (cell.CellType)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? case CellType.Blank: //BLANK: ?
? ? ? ? ? ? ? ? ? ? return null;
? ? ? ? ? ? ? ? case CellType.Boolean: //BOOLEAN: ?
? ? ? ? ? ? ? ? ? ? return cell.BooleanCellValue;
? ? ? ? ? ? ? ? case CellType.Numeric: //NUMERIC: ?
? ? ? ? ? ? ? ? ? ? return cell.NumericCellValue;
? ? ? ? ? ? ? ? case CellType.String: //STRING: ?
? ? ? ? ? ? ? ? ? ? return cell.StringCellValue;
? ? ? ? ? ? ? ? case CellType.Error: //ERROR: ?
? ? ? ? ? ? ? ? ? ? return cell.ErrorCellValue;
? ? ? ? ? ? ? ? case CellType.Formula: //FORMULA: ?
? ? ? ? ? ? ? ? default:
? ? ? ? ? ? ? ? ? ? return "=" + cell.CellFormula;
? ? ? ? ? ? }
}

原文鏈接:https://blog.csdn.net/xiaolu1014/article/details/98454110

欄目分類
最近更新