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.

125 lines
3.2 KiB

3 years ago
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using NPOI.SS.Util;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System.Drawing;
namespace Stone.Common
{
public class MyExcelX
{
public XSSFWorkbook hssfworkbook = null;
public string filename = null;
public XSSFSheet sheet = null;
public MyExcelX()
{
}
/// <summary>
/// 新建一个Excel文件
/// </summary>
/// <param name="_filename">要新建的Excel文件路径</param>
public void NewExcel(string _filename)
{
filename = _filename;
hssfworkbook = new XSSFWorkbook();
}
/// <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 XSSFWorkbook(file);
}
/// <summary>
/// 新建一个工作表
/// </summary>
/// <param name="sheetname"></param>
public void NewSheet(string sheetname)
{
sheet = (XSSFSheet)hssfworkbook.CreateSheet(sheetname);
}
/// <summary>
/// 打开一个工作表
/// </summary>
/// <param name="sheetname"></param>
public void OpenSheet(string sheetname)
{
sheet = (XSSFSheet)hssfworkbook.GetSheet(sheetname);
}
/// <summary>
/// 创建一行
/// </summary>
/// <param name="rowindex"></param>
public void CreateRow(int rowindex)
{
sheet.CreateRow(rowindex);
}
/// <summary>
/// 创建一行
/// </summary>
/// <param name="rowindex"></param>
public void CreateRow(int rowindex, short rowheight)
{
sheet.CreateRow(rowindex);
sheet.GetRow(rowindex).Height = rowheight;
}
/// <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);
}
public void SetCellValue(int rowindex, int colindex, double 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();
}
}
}