您好,登錄后才能下訂單哦!
本篇內容介紹了“C#如何使用NPOI對Excel數據進行導入導出”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
NPOI 是 POI 項目的 .NET 版本。POI是一個開源的Java讀寫Excel、WORD等微軟OLE2組件文檔的項目, 使用 NPOI 你就可以在沒有安裝 Office 或者相應環境的機器上對 WORD/EXCEL 文檔進行讀寫。
NPOI: V2.5.1
MyXls: (已停止)
Aspose.Cell.dll: 收費
EPPlus 5
Spire.XLS: 收費
使用時需引用需要引用所有5個dll
ICSharpCode.SharpZipLib.dll
NPOI.dll
NPOI.OOXML.dll
NPOI.OpenXml4Net.dll
NPOI.OpenXmlFormats.dll
DataTable dt = ImportToTable("00.xls"); if (dt != null) { Console.Write(dt.Rows.Count); Console.ReadKey(); } public static DataTable ImportToTable(string fileName) { DataTable dt = new DataTable(); IWorkbook workbook; string fileExt = Path.GetExtension(fileName).ToLower(); using (FileStream fs = new FileStream(fileName, 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; return null; } ISheet sheet = workbook.GetSheetAt(0);//Sheet總數量:workbook.NumberOfSheets //表頭 IRow header = sheet.GetRow(sheet.FirstRowNum); 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())); } //數據 for (int i = sheet.FirstRowNum + 1; i <= sheet.LastRowNum; i++) { DataRow dr = dt.NewRow(); bool hasValue = false; IRow row = sheet.GetRow(i); for (int j = row.FirstCellNum; j < row.LastCellNum; j++) { 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; } } /// /// 獲取單元格類型 /// /// /// /// 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; } }
IEnumerator rows = sheet.GetEnumerator(); while (rows.MoveNext()) { IRow row = (HSSFRow)rows.Current; ICell cell = row.GetCell(0); if (cell != null && cell.StringCellValue == "XX") { return row.GetCell(1).StringCellValue; } }
IWorkbook workbook = new HSSFWorkbook(); //add picture data to this workbook. byte[] bytes = System.IO.File.ReadAllBytes(@"00.jpg"); int pictureIdx = workbook.AddPicture(bytes, PictureType.JPEG); //create sheet ISheet sheet = workbook.CreateSheet("Sheet1"); // Create the drawing patriarch. This is the top level container for all shapes. IDrawing patriarch = sheet.CreateDrawingPatriarch(); //add a picture HSSFClientAnchor anchor = new HSSFClientAnchor(0, 0, 1023, 0, 0, 0, 1, 3); IPicture pict = patriarch.CreatePicture(anchor, pictureIdx); //保存為Excel文件 using (FileStream fs = new FileStream("00_new.xls", FileMode.Create, FileAccess.Write)) { workbook.Write(fs); }
IWorkbook workbook; using (FileStream fs = new FileStream("模板文件.xls", FileMode.Open, FileAccess.Read)) { workbook = new HSSFWorkbook(fs); } ISheet cloneSheet = workbook.CloneSheet(workbook.GetSheetIndex("Sheet1"));//復制第一個模板Sheet cloneSheet.ForceFormulaRecalculation = true; workbook.SetSheetName(workbook.GetSheetIndex(cloneSheet), "SheetClone");//設置新SheetName cloneSheet.GetRow(4).GetCell(1).SetCellValue("a");//為已經存在的單元格賦值 IRow row = cloneSheet.GetRow(15); if (row == null) row = cloneSheet.CreateRow(15); ICell cell = row.GetCell(7); if (cell == null) cell = row.CreateCell(7); cell.SetCellValue("XX");// 為不存在的單元格,先新建再賦值 cloneSheet.ShiftRows(51, 60, 34);//51-60行(尾部)整體移動34行,騰出更多控件插入多行數據 workbook.RemoveSheetAt(workbook.GetSheetIndex("Sheet1"));//移除原模板Sheet FileStream fs_new = new FileStream(DateTime.Now.Ticks + ".xls", FileMode.Create); workbook.Write(fs_new); fs_new.Close();
調用方式:
ExportToExcel(dt, "00_new.xls");
代碼
public static void ExportToExcel(DataTable dt, string fileName) { IWorkbook workbook; string fileExt = Path.GetExtension(fileName).ToLower(); //XSSFWorkbook 適用XLSX格式,HSSFWorkbook 適用XLS格式 if (fileExt == ".xlsx") { workbook = new XSSFWorkbook(); } else if (fileExt == ".xls") { workbook = new HSSFWorkbook(); } else { 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); } //數據 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()); } } //保存為Excel文件 using (FileStream fs = new FileStream(fileName, FileMode.Create, FileAccess.Write)) { workbook.Write(fs); } }
public static MemoryStream ExportToExcel(DataTable dt, string HeaderText) { var workbook = new HSSFWorkbook(); ISheet sheet = workbook.CreateSheet(string.IsNullOrWhiteSpace(dt.TableName) ? "Sheet1" : dt.TableName); //右擊文件“屬性”信息 #region 文件屬性信息 { var dsi = PropertySetFactory.CreateDocumentSummaryInformation(); dsi.Company = "NPOI"; workbook.DocumentSummaryInformation = dsi; SummaryInformation si = PropertySetFactory.CreateSummaryInformation(); si.Author = "文件作者信息"; si.ApplicationName = "創建程序信息"; si.LastAuthor = "最后保存者信息"; si.Comments = "作者信息"; si.Title = "標題信息"; si.Subject = "主題信息"; si.CreateDateTime = DateTime.Now; workbook.SummaryInformation = si; } #endregion //格式 var dateStyle = workbook.CreateCellStyle(); var format = workbook.CreateDataFormat(); dateStyle.DataFormat = format.GetFormat("yyyy-mm-dd");//日期格式 //取得列寬 var arrColWidth = new int[dt.Columns.Count]; foreach (DataColumn item in dt.Columns) { arrColWidth[item.Ordinal] = Encoding.GetEncoding(936).GetBytes(item.ColumnName.ToString()).Length; } for (var i = 0; i < dt.Rows.Count; i++) { for (var j = 0; j < dt.Columns.Count; j++) { int intTemp = Encoding.GetEncoding(936).GetBytes(dt.Rows[i][j].ToString()).Length; if (intTemp > arrColWidth[j]) { arrColWidth[j] = intTemp; } } } int rowIndex = 0; foreach (DataRow row in dt.Rows) { #region 表頭 列頭 if (rowIndex == 65535 || rowIndex == 0) { if (rowIndex != 0) { sheet = workbook.CreateSheet();//超過65535行,則新建一個Sheet } #region 表頭及樣式 { var headerRow = sheet.CreateRow(0); headerRow.HeightInPoints = 25; headerRow.CreateCell(0).SetCellValue(HeaderText); //CellStyle ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;// 左右居中 headStyle.VerticalAlignment = VerticalAlignment.Center;// 上下居中 // 設置單元格的背景顏色(單元格的樣式會覆蓋列或行的樣式) headStyle.FillForegroundColor = (short)11; //定義font IFont font = workbook.CreateFont(); font.FontHeightInPoints = 20; font.Boldweight = 700; headStyle.SetFont(font); headerRow.GetCell(0).CellStyle = headStyle; sheet.AddMergedRegion(new NPOI.SS.Util.CellRangeAddress(0, 0, 0, dt.Columns.Count - 1));//合并區域 } #endregion #region 列頭及樣式 { var headerRow = sheet.CreateRow(1); //CellStyle ICellStyle headStyle = workbook.CreateCellStyle(); headStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;// 左右居中 headStyle.VerticalAlignment = VerticalAlignment.Center;// 上下居中 //定義font IFont font = workbook.CreateFont(); font.FontHeightInPoints = 10; font.Boldweight = 700; headStyle.SetFont(font); foreach (DataColumn column in dt.Columns) { headerRow.CreateCell(column.Ordinal).SetCellValue(column.ColumnName); headerRow.GetCell(column.Ordinal).CellStyle = headStyle; sheet.SetColumnWidth(column.Ordinal, (arrColWidth[column.Ordinal] + 1) * 256);//設置列寬 } } #endregion rowIndex = 2;//數據行RowIndex為2(表頭和列頭個占一行) } #endregion #region 內容 var dataRow = sheet.CreateRow(rowIndex); foreach (DataColumn column in dt.Columns) { var newCell = dataRow.CreateCell(column.Ordinal); string drValue = row[column].ToString(); switch (column.DataType.ToString()) { case "System.String"://字符串類型 newCell.SetCellValue(drValue); break; case "System.DateTime"://日期類型 DateTime dateV; DateTime.TryParse(drValue, out dateV); newCell.SetCellValue(dateV); newCell.CellStyle = dateStyle;//格式化顯示 break; case "System.Boolean"://布爾型 bool boolV = false; bool.TryParse(drValue, out boolV); newCell.SetCellValue(boolV); break; case "System.Int16"://整型 case "System.Int32": case "System.Int64": case "System.Byte": int intV = 0; int.TryParse(drValue, out intV); newCell.SetCellValue(intV); break; case "System.Decimal"://浮點型 case "System.Double": double doubV = 0; double.TryParse(drValue, out doubV); newCell.SetCellValue(doubV); break; case "System.DBNull"://空值處理 newCell.SetCellValue(""); break; default: newCell.SetCellValue("");//設置單元格公式:newCell.SetCellFormula("SUM($B0:$D0)") break; } } #endregion rowIndex++; } //自動列寬 for (int i = 0; i <= dt.Columns.Count; i++) sheet.AutoSizeColumn(i, true); using (MemoryStream ms = new MemoryStream()) { workbook.Write(ms); ms.Flush(); ms.Position = 0; return ms; } }
public static void ExportToExcelByWeb(DataTable dt, string HeaderText, string FileName) { HttpContext context = HttpContext.Current; context.Response.ContentType = "application/vnd.ms-excel"; context.Response.ContentEncoding = Encoding.UTF8; context.Response.Charset = "UTF-8"; context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", HttpUtility.UrlEncode(FileName, Encoding.UTF8))); byte[] data = ExportToExcel(dt, HeaderText).GetBuffer();//Read()方法也可以 context.Response.BinaryWrite(data);// 或者: context.Response.OutputStream.Write(data,0,data.Length) context.Response.End(); }
public static void ExportToExcel(DataTable dt, string HeaderText, string FileName) { using (MemoryStream ms = ExportToExcel(dt, HeaderText)) { using (FileStream fs = new FileStream(FileName, FileMode.Create, FileAccess.Write)) { byte[] data = ms.ToArray();//跟GetBuffer()對比,速度稍慢,但無空數據 fs.Write(data, 0, data.Length); fs.Flush(); } } }
GridView導出到Excel
Web中的GridView可直接導出到Excel:renderControl()
“C#如何使用NPOI對Excel數據進行導入導出”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。