我有以下格式的excel文件。
col 1|col 2 | col 3 | status|some col|
-------------------------------------
1 | 23 | test | UDS | Test
-------------------------------------
12 | 2 | test2 | ADS | Test23
我需要将状态列中的所有值读入List。我该怎么做?这里'状态'列始终位于excel中的固定区域。恩。第8行,第AA栏将始终为“状态”
有些列标题正在重复,因此我无法将此excel读入数据表并从数据表中读取列值。
答案 0 :(得分:1)
使用Aspose.Cells API,这是完成任务的另一种简单方法:例如 示例代码:
//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的支持开发人员/传播者。
答案 1 :(得分:0)
我一直在使用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;
}
答案 2 :(得分:0)
使用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();