使用NPOI操作Excel(03、07)
2015-05-09 16:35
639 查看
using System; using System.Collections.Generic; using System.Linq; using System.Text; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using NPOI.HSSF.UserModel; using System.IO; using System.Data; using NPOI.SS.Util; namespace Common { public class ExcelHelper : IDisposable { private string fileName = null; //文件名 private IWorkbook workbook = null; private FileStream fs = null; private bool disposed; public ExcelHelper(string fileName) { this.fileName = fileName; disposed = false; } /// <summary> /// 生成Sheet对象 /// </summary> /// <param name="fileName">导出Excel文件完整文件名</param> /// <param name="sheetName">要导入的excel的sheet的名称</param> /// <returns></returns> public ISheet GenerateSheet(string sheetName) { ISheet sheet = null; fs = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 workbook = new XSSFWorkbook(); else if (fileName.IndexOf(".xls") > 0) // 2003版本 workbook = new HSSFWorkbook(); if (workbook != null) { sheet = workbook.CreateSheet(sheetName); } return sheet; } /// <summary> /// 将DataTable数据导入到excel中 /// </summary> /// <param name="sheet">所要操作Excel的Sheet对象</param> /// <param name="data">要导入的数据</param> /// <param name="dataRowStart">数据起始行索引</param> /// <returns>导入数据行数(包含列名那一行)</returns> public int DataTableToExcel(ISheet sheet, DataTable data, int dataRowStart) { int i = 0; int j = 0; try { if (sheet != null) { for (i = 0; i < data.Rows.Count; ++i) { IRow row = sheet.CreateRow(dataRowStart); for (j = 0; j < data.Columns.Count; ++j) { row.CreateCell(j).SetCellValue(data.Rows[i][j].ToString()); } ++dataRowStart; } workbook.Write(fs); //写入到excel } return dataRowStart; } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); return -1; } } /// <summary> /// 将excel中的数据导入到DataTable中 /// </summary> /// <param name="sheetName">excel工作薄sheet的名称</param> /// <param name="dataRowStart">数据起始行索引</param> /// <returns>返回的DataTable</returns> public DataTable ExcelToDataTable(DataTable tableModel, string sheetName, int dataRowStart) { ISheet sheet = null; DataTable data = tableModel.Clone(); try { fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); if (fileName.IndexOf(".xlsx") > 0) // 2007版本 workbook = new XSSFWorkbook(fs); else if (fileName.IndexOf(".xls") > 0) // 2003版本 workbook = new HSSFWorkbook(fs); if (sheetName != null) { sheet = workbook.GetSheet(sheetName); if (sheet == null) //如果没有找到指定的sheetName对应的sheet,则尝试获取第一个sheet { sheet = workbook.GetSheetAt(0); } } else { sheet = workbook.GetSheetAt(0); } if (sheet != null) { IRow firstRow = sheet.GetRow(dataRowStart); int cellCount = firstRow.LastCellNum; //一行最后一个cell的编号 即总的列数 //最后一列的标号 int rowCount = sheet.LastRowNum; for (int i = dataRowStart; i <= rowCount; ++i) { IRow row = sheet.GetRow(i); if (row == null) continue; //没有数据的行默认是null DataRow dataRow = data.NewRow(); for (int j = row.FirstCellNum; j < cellCount; ++j) { if (row.GetCell(j) != null) //同理,没有数据的单元格都默认是null dataRow[j] = row.GetCell(j).ToString(); } data.Rows.Add(dataRow); } } return data; } catch (Exception ex) { Console.WriteLine("Exception: " + ex.Message); return null; } } /// <summary> /// 设置表头(支持单元格合并) /// </summary> /// <param name="sheet">要合并单元格所在的sheet</param> /// <param name="mergeArrayList">需要进行单元格合并的单元格索引数组,数组长度为4,第一个为行起始位置,第二个为行结束为止,第三个为列起始位置,第四个为列结束为止</param> /// <param name="valueList">单元格合并后值的集合,与合并单元格索引数组集合一一对应</param> //public void SetCellRangeAddress(ISheet sheet, int rowstart, int rowend, int colstart, int colend, string value) public void SetExcelHeader(ISheet sheet, IList<int[]> mergeArrayList, IList<string> valueList) { for (int m = 0; m < mergeArrayList.Count; m++) { if (mergeArrayList[m].Length > 4) { return; } int rowstart = mergeArrayList[m][0]; int rowend = mergeArrayList[m][1]; int colstart = mergeArrayList[m][2]; int colend = mergeArrayList[m][3]; string value = ""; if (m < valueList.Count) { value = valueList[m]; } ICellStyle style = workbook.CreateCellStyle(); style.Alignment = HorizontalAlignment.Center; IFont font = workbook.CreateFont(); font.Boldweight = 700; font.FontHeight = 20 * 20; style.SetFont(font); for (int i = rowstart; i < rowend + 1; i++) { IRow row = sheet.GetRow(i); if (row==null) { row = sheet.CreateRow(i); } for (int j = colstart; j < colend + 1; j++) { ICell cell = row.CreateCell(j); cell.SetCellValue(value); cell.CellStyle = style; } } if (rowend - rowstart > 0 || colend - colstart > 0) { CellRangeAddress cellRangeAddress = new CellRangeAddress(rowstart, rowend, colstart, colend); sheet.AddMergedRegion(cellRangeAddress); } } //workbook.Write(fs); } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { if (fs != null) fs.Close(); } fs = null; disposed = true; } } } }
CommenClass
相关文章推荐
- NPOI读写Excel (使用NPOI2.0 包含03和07版本)
- 使用第三方控件NPOI操作excel
- 使用 EPPlus,NPOI,操作EXCEL
- C# 如何使用NPOI操作Excel
- 用NPOI操作EXCEL--巧妙使用Excel Chart
- 使用NPOI操作Excel文件及其日期处理
- 使用NPOI操作Excel文件及其日期处理
- Python 使用 Xlrd/xlwt 操作 Excel 分类: python基础学习 2014-03-17 12:06 958人阅读 评论(0) 收藏
- 使用NPOI操作EXCEL时设置超链接和文字颜色
- 使用NPOI操作Excel时,给一列所有的单元格加上下拉框的输入限制
- POI 操作Excel支持03/07
- 使用NPOI操作Excel导入导出数据
- C#中使用NPOI操作excel
- 3.5 用NPOI操作EXCEL--巧妙使用Excel Chart
- 使用 npoi 操作excel ”npoi“可从“http://www.codeplex.com/npoi”下载
- 使用NPOI操作excel
- C#开发中使用Npoi操作excel实例代码
- C#开发中使用Npoi操作excel实例代码
- C#开发中使用Npoi操作excel实例代码
- 使用NPOI操作Excel