Aspose.cell使用的一些方法笔记,方便自己记录。
Aspose.cell为Aspose中的一个操作EXCEL的类库,本次使用版本为18.4.0.0。
Workbook wb = new Workbook();
Worksheet ws = wb.Worksheets[0];//或者(Worksheet ws = wb.Worksheets["表单名称"]);
Workbook wb = new Workbook();
wb.Worksheets.Clear();
wb.Worksheets.Add("表单名称1");
Worksheet ws = wb.Worksheets[0];
wb.Worksheets.Add("表单名称2");
Worksheet ws = wb.Worksheets[1];
///
/// 打开指定路径工作簿
///
/// 工作簿路径
///
public static Workbook OpenWorkbook(string path)
{
return File.Exists(path) ? new Workbook(path) : null;
}
///
/// 获取指定编号表单
///
/// 已打开工作簿对象
/// 表单索引
///
public static Worksheet GetWorksheet(Workbook workbook, int sheetIndex)
{
return sheetIndex < 0 ? null : workbook.Worksheets[sheetIndex];
}
///
/// 根据路径获取指定编号表单
///
/// 工作簿路径
/// 表单索引
///
public static Worksheet GetWorksheet(string path, int sheetIndex)
{
return GetWorksheet(OpenWorkbook(path), sheetIndex);
}
///
/// 获取单元格(按行列索引获取且包括单元格)
///
///
///
///
///
public static Cells GetCell(Worksheet sheet, int rowIndex, int colIndex)
{
var cell = sheet.Cells[rowIndex, colIndex];
return GetCell;
}
///
/// 获取单元格字符串
///
///
///
///
///
public string GetCellsValue(Cells cell, int rowIndex, int colIndex)
{
string str = GetRealStr(cell[rowIndex, colIndex].StringValue);
return str;
}
///
/// 去除字符串中特殊字符
///
/// 字符串
///
public string GetRealStr(string Str)
{
string realstr=Str.Trim().Replace("\n", "").Replace(" ", "").Replace("\t", "").Replace("\r", "");
return realstr;
}
///
/// 设置单元格(按行列索引获取且包括单元格)
///
///
///
///
///
public static void SetCell(Worksheet sheet, int rowIndex, int colIndex, string value)
{
var cell = sheet.Cells[rowIndex, colIndex];
SetCell(cell, value);
}
///
/// 设置单元格(包括合并单元格)
///
///
///
public static void SetCell(Cell cell, string value)
{
if (cell.IsMerged)
{
cell.GetMergedRange().PutValue(value, false, false);
}
else
{
cell.PutValue(value);
}
}
///
/// 自适应行高
///
///
public static void AutoFitRows(Worksheet sheet)
{
sheet.AutoFitRows(sheet.Cells.MinDataRow, sheet.Cells.MaxDataRow);
}
///
/// 自适应列宽
///
///
public static void AutoFitColumns(Worksheet sheet)
{
sheet.AutoFitColumns(sheet.Cells.MinDataColumn, sheet.Cells.MaxDataColumn);
}
持续更新中
《Aspose下载链接》