使用 aspose 单元格读取 excel 中某个单元格的所有值
Read all the values of a cell in excel using aspose cells
我有一个 excel 文件,格式如下。
col 1|col 2 | col 3 | status|some col|
-------------------------------------
1 | 23 | test | UDS | Test
-------------------------------------
12 | 2 | test2 | ADS | Test23
我需要将状态列中的所有值读入列表。我该怎么做?这里 'Status' 列始终位于 excel 中的固定区域。前任。第 8 行,AA 列将始终为 'Status'
某些列 headers 重复,所以我无法将此 excel 读入数据表并从数据表中读取列值。
我也一直在 excel 阅读。这是我实施的方式,可能会对您有所帮助。我已经更新了代码以匹配所问的问题。如果我能以任何方式帮助您,请告诉我!
public static List<string> ReadExcelDataFile(string fileFullPath)
{
Application xlApp = new Application();
Workbook xlWorkBook = null;
Worksheet dataSheet = null;
Range dataRange = null;
List<string> data = new List<string>();
object[,] valueArray;
try
{
// Open the excel file
xlWorkBook = xlApp.Workbooks.Open(fileFullPath, null, true);
if (xlWorkBook.Worksheets != null
&& xlWorkBook.Worksheets.Count > 0)
{
// Get the first data sheet
dataSheet = xlWorkBook.Worksheets[1];
// Get range of data in the worksheet
dataRange = dataSheet.UsedRange;
// Read all data from data range in the worksheet
valueArray = (object[,])dataRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
// Here if you sure of the column index then you need not loop and find the column in excel shet. Just directly index to the column and skip first loop
for (int colIndex = 0; colIndex < valueArray.GetLength(1); colIndex++)
{
if (valueArray[0, colIndex] != null
&& !string.IsNullOrEmpty(valueArray[0, colIndex].ToString())
&& valueArray[0, colIndex].ToString().Equals("status"))
{
for (int rowIndex = 1; rowIndex < valueArray.GetLength(0); rowIndex++)
{
if (valueArray[rowIndex, colIndex] != null
&& !string.IsNullOrEmpty(valueArray[rowIndex, colIndex].ToString()))
{
// Get data from each column which is not null and is a numeric value
data.Add(valueArray[rowIndex, colIndex].ToString());
}
}
}
}
}
else
{
throw new Exception("Invalid or Empty sheet");
}
}
catch (Exception generalException)
{
throw generalException;
}
finally
{
if (xlWorkBook != null)
{
// Close the workbook after job is done
xlWorkBook.Close();
xlApp.Quit();
}
}
return data;
}
使用 Aspose.Cells 我可以按如下方式执行此操作,
Workbook workbook = new Workbook(exlFile);
Worksheet worksheet = workbook.Worksheets[0];
int column = 3; //this is fixed
int row = 5; // this is fixed
int rows = worksheet.Cells.MaxRow;
Range range = worksheet.Cells.CreateRange(row, column, rows - row + 1, 1);
DataTable dataTable = range.ExportDataTable();
使用 Aspose.Cells API,这是完成 task:e.g 的另一种简单方法
示例代码:
//Open your template file.
Workbook wb = new Workbook("e:\test2\Book1.xlsx");
//Get the first worksheet.
Worksheet worksheet = wb.Worksheets[0];
//Get the cells collection.
Cells cells = worksheet.Cells;
//Define the list.
List<string> myList = new List<string>();
//Get the AA column index. (Since "Status" is always @ AA column.
int col = CellsHelper.ColumnNameToIndex("AA");
//Get the last row index in AA column.
int last_row = worksheet.Cells.GetLastDataRow(col);
//Loop through the "Status" column while start collecting values from row 9
//to save each value to List
for (int i = 8; i <= last_row; i++)
{
myList.Add(cells[i, col].Value.ToString());
}
}
我在 Aspose 担任支持开发人员/传播者。
我有一个 excel 文件,格式如下。
col 1|col 2 | col 3 | status|some col|
-------------------------------------
1 | 23 | test | UDS | Test
-------------------------------------
12 | 2 | test2 | ADS | Test23
我需要将状态列中的所有值读入列表。我该怎么做?这里 'Status' 列始终位于 excel 中的固定区域。前任。第 8 行,AA 列将始终为 'Status'
某些列 headers 重复,所以我无法将此 excel 读入数据表并从数据表中读取列值。
我也一直在 excel 阅读。这是我实施的方式,可能会对您有所帮助。我已经更新了代码以匹配所问的问题。如果我能以任何方式帮助您,请告诉我!
public static List<string> ReadExcelDataFile(string fileFullPath)
{
Application xlApp = new Application();
Workbook xlWorkBook = null;
Worksheet dataSheet = null;
Range dataRange = null;
List<string> data = new List<string>();
object[,] valueArray;
try
{
// Open the excel file
xlWorkBook = xlApp.Workbooks.Open(fileFullPath, null, true);
if (xlWorkBook.Worksheets != null
&& xlWorkBook.Worksheets.Count > 0)
{
// Get the first data sheet
dataSheet = xlWorkBook.Worksheets[1];
// Get range of data in the worksheet
dataRange = dataSheet.UsedRange;
// Read all data from data range in the worksheet
valueArray = (object[,])dataRange.get_Value(XlRangeValueDataType.xlRangeValueDefault);
// Here if you sure of the column index then you need not loop and find the column in excel shet. Just directly index to the column and skip first loop
for (int colIndex = 0; colIndex < valueArray.GetLength(1); colIndex++)
{
if (valueArray[0, colIndex] != null
&& !string.IsNullOrEmpty(valueArray[0, colIndex].ToString())
&& valueArray[0, colIndex].ToString().Equals("status"))
{
for (int rowIndex = 1; rowIndex < valueArray.GetLength(0); rowIndex++)
{
if (valueArray[rowIndex, colIndex] != null
&& !string.IsNullOrEmpty(valueArray[rowIndex, colIndex].ToString()))
{
// Get data from each column which is not null and is a numeric value
data.Add(valueArray[rowIndex, colIndex].ToString());
}
}
}
}
}
else
{
throw new Exception("Invalid or Empty sheet");
}
}
catch (Exception generalException)
{
throw generalException;
}
finally
{
if (xlWorkBook != null)
{
// Close the workbook after job is done
xlWorkBook.Close();
xlApp.Quit();
}
}
return data;
}
使用 Aspose.Cells 我可以按如下方式执行此操作,
Workbook workbook = new Workbook(exlFile);
Worksheet worksheet = workbook.Worksheets[0];
int column = 3; //this is fixed
int row = 5; // this is fixed
int rows = worksheet.Cells.MaxRow;
Range range = worksheet.Cells.CreateRange(row, column, rows - row + 1, 1);
DataTable dataTable = range.ExportDataTable();
使用 Aspose.Cells API,这是完成 task:e.g 的另一种简单方法 示例代码:
//Open your template file.
Workbook wb = new Workbook("e:\test2\Book1.xlsx");
//Get the first worksheet.
Worksheet worksheet = wb.Worksheets[0];
//Get the cells collection.
Cells cells = worksheet.Cells;
//Define the list.
List<string> myList = new List<string>();
//Get the AA column index. (Since "Status" is always @ AA column.
int col = CellsHelper.ColumnNameToIndex("AA");
//Get the last row index in AA column.
int last_row = worksheet.Cells.GetLastDataRow(col);
//Loop through the "Status" column while start collecting values from row 9
//to save each value to List
for (int i = 8; i <= last_row; i++)
{
myList.Add(cells[i, col].Value.ToString());
}
}
我在 Aspose 担任支持开发人员/传播者。