using System; using System.Collections.Generic; using System.Text; using NPOI.HPSF; using NPOI.HSSF.UserModel; using NPOI.HSSF.Record; using System.IO; using NPOI.SS.Util; using NPOI.SS.UserModel; namespace Stone.Common { public class MyExcel { public HSSFWorkbook hssfworkbook = null; public string filename = null; public HSSFSheet sheet = null; public MyExcel() { } /// /// 新建一个Excel文件 /// /// 要新建的Excel文件路径 public void NewExcel(string _filename) { filename = _filename; hssfworkbook = new HSSFWorkbook(); } /// /// 打开一个Excel文件 /// /// 要打开的Excel文件路径 public void OpenExcel(string _filename) { filename = _filename; FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read); hssfworkbook = new HSSFWorkbook(file); } /// /// 新建一个工作表 /// /// public void NewSheet(string sheetname) { sheet = (HSSFSheet)hssfworkbook.CreateSheet(sheetname); } /// /// 打开一个工作表 /// /// public void OpenSheet(string sheetname) { sheet = (HSSFSheet)hssfworkbook.GetSheet(sheetname); } /// /// 创建一行 /// /// public void CreateRow(int rowindex) { sheet.CreateRow(rowindex); } /// /// 创建一单元格 /// /// /// public void CreateCell(int rowindex, int colindex) { sheet.GetRow(rowindex).CreateCell(colindex); } /// /// 设置单元格的值 /// /// /// /// public void SetCellValue(int rowindex, int colindex, string value) { sheet.GetRow(rowindex).GetCell(colindex).SetCellValue(value); } /// /// 保存Excel文件 /// public void Save() { FileStream file = new FileStream(filename, FileMode.Create); hssfworkbook.Write(file); file.Close(); } } }