1.新建项目
2.拷贝 Aspose.Cell 文件到项目文件夹并引用
3.添加 AsposeExcelHelper 插件类
#region 构造函数
public AsposeExcelHelper()
{
book = new Workbook();
SetSheets(0);
}
#endregion
#region Excel属性
public Worksheet sheet { get; set; }
public Workbook book { get; set; }
#endregion
#region Excel操作方法
/// <summary>
/// 打开Excel文件,默认第一个Sheet
/// </summary>
/// <param name="strPath"></param>
public void OpenExcel(string strPath)
{
if (string.IsNullOrEmpty(strPath))
{
book = new Workbook();
SetSheets(0);
}
else
{
book.Open(strPath);
SetSheets(0);
}
}
/// <summary>
/// 打开Excel文件,选择打开某一个Sheet
/// </summary>
/// <param name="strPath"></param>
/// <param name="sheetIndex"></param>
/// <returns></returns>
public bool OpenExcel(string strPath, int sheetIndex)
{
try
{
if (string.IsNullOrEmpty(strPath))
{
book = new Workbook();
SetSheets(sheetIndex);
}
else
{
book.Open(strPath);
SetSheets(sheetIndex);
}
return true;
}
catch (Exception)
{
return false;
}
//return wbk;
}
public void CopySheet(int A)
{
book.Worksheets.AddCopy(A);
}
/// <summary>
/// 添加一个行
/// </summary>
/// <param name="rowIndex"></param>
public void InsertRow(int rowIndex)
{
sheet.Cells.InsertRow(rowIndex);
}
/// <summary>
/// 添加一个行
/// </summary>
/// <param name="rowIndex"></param>
public void InsertColumn(int colIndex)
{
sheet.Cells.InsertColumn(colIndex);
}
/// <summary>
/// 设置Excel操作的Sheet页
/// </summary>
/// <param name="sheetIndex"></param>
public void SetSheets(int sheetIndex)
{
sheet = book.Worksheets[sheetIndex];
}
/// <summary>
/// 获取Sheet页面使用的列总数
/// </summary>
/// <returns></returns>
public int GetUsedRangeColumnsCount()
{
return sheet.Cells.MaxColumn;
}
/// <summary>
/// 获取Sheet页面使用的行总数
/// </summary>
/// <returns></returns>
public int GetUsedRangeRowCount()
{
return sheet.Cells.MaxDataRow;
}
/// <summary>
/// 根据行号,和文本获取列索引。
/// </summary>
/// <param name="rowIndex"></param>
/// <param name="strText"></param>
/// <returns></returns>
public int RowGetColumnIndex(int rowIndex, string strText)
{
int result = 0;
int colCount = GetUsedRangeColumnsCount();
for (int i = 0; i < colCount; i++)
{
string strTemp = GetCellsText(rowIndex, i);
if (strTemp == strText)
{
result = i;
break;
}
}
return result;
}
/// <summary>
/// 获取单元格的值
/// </summary>
/// <param name="rowA"></param>
/// <param name="colA"></param>
/// <returns></returns>
public string GetCellsText(int rowA, int colA)
{
Cell cell = sheet.Cells[rowA, colA];
string strText = "";
if (cell.StringValue != null)
{
strText = cell.StringValue;
}
return strText;
}
/// <summary>
/// 设置单元格的值
/// </summary>
/// <param name="row"></param>
/// <param name="col"></param>
/// <param name="strText"></param>
public void SetCellText(int row, int col, string strText)
{
if (string.IsNullOrEmpty(strText))
{
return;
}
double number = CheckISNumber(strText);
if (number == -0.99999)
{
sheet.Cells[row, col].PutValue(strText);
}
else
{
sheet.Cells[row, col].PutValue(number);
}
}
/// <summary>
/// 设置单元格的值
/// </summary>
/// <param name="row"></param>
/// <param name="col"></param>
/// <param name="strText"></param>
public void SetCellText(int row, int col, Color color, string strText)
{
if (string.IsNullOrEmpty(strText))
{
return;
}
double number = CheckISNumber(strText);
if (number == -0.99999)
{
sheet.Cells[row, col].PutValue(strText);
}
else
{
sheet.Cells[row, col].PutValue(number);
}
SetCellColor(row, col, color);
}
/// <summary>
/// 设置单元格值
/// </summary>
/// <param name="row"></param>
/// <param name="col"></param>
/// <param name="strText"></param>
public void SetCellString(int row, int col, object strText)
{
string strTemp = string.Empty;
try
{
strTemp = strText.ToString();
if (string.IsNullOrEmpty(strTemp))
{
return;
}
}
catch (Exception)
{
return;
}
sheet.Cells[row, col].PutValue(strTemp);
}
public void SetCellValue(int row, int col, string strText)
{
try
{
if (string.IsNullOrEmpty(strText))
{
return;
}
}
catch (Exception)
{
return;
}
sheet.Cells[row, col].PutValue(strText);
}
public void SetCellObject(int row, int col, Object obj)
{
try
{
if (obj != null)
{
return;
}
sheet.Cells[row, col].PutValue(obj);
}
catch (Exception)
{
return;
}