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.

173 lines
4.8 KiB

using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Shentun.Utilities
{
public class NPOIExcelHelper : IDisposable
{
private IWorkbook _workbook;
private ISheet _sheet;
private bool _disposed;
private FileStream _fileStream = null;
public void CreateWorkbook()
{
if (_workbook != null)
{
throw new Exception("已存在一个工作簿,禁止创建");
}
_workbook = new HSSFWorkbook();
}
public void OpenWorkbook(string fileName)
{
if (_workbook != null)
{
throw new Exception("已存在一个工作簿,禁止打开");
}
if (!File.Exists(fileName))
{
throw new Exception(fileName + "不存在,不能打开");
}
_fileStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite);
if (fileName.IndexOf(".xlsx") > 0) // 2007版本
_workbook = new XSSFWorkbook(_fileStream);
else if (fileName.IndexOf(".xls") > 0) // 2003版本
_workbook = new HSSFWorkbook(_fileStream);
else
throw new Exception("Excel文件后缀名必须为xls或xlsx");
}
public void SelectSheet(string sheetName)
{
_sheet = _workbook.GetSheet(sheetName);
if (_sheet == null)
throw new Exception(sheetName + "不存在");
}
public void SelectFirstSheet()
{
if (_workbook.NumberOfSheets == 0)
{
AddSheet("Sheet1");
}
_sheet = _workbook.GetSheetAt(0);
}
public void AddSheet(string sheetName)
{
_sheet = _workbook.CreateSheet(sheetName);
}
public ICell GetCell(int row, int column)
{
IRow sheetRow = (IRow)_sheet.GetRow(row);
if (sheetRow == null)
{
sheetRow = (IRow)_sheet.CreateRow(row);
}
ICell cell = sheetRow.GetCell(column);
if (cell == null)
cell = sheetRow.CreateCell(column);
return cell;
}
public void SetCellValue(int row, int column, string value)
{
ICell cell = GetCell(row, column);
cell.SetCellValue(value);
}
public void SetCellValue(int row, int column, Double value)
{
ICell cell = GetCell(row, column);
cell.SetCellValue(value);
}
public void SetCellValue(int row, int column, DateTime value)
{
ICell cell = GetCell(row, column);
cell.SetCellValue(value);
}
public void SetCellValue(int row, int column, bool value)
{
ICell cell = GetCell(row, column);
cell.SetCellValue(value);
}
public void SetCellValue(int row, int column, IRichTextString value)
{
ICell cell = GetCell(row, column);
cell.SetCellValue(value);
}
public void InsertDataTable(System.Data.DataTable dt)
{
for (int i = 0; i <= dt.Columns.Count - 1; i++)
{
GetCell(0, i).SetCellValue(dt.Columns[i].ColumnName);
}
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
for (int j = 0; j <= dt.Columns.Count - 1; j++)
{
ICell cell = GetCell(i + 1, j);
cell.SetCellValue(dt.Rows[i][j].ToString());
}
}
}
public void Save()
{
if (_fileStream == null)
{
throw new Exception("_fileStream不能为空");
}
_workbook.Write(_fileStream);
_fileStream.Close();
_workbook.Close();
}
public void Save(string fileName)
{
FileStream fileStream = new FileStream(fileName, FileMode.Create);
try
{
_workbook.Write(fileStream);
}
finally
{
fileStream.Close();
_workbook.Close();
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
{
if (!this._disposed)
{
if (disposing)
{
if (_fileStream != null)
_fileStream.Close();
}
_fileStream = null;
_disposed = true;
}
}
}
}