You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

106 lines
2.7 KiB

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()
{
}
/// <summary>
/// 新建一个Excel文件
/// </summary>
/// <param name="_filename">要新建的Excel文件路径</param>
public void NewExcel(string _filename)
{
filename = _filename;
hssfworkbook = new HSSFWorkbook();
}
/// <summary>
/// 打开一个Excel文件
/// </summary>
/// <param name="_filename">要打开的Excel文件路径</param>
public void OpenExcel(string _filename)
{
filename = _filename;
FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read);
hssfworkbook = new HSSFWorkbook(file);
}
/// <summary>
/// 新建一个工作表
/// </summary>
/// <param name="sheetname"></param>
public void NewSheet(string sheetname)
{
sheet = (HSSFSheet)hssfworkbook.CreateSheet(sheetname);
}
/// <summary>
/// 打开一个工作表
/// </summary>
/// <param name="sheetname"></param>
public void OpenSheet(string sheetname)
{
sheet = (HSSFSheet)hssfworkbook.GetSheet(sheetname);
}
/// <summary>
/// 创建一行
/// </summary>
/// <param name="rowindex"></param>
public void CreateRow(int rowindex)
{
sheet.CreateRow(rowindex);
}
/// <summary>
/// 创建一单元格
/// </summary>
/// <param name="rowindex"></param>
/// <param name="colindex"></param>
public void CreateCell(int rowindex, int colindex)
{
sheet.GetRow(rowindex).CreateCell(colindex);
}
/// <summary>
/// 设置单元格的值
/// </summary>
/// <param name="rowindex"></param>
/// <param name="colindex"></param>
/// <param name="value"></param>
public void SetCellValue(int rowindex, int colindex, string value)
{
sheet.GetRow(rowindex).GetCell(colindex).SetCellValue(value);
}
/// <summary>
/// 保存Excel文件
/// </summary>
public void Save()
{
FileStream file = new FileStream(filename, FileMode.Create);
hssfworkbook.Write(file);
file.Close();
}
}
}