获取单元格内容
Contents
[
Hide
]
Aspose.Cells - 获取单元格内容
Cells[0]或Cells[name]方法可用于访问单元格。
C#
Workbook workbook = new Workbook("../../data/test.xlsx");
Worksheet sheet1 = workbook.Worksheets[0];
Cells cells = sheet1.Cells;
Range range = sheet1.Cells.MaxDisplayRange;
int tcols = range.ColumnCount;
int trows = range.RowCount;
for (int i = 0 ; i < trows; i++)
{
for (int j = 0 ; j < tcols ; j++)
{
if (cells[i, j].Type != CellValueType.IsNull)
{
Console.WriteLine(cells[i, j].Name + " - " + cells[i, j].Value);
}
}
}
NPOI - HSSF XSSF - 获取单元格内容
NPOI提供了Cell类来访问单元格的各种属性。
C#
IWorkbook wb = new XSSFWorkbook("../../data/test.xlsx");
ISheet sheet1 = wb.GetSheetAt(0);
for (int index = 0; index <= sheet1.LastRowNum; index++)
{
IRow row = sheet1.GetRow(index);
foreach (ICell cell in row.Cells)
{
CellReference cellRef = new CellReference(row.RowNum, cell.ColumnIndex);
Console.Write(cellRef.FormatAsString());
Console.Write(" - ");
switch (cell.CellType)
{
case CellType.String:
Console.Write(cell.StringCellValue);
break;
case CellType.Numeric:
if (DateUtil.IsCellDateFormatted(cell))
Console.Write(cell.DateCellValue);
else
Console.Write(cell.NumericCellValue);
break;
case CellType.Boolean:
Console.Write(cell.BooleanCellValue);
break;
case CellType.Formula:
Console.Write(cell.CellFormula);
break;
}
Console.WriteLine();
}
}
下载运行代码
从以下任何社交编程网站下载获取单元格内容
更多详情,请访问Aspose.Cells数据处理功能